refactor: rename ActionMethod

This commit is contained in:
lilong.129
2023-04-25 14:39:29 +08:00
parent 7a255e6ef5
commit 556050367e
3 changed files with 51 additions and 49 deletions
+38 -40
View File
@@ -23,23 +23,20 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
type MobileMethod string type ActionMethod string
const ( const (
AppInstall MobileMethod = "install" ACTION_AppInstall ActionMethod = "install"
AppUninstall MobileMethod = "uninstall" ACTION_AppUninstall ActionMethod = "uninstall"
AppStart MobileMethod = "app_start" ACTION_AppStart ActionMethod = "app_start"
AppLaunch MobileMethod = "app_launch" // 启动 app 并堵塞等待 app 首屏加载完成 ACTION_AppLaunch ActionMethod = "app_launch" // 启动 app 并堵塞等待 app 首屏加载完成
AppTerminate MobileMethod = "app_terminate" ACTION_AppTerminate ActionMethod = "app_terminate"
AppStop MobileMethod = "app_stop" ACTION_AppStop ActionMethod = "app_stop"
CtlScreenShot MobileMethod = "screenshot" ACTION_ScreenShot ActionMethod = "screenshot"
CtlSleep MobileMethod = "sleep" ACTION_Sleep ActionMethod = "sleep"
CtlSleepRandom MobileMethod = "sleep_random" ACTION_SleepRandom ActionMethod = "sleep_random"
CtlStartCamera MobileMethod = "camera_start" // alias for app_launch camera ACTION_StartCamera ActionMethod = "camera_start" // alias for app_launch camera
CtlStopCamera MobileMethod = "camera_stop" // alias for app_terminate camera ACTION_StopCamera ActionMethod = "camera_stop" // alias for app_terminate camera
RecordStart MobileMethod = "record_start"
RecordStop MobileMethod = "record_stop"
VideoCrawler MobileMethod = "video_crawler"
// UI validation // UI validation
// selectors // selectors
@@ -55,26 +52,27 @@ const (
AssertionNotExists string = "not_exists" AssertionNotExists string = "not_exists"
// UI handling // UI handling
ACTION_Home MobileMethod = "home" ACTION_Home ActionMethod = "home"
ACTION_TapXY MobileMethod = "tap_xy" ACTION_TapXY ActionMethod = "tap_xy"
ACTION_TapAbsXY MobileMethod = "tap_abs_xy" ACTION_TapAbsXY ActionMethod = "tap_abs_xy"
ACTION_TapByOCR MobileMethod = "tap_ocr" ACTION_TapByOCR ActionMethod = "tap_ocr"
ACTION_TapByCV MobileMethod = "tap_cv" ACTION_TapByCV ActionMethod = "tap_cv"
ACTION_Tap MobileMethod = "tap" ACTION_Tap ActionMethod = "tap"
ACTION_DoubleTapXY MobileMethod = "double_tap_xy" ACTION_DoubleTapXY ActionMethod = "double_tap_xy"
ACTION_DoubleTap MobileMethod = "double_tap" ACTION_DoubleTap ActionMethod = "double_tap"
ACTION_Swipe MobileMethod = "swipe" ACTION_Swipe ActionMethod = "swipe"
ACTION_Input MobileMethod = "input" ACTION_Input ActionMethod = "input"
ACTION_Back MobileMethod = "back" ACTION_Back ActionMethod = "back"
// custom actions // custom actions
ACTION_SwipeToTapApp MobileMethod = "swipe_to_tap_app" // swipe left & right to find app and tap ACTION_SwipeToTapApp ActionMethod = "swipe_to_tap_app" // swipe left & right to find app and tap
ACTION_SwipeToTapText MobileMethod = "swipe_to_tap_text" // swipe up & down to find text and tap ACTION_SwipeToTapText ActionMethod = "swipe_to_tap_text" // swipe up & down to find text and tap
ACTION_SwipeToTapTexts MobileMethod = "swipe_to_tap_texts" // swipe up & down to find text and tap ACTION_SwipeToTapTexts ActionMethod = "swipe_to_tap_texts" // swipe up & down to find text and tap
ACTION_VideoCrawler ActionMethod = "video_crawler"
) )
type MobileAction struct { type MobileAction struct {
Method MobileMethod `json:"method,omitempty" yaml:"method,omitempty"` Method ActionMethod `json:"method,omitempty" yaml:"method,omitempty"`
Params interface{} `json:"params,omitempty" yaml:"params,omitempty"` Params interface{} `json:"params,omitempty" yaml:"params,omitempty"`
Identifier string `json:"identifier,omitempty" yaml:"identifier,omitempty"` // used to identify the action in log Identifier string `json:"identifier,omitempty" yaml:"identifier,omitempty"` // used to identify the action in log
@@ -397,15 +395,15 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
log.Info().Str("method", string(action.Method)).Interface("params", action.Params).Msg("start UI action") log.Info().Str("method", string(action.Method)).Interface("params", action.Params).Msg("start UI action")
switch action.Method { switch action.Method {
case AppInstall: case ACTION_AppInstall:
// TODO // TODO
return errActionNotImplemented return errActionNotImplemented
case AppLaunch: case ACTION_AppLaunch:
if bundleId, ok := action.Params.(string); ok { if bundleId, ok := action.Params.(string); ok {
return dExt.Driver.AppLaunch(bundleId) return dExt.Driver.AppLaunch(bundleId)
} }
return fmt.Errorf("invalid %s params, should be bundleId(string), got %v", return fmt.Errorf("invalid %s params, should be bundleId(string), got %v",
AppLaunch, action.Params) ACTION_AppLaunch, action.Params)
case ACTION_SwipeToTapApp: case ACTION_SwipeToTapApp:
if appName, ok := action.Params.(string); ok { if appName, ok := action.Params.(string); ok {
return dExt.swipeToTapApp(appName, action) return dExt.swipeToTapApp(appName, action)
@@ -515,7 +513,7 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
} }
return fmt.Errorf("invalid %s params, should be app text([]string), got %v", return fmt.Errorf("invalid %s params, should be app text([]string), got %v",
ACTION_SwipeToTapText, action.Params) ACTION_SwipeToTapText, action.Params)
case AppTerminate: case ACTION_AppTerminate:
if bundleId, ok := action.Params.(string); ok { if bundleId, ok := action.Params.(string); ok {
success, err := dExt.Driver.AppTerminate(bundleId) success, err := dExt.Driver.AppTerminate(bundleId)
if err != nil { if err != nil {
@@ -640,7 +638,7 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
return dExt.Driver.Input(param, options...) return dExt.Driver.Input(param, options...)
case ACTION_Back: case ACTION_Back:
return dExt.Driver.PressBack() return dExt.Driver.PressBack()
case CtlSleep: case ACTION_Sleep:
if param, ok := action.Params.(json.Number); ok { if param, ok := action.Params.(json.Number); ok {
seconds, _ := param.Float64() seconds, _ := param.Float64()
time.Sleep(time.Duration(seconds*1000) * time.Millisecond) time.Sleep(time.Duration(seconds*1000) * time.Millisecond)
@@ -653,22 +651,22 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
return nil return nil
} }
return fmt.Errorf("invalid sleep params: %v(%T)", action.Params, action.Params) return fmt.Errorf("invalid sleep params: %v(%T)", action.Params, action.Params)
case CtlSleepRandom: case ACTION_SleepRandom:
params, ok := action.Params.([]interface{}) params, ok := action.Params.([]interface{})
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)
} }
return sleepRandom(params) return sleepRandom(params)
case CtlScreenShot: case ACTION_ScreenShot:
// take screenshot // take screenshot
log.Info().Msg("take screenshot for current screen") log.Info().Msg("take screenshot for current screen")
_, err := dExt.TakeScreenShot(builtin.GenNameWithTimestamp("step_%d_screenshot")) _, err := dExt.TakeScreenShot(builtin.GenNameWithTimestamp("step_%d_screenshot"))
return err return err
case CtlStartCamera: case ACTION_StartCamera:
return dExt.Driver.StartCamera() return dExt.Driver.StartCamera()
case CtlStopCamera: case ACTION_StopCamera:
return dExt.Driver.StopCamera() return dExt.Driver.StopCamera()
case VideoCrawler: case ACTION_VideoCrawler:
params, ok := action.Params.(map[string]interface{}) params, ok := action.Params.(map[string]interface{})
if !ok { if !ok {
return fmt.Errorf("invalid video crawler params: %v(%T)", action.Params, action.Params) return fmt.Errorf("invalid video crawler params: %v(%T)", action.Params, action.Params)
+4
View File
@@ -1,5 +1,9 @@
package uixt package uixt
type VideoCrawlerConfigs struct {
Target struct{}
}
func (dExt *DriverExt) VideoCrawler(params map[string]interface{}) error { func (dExt *DriverExt) VideoCrawler(params map[string]interface{}) error {
return nil return nil
} }
+9 -9
View File
@@ -36,7 +36,7 @@ func (s *StepMobile) Serial(serial string) *StepMobile {
func (s *StepMobile) InstallApp(path string) *StepMobile { func (s *StepMobile) InstallApp(path string) *StepMobile {
s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{ s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{
Method: uixt.AppInstall, Method: uixt.ACTION_AppInstall,
Params: path, Params: path,
}) })
return s return s
@@ -44,7 +44,7 @@ func (s *StepMobile) InstallApp(path string) *StepMobile {
func (s *StepMobile) AppLaunch(bundleId string) *StepMobile { func (s *StepMobile) AppLaunch(bundleId string) *StepMobile {
s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{ s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{
Method: uixt.AppLaunch, Method: uixt.ACTION_AppLaunch,
Params: bundleId, Params: bundleId,
}) })
return s return s
@@ -52,7 +52,7 @@ func (s *StepMobile) AppLaunch(bundleId string) *StepMobile {
func (s *StepMobile) AppTerminate(bundleId string) *StepMobile { func (s *StepMobile) AppTerminate(bundleId string) *StepMobile {
s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{ s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{
Method: uixt.AppTerminate, Method: uixt.ACTION_AppTerminate,
Params: bundleId, Params: bundleId,
}) })
return s return s
@@ -275,7 +275,7 @@ func (s *StepMobile) Input(text string, options ...uixt.ActionOption) *StepMobil
// Sleep specify sleep seconds after last action // Sleep specify sleep seconds after last action
func (s *StepMobile) Sleep(n float64) *StepMobile { func (s *StepMobile) Sleep(n float64) *StepMobile {
s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{ s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{
Method: uixt.CtlSleep, Method: uixt.ACTION_Sleep,
Params: n, Params: n,
}) })
return &StepMobile{step: s.step} return &StepMobile{step: s.step}
@@ -287,7 +287,7 @@ func (s *StepMobile) Sleep(n float64) *StepMobile {
// 2. [min1, max1, weight1, min2, max2, weight2, ...] : weight is the probability of the time range // 2. [min1, max1, weight1, min2, max2, weight2, ...] : weight is the probability of the time range
func (s *StepMobile) SleepRandom(params ...float64) *StepMobile { func (s *StepMobile) SleepRandom(params ...float64) *StepMobile {
s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{ s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{
Method: uixt.CtlSleepRandom, Method: uixt.ACTION_SleepRandom,
Params: params, Params: params,
}) })
return &StepMobile{step: s.step} return &StepMobile{step: s.step}
@@ -295,7 +295,7 @@ func (s *StepMobile) SleepRandom(params ...float64) *StepMobile {
func (s *StepMobile) VideoCrawler(params map[string]interface{}) *StepMobile { func (s *StepMobile) VideoCrawler(params map[string]interface{}) *StepMobile {
s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{ s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{
Method: uixt.VideoCrawler, Method: uixt.ACTION_VideoCrawler,
Params: params, Params: params,
}) })
return &StepMobile{step: s.step} return &StepMobile{step: s.step}
@@ -303,7 +303,7 @@ func (s *StepMobile) VideoCrawler(params map[string]interface{}) *StepMobile {
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.ACTION_ScreenShot,
Params: nil, Params: nil,
}) })
return &StepMobile{step: s.step} return &StepMobile{step: s.step}
@@ -311,7 +311,7 @@ func (s *StepMobile) ScreenShot() *StepMobile {
func (s *StepMobile) StartCamera() *StepMobile { func (s *StepMobile) StartCamera() *StepMobile {
s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{ s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{
Method: uixt.CtlStartCamera, Method: uixt.ACTION_StartCamera,
Params: nil, Params: nil,
}) })
return &StepMobile{step: s.step} return &StepMobile{step: s.step}
@@ -319,7 +319,7 @@ func (s *StepMobile) StartCamera() *StepMobile {
func (s *StepMobile) StopCamera() *StepMobile { func (s *StepMobile) StopCamera() *StepMobile {
s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{ s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{
Method: uixt.CtlStopCamera, Method: uixt.ACTION_StopCamera,
Params: nil, Params: nil,
}) })
return &StepMobile{step: s.step} return &StepMobile{step: s.step}