From ec72cb8904ab4f4c9dc596ae1c8cd6e7e7999d41 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Sun, 10 Nov 2024 22:39:34 +0800 Subject: [PATCH] feat: mobile sleep step supports specify start time --- hrp/internal/version/VERSION | 2 +- hrp/pkg/uixt/action.go | 28 +++++++++++++++++++++++++++- hrp/step_mobile_ui.go | 35 +++++++++++++++++++++++++++++++---- 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/hrp/internal/version/VERSION b/hrp/internal/version/VERSION index 4892ef0a..c97d9065 100644 --- a/hrp/internal/version/VERSION +++ b/hrp/internal/version/VERSION @@ -1 +1 @@ -v5.0.0+2411102027 +v5.0.0+2411102239 diff --git a/hrp/pkg/uixt/action.go b/hrp/pkg/uixt/action.go index 9b9f8631..f3840ab4 100644 --- a/hrp/pkg/uixt/action.go +++ b/hrp/pkg/uixt/action.go @@ -24,6 +24,7 @@ const ( ACTION_AppStop ActionMethod = "app_stop" ACTION_ScreenShot ActionMethod = "screenshot" ACTION_Sleep ActionMethod = "sleep" + ACTION_SleepMS ActionMethod = "sleep_ms" ACTION_SleepRandom ActionMethod = "sleep_random" ACTION_StartCamera ActionMethod = "camera_start" // alias for app_launch camera ACTION_StopCamera ActionMethod = "camera_stop" // alias for app_terminate camera @@ -724,8 +725,24 @@ func (dExt *DriverExt) DoAction(action MobileAction) (err error) { } else if param, ok := action.Params.(int64); ok { time.Sleep(time.Duration(param) * time.Second) return nil + } else if sd, ok := action.Params.(SleepConfig); ok { + sleepStrict(sd.StartTime, int64(sd.Seconds*1000)) + return nil } return fmt.Errorf("invalid sleep params: %v(%T)", action.Params, action.Params) + case ACTION_SleepMS: + if param, ok := action.Params.(json.Number); ok { + milliseconds, _ := param.Int64() + time.Sleep(time.Duration(milliseconds) * time.Millisecond) + return nil + } else if param, ok := action.Params.(int64); ok { + time.Sleep(time.Duration(param) * time.Millisecond) + return nil + } else if sd, ok := action.Params.(SleepConfig); ok { + sleepStrict(sd.StartTime, sd.Milliseconds) + return nil + } + return fmt.Errorf("invalid sleep ms params: %v(%T)", action.Params, action.Params) case ACTION_SleepRandom: if params, ok := action.Params.([]interface{}); ok { sleepStrict(time.Now(), getSimulationDuration(params)) @@ -750,6 +767,12 @@ func (dExt *DriverExt) DoAction(action MobileAction) (err error) { return nil } +type SleepConfig struct { + StartTime time.Time `json:"start_time"` + Seconds float64 `json:"seconds"` + Milliseconds int64 `json:"milliseconds"` +} + var errActionNotImplemented = errors.New("UI action not implemented") // getSimulationDuration returns simulation duration by given params (in seconds) @@ -821,7 +844,10 @@ func getSimulationDuration(params []interface{}) (milliseconds int64) { // sleepStrict sleeps strict duration with given params // startTime is used to correct sleep duration caused by process time func sleepStrict(startTime time.Time, strictMilliseconds int64) { - elapsed := time.Since(startTime).Milliseconds() + var elapsed int64 + if !startTime.IsZero() { + elapsed = time.Since(startTime).Milliseconds() + } dur := strictMilliseconds - elapsed // if elapsed time is greater than given duration, skip sleep to reduce deviation caused by process time diff --git a/hrp/step_mobile_ui.go b/hrp/step_mobile_ui.go index 507c0161..8dde4c52 100644 --- a/hrp/step_mobile_ui.go +++ b/hrp/step_mobile_ui.go @@ -92,6 +92,7 @@ type MobileUI struct { // StepMobile implements IStep interface. type StepMobile struct { StepConfig + Mobile *MobileUI `json:"mobile,omitempty" yaml:"mobile,omitempty"` Android *MobileUI `json:"android,omitempty" yaml:"android,omitempty"` Harmony *MobileUI `json:"harmony,omitempty" yaml:"harmony,omitempty"` IOS *MobileUI `json:"ios,omitempty" yaml:"ios,omitempty"` @@ -117,6 +118,9 @@ func (s *StepMobile) obj() *MobileUI { s.cache = s.Android s.cache.OSType = string(stepTypeAndroid) return s.cache + } else if s.Mobile != nil { + s.cache = s.Mobile + return s.cache } panic("no mobile device config") @@ -362,12 +366,35 @@ func (s *StepMobile) Input(text string, options ...uixt.ActionOption) *StepMobil } // Sleep specify sleep seconds after last action -func (s *StepMobile) Sleep(n float64) *StepMobile { - s.obj().Actions = append(s.obj().Actions, uixt.MobileAction{ +func (s *StepMobile) Sleep(nSeconds float64, startTime ...time.Time) *StepMobile { + action := uixt.MobileAction{ Method: uixt.ACTION_Sleep, - Params: n, + Params: nSeconds, Options: nil, - }) + } + if len(startTime) > 0 { + action.Params = uixt.SleepConfig{ + StartTime: startTime[0], + Seconds: nSeconds, + } + } + s.obj().Actions = append(s.obj().Actions, action) + return s +} + +func (s *StepMobile) SleepMS(nMilliseconds int64, startTime ...time.Time) *StepMobile { + action := uixt.MobileAction{ + Method: uixt.ACTION_SleepMS, + Params: nMilliseconds, + Options: nil, + } + if len(startTime) > 0 { + action.Params = uixt.SleepConfig{ + StartTime: startTime[0], + Milliseconds: nMilliseconds, + } + } + s.obj().Actions = append(s.obj().Actions, action) return s }