mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-26 10:01:28 +08:00
feat: mobile sleep step supports specify start time
This commit is contained in:
@@ -1 +1 @@
|
||||
v5.0.0+2411102027
|
||||
v5.0.0+2411102239
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user