diff --git a/hrp/pkg/uixt/ext.go b/hrp/pkg/uixt/ext.go index d87e7b18..3a75c156 100644 --- a/hrp/pkg/uixt/ext.go +++ b/hrp/pkg/uixt/ext.go @@ -7,6 +7,7 @@ import ( "image" "image/jpeg" "image/png" + "math/rand" "mime" "mime/multipart" "net/http" @@ -32,6 +33,7 @@ const ( AppStop MobileMethod = "app_stop" CtlScreenShot MobileMethod = "screenshot" CtlSleep MobileMethod = "sleep" + CtlSleepRandom MobileMethod = "sleep_random" CtlStartCamera MobileMethod = "camera_start" // alias for app_launch camera CtlStopCamera MobileMethod = "camera_stop" // alias for app_terminate camera RecordStart MobileMethod = "record_start" @@ -315,6 +317,10 @@ func isPathExists(path string) bool { return true } +func init() { + rand.Seed(time.Now().UnixNano()) +} + func (dExt *DriverExt) FindUIRectInUIKit(search string, options ...DataOption) (x, y, width, height float64, err error) { // click on text, using OCR if !isPathExists(search) { @@ -602,6 +608,16 @@ func (dExt *DriverExt) DoAction(action MobileAction) error { return nil } return fmt.Errorf("invalid sleep params: %v(%T)", action.Params, action.Params) + case CtlSleepRandom: + if params, ok := action.Params.([]interface{}); ok && len(params) == 2 { + a := params[0].(float64) + b := params[1].(float64) + n := a + rand.Float64()*(b-a) + log.Info().Float64("duration", n).Msg("sleep random seconds") + time.Sleep(time.Duration(n*1000) * time.Millisecond) + return nil + } + return fmt.Errorf("invalid sleep random params: %v(%T)", action.Params, action.Params) case CtlScreenShot: // take snapshot log.Info().Msg("take snapshot for current screen") diff --git a/hrp/step_mobile_ui.go b/hrp/step_mobile_ui.go index 5d37b203..16dc290b 100644 --- a/hrp/step_mobile_ui.go +++ b/hrp/step_mobile_ui.go @@ -281,6 +281,14 @@ func (s *StepMobile) Sleep(n float64) *StepMobile { return &StepMobile{step: s.step} } +func (s *StepMobile) SleepRandom(a, b float64) *StepMobile { + s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{ + Method: uixt.CtlSleepRandom, + Params: []float64{a, b}, + }) + return &StepMobile{step: s.step} +} + func (s *StepMobile) ScreenShot() *StepMobile { s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{ Method: uixt.CtlScreenShot,