mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-10 06:51:44 +08:00
feat: add video crawler
This commit is contained in:
@@ -39,6 +39,7 @@ const (
|
|||||||
CtlStopCamera MobileMethod = "camera_stop" // alias for app_terminate camera
|
CtlStopCamera MobileMethod = "camera_stop" // alias for app_terminate camera
|
||||||
RecordStart MobileMethod = "record_start"
|
RecordStart MobileMethod = "record_start"
|
||||||
RecordStop MobileMethod = "record_stop"
|
RecordStop MobileMethod = "record_stop"
|
||||||
|
VideoCrawler MobileMethod = "video_crawler"
|
||||||
|
|
||||||
// UI validation
|
// UI validation
|
||||||
// selectors
|
// selectors
|
||||||
@@ -657,50 +658,7 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("invalid sleep random params: %v(%T)", action.Params, action.Params)
|
return fmt.Errorf("invalid sleep random params: %v(%T)", action.Params, action.Params)
|
||||||
}
|
}
|
||||||
// append default weight 1
|
return sleepRandom(params)
|
||||||
if len(params) == 2 {
|
|
||||||
params = append(params, 1.0)
|
|
||||||
}
|
|
||||||
|
|
||||||
var sections []struct {
|
|
||||||
min, max, weight float64
|
|
||||||
}
|
|
||||||
totalProb := 0.0
|
|
||||||
for i := 0; i+3 <= len(params); i += 3 {
|
|
||||||
min, err := convertToFloat64(params[i])
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrapf(err, "invalid minimum time: %v", params[i])
|
|
||||||
}
|
|
||||||
max, err := convertToFloat64(params[i+1])
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrapf(err, "invalid maximum time: %v", params[i+1])
|
|
||||||
}
|
|
||||||
weight, err := convertToFloat64(params[i+2])
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrapf(err, "invalid weight value: %v", params[i+2])
|
|
||||||
}
|
|
||||||
totalProb += weight
|
|
||||||
sections = append(sections,
|
|
||||||
struct{ min, max, weight float64 }{min, max, weight},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if totalProb == 0 {
|
|
||||||
log.Warn().Msg("total weight is 0, skip sleep")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
r := rand.Float64()
|
|
||||||
accProb := 0.0
|
|
||||||
for _, s := range sections {
|
|
||||||
accProb += s.weight / totalProb
|
|
||||||
if r < accProb {
|
|
||||||
n := s.min + rand.Float64()*(s.max-s.min)
|
|
||||||
log.Info().Float64("duration", n).Msg("sleep random seconds")
|
|
||||||
time.Sleep(time.Duration(n*1000) * time.Millisecond)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case CtlScreenShot:
|
case CtlScreenShot:
|
||||||
// take screenshot
|
// take screenshot
|
||||||
log.Info().Msg("take screenshot for current screen")
|
log.Info().Msg("take screenshot for current screen")
|
||||||
@@ -710,6 +668,60 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
|
|||||||
return dExt.Driver.StartCamera()
|
return dExt.Driver.StartCamera()
|
||||||
case CtlStopCamera:
|
case CtlStopCamera:
|
||||||
return dExt.Driver.StopCamera()
|
return dExt.Driver.StopCamera()
|
||||||
|
case VideoCrawler:
|
||||||
|
params, ok := action.Params.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("invalid video crawler params: %v(%T)", action.Params, action.Params)
|
||||||
|
}
|
||||||
|
return dExt.VideoCrawler(params)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func sleepRandom(params []interface{}) error {
|
||||||
|
// append default weight 1
|
||||||
|
if len(params) == 2 {
|
||||||
|
params = append(params, 1.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
var sections []struct {
|
||||||
|
min, max, weight float64
|
||||||
|
}
|
||||||
|
totalProb := 0.0
|
||||||
|
for i := 0; i+3 <= len(params); i += 3 {
|
||||||
|
min, err := convertToFloat64(params[i])
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "invalid minimum time: %v", params[i])
|
||||||
|
}
|
||||||
|
max, err := convertToFloat64(params[i+1])
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "invalid maximum time: %v", params[i+1])
|
||||||
|
}
|
||||||
|
weight, err := convertToFloat64(params[i+2])
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "invalid weight value: %v", params[i+2])
|
||||||
|
}
|
||||||
|
totalProb += weight
|
||||||
|
sections = append(sections,
|
||||||
|
struct{ min, max, weight float64 }{min, max, weight},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if totalProb == 0 {
|
||||||
|
log.Warn().Msg("total weight is 0, skip sleep")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r := rand.Float64()
|
||||||
|
accProb := 0.0
|
||||||
|
for _, s := range sections {
|
||||||
|
accProb += s.weight / totalProb
|
||||||
|
if r < accProb {
|
||||||
|
n := s.min + rand.Float64()*(s.max-s.min)
|
||||||
|
log.Info().Float64("duration", n).Msg("sleep random seconds")
|
||||||
|
time.Sleep(time.Duration(n*1000) * time.Millisecond)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
5
hrp/pkg/uixt/video_crawler.go
Normal file
5
hrp/pkg/uixt/video_crawler.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package uixt
|
||||||
|
|
||||||
|
func (dExt *DriverExt) VideoCrawler(params map[string]interface{}) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type MobileStep struct {
|
type MobileStep struct {
|
||||||
Serial string `json:"serial,omitempty" yaml:"serial,omitempty"`
|
Serial string `json:"serial,omitempty" yaml:"serial,omitempty"` // android serial or ios udid
|
||||||
uixt.MobileAction `yaml:",inline"`
|
uixt.MobileAction `yaml:",inline"`
|
||||||
Actions []uixt.MobileAction `json:"actions,omitempty" yaml:"actions,omitempty"`
|
Actions []uixt.MobileAction `json:"actions,omitempty" yaml:"actions,omitempty"`
|
||||||
}
|
}
|
||||||
@@ -293,6 +293,14 @@ func (s *StepMobile) SleepRandom(params ...float64) *StepMobile {
|
|||||||
return &StepMobile{step: s.step}
|
return &StepMobile{step: s.step}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *StepMobile) VideoCrawler(params map[string]interface{}) *StepMobile {
|
||||||
|
s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{
|
||||||
|
Method: uixt.VideoCrawler,
|
||||||
|
Params: params,
|
||||||
|
})
|
||||||
|
return &StepMobile{step: s.step}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *StepMobile) ScreenShot() *StepMobile {
|
func (s *StepMobile) ScreenShot() *StepMobile {
|
||||||
s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{
|
s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{
|
||||||
Method: uixt.CtlScreenShot,
|
Method: uixt.CtlScreenShot,
|
||||||
|
|||||||
Reference in New Issue
Block a user