feat: sleep random seconds

This commit is contained in:
debugtalk
2023-02-13 22:22:49 +08:00
parent 5a67fc1155
commit 7f93cce3eb
2 changed files with 24 additions and 0 deletions

View File

@@ -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")

View File

@@ -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,