From 3a404c8372e625b2caac618fcd61338d9e07673a Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Mon, 1 May 2023 15:09:28 +0800 Subject: [PATCH] refactor: merge ActionOption with DataOption --- examples/uitest/demo_kuaishou_test.go | 2 +- examples/worldcup/main_test.go | 2 +- hrp/pkg/uixt/action.go | 293 ++++++++++++++++++-------- hrp/pkg/uixt/android_adb_driver.go | 26 +-- hrp/pkg/uixt/android_uia2_driver.go | 26 +-- hrp/pkg/uixt/drag.go | 2 +- hrp/pkg/uixt/ext.go | 2 +- hrp/pkg/uixt/interface.go | 159 +------------- hrp/pkg/uixt/ios_driver.go | 26 +-- hrp/pkg/uixt/ocr_vedem.go | 16 +- hrp/pkg/uixt/opencv.go | 2 +- hrp/pkg/uixt/opencv_off.go | 2 +- hrp/pkg/uixt/swipe.go | 96 +++------ hrp/pkg/uixt/swipe_test.go | 21 +- hrp/pkg/uixt/tap.go | 24 +-- hrp/step_mobile_ui.go | 151 ++++++------- 16 files changed, 387 insertions(+), 463 deletions(-) diff --git a/examples/uitest/demo_kuaishou_test.go b/examples/uitest/demo_kuaishou_test.go index d0d47e27..2cc179aa 100644 --- a/examples/uitest/demo_kuaishou_test.go +++ b/examples/uitest/demo_kuaishou_test.go @@ -39,7 +39,7 @@ func TestAndroidKuaiShouFeedCardLive(t *testing.T) { uixt.WithCustomDirection(0.9, 0.7, 0.9, 0.3), uixt.WithScope(0.2, 0.5, 0.8, 0.8), uixt.WithMaxRetryTimes(20), - uixt.WithWaitTime(60), + uixt.WithInterval(60), uixt.WithIdentifier("click_live"), ), hrp.NewStep("等待1分钟"). diff --git a/examples/worldcup/main_test.go b/examples/worldcup/main_test.go index 03fa9e9c..ce54be59 100644 --- a/examples/worldcup/main_test.go +++ b/examples/worldcup/main_test.go @@ -89,7 +89,7 @@ func TestIOSDouyinWorldCupLive(t *testing.T) { uixt.WithMaxRetryTimes(5), uixt.WithCustomDirection(0.4, 0.07, 0.6, 0.07), // 滑动 tab,从左到右,解决「世界杯」被遮挡的问题 uixt.WithScope(0, 0, 1, 0.15), // 限定 tab 区域 - uixt.WithWaitTime(1), + uixt.WithInterval(1), ), hrp.NewStep("点击进入赛程晋级"). Loop(5). // 重复执行 5 次 diff --git a/hrp/pkg/uixt/action.go b/hrp/pkg/uixt/action.go index 7518c327..9778b623 100644 --- a/hrp/pkg/uixt/action.go +++ b/hrp/pkg/uixt/action.go @@ -3,6 +3,7 @@ package uixt import ( "encoding/json" "fmt" + "math" "math/rand" "time" @@ -61,116 +62,260 @@ const ( ) type MobileAction struct { - Method ActionMethod `json:"method,omitempty" yaml:"method,omitempty"` - Params interface{} `json:"params,omitempty" yaml:"params,omitempty"` - - Identifier string `json:"identifier,omitempty" yaml:"identifier,omitempty"` // used to identify the action in log - MaxRetryTimes int `json:"max_retry_times,omitempty" yaml:"max_retry_times,omitempty"` // max retry times - WaitTime float64 `json:"wait_time,omitempty" yaml:"wait_time,omitempty"` // wait time between swipe and ocr, unit: second - Duration float64 `json:"duration,omitempty" yaml:"duration,omitempty"` // used to set duration of ios swipe action - Steps int `json:"steps,omitempty" yaml:"steps,omitempty"` // used to set steps of android swipe action - Direction interface{} `json:"direction,omitempty" yaml:"direction,omitempty"` // used by swipe to tap text or app - Scope []float64 `json:"scope,omitempty" yaml:"scope,omitempty"` // used by ocr to get text position in the scope - Offset []int `json:"offset,omitempty" yaml:"offset,omitempty"` // used to tap offset of point - Index int `json:"index,omitempty" yaml:"index,omitempty"` // index of the target element, should start from 1 - Timeout int `json:"timeout,omitempty" yaml:"timeout,omitempty"` // TODO: wait timeout in seconds for mobile action - IgnoreNotFoundError bool `json:"ignore_NotFoundError,omitempty" yaml:"ignore_NotFoundError,omitempty"` // ignore error if target element not found - Text string `json:"text,omitempty" yaml:"text,omitempty"` - ID string `json:"id,omitempty" yaml:"id,omitempty"` - Description string `json:"description,omitempty" yaml:"description,omitempty"` + Method ActionMethod `json:"method,omitempty" yaml:"method,omitempty"` + Params interface{} `json:"params,omitempty" yaml:"params,omitempty"` + Options *ActionOptions `json:"options,omitempty" yaml:"options,omitempty"` } -type ActionOption func(o *MobileAction) +type ActionOptions struct { + // log + Identifier string `json:"identifier,omitempty" yaml:"identifier,omitempty"` // used to identify the action in log + + // control related + MaxRetryTimes int `json:"max_retry_times,omitempty" yaml:"max_retry_times,omitempty"` // max retry times + IgnoreNotFoundError bool `json:"ignore_NotFoundError,omitempty" yaml:"ignore_NotFoundError,omitempty"` // ignore error if target element not found + Interval float64 `json:"interval,omitempty" yaml:"interval,omitempty"` // interval between retries in seconds + PressDuration float64 `json:"duration,omitempty" yaml:"duration,omitempty"` // used to set duration of ios swipe action + Steps int `json:"steps,omitempty" yaml:"steps,omitempty"` // used to set steps of android swipe action + Direction interface{} `json:"direction,omitempty" yaml:"direction,omitempty"` // used by swipe to tap text or app + Timeout int `json:"timeout,omitempty" yaml:"timeout,omitempty"` // TODO: wait timeout in seconds for mobile action + Frequency int `json:"frequency,omitempty" yaml:"frequency,omitempty"` + + // scope related + Scope []float64 `json:"scope,omitempty" yaml:"scope,omitempty"` // used by ocr to get text position in the scope + AbsScope []int `json:"abs_scope,omitempty" yaml:"abs_scope,omitempty"` // used by ocr to get text position in the scope + Offset []int `json:"offset,omitempty" yaml:"offset,omitempty"` // used to tap offset of point + Index int `json:"index,omitempty" yaml:"index,omitempty"` // index of the target element, should start from 1 + + // element related + Text string `json:"text,omitempty" yaml:"text,omitempty"` + ID string `json:"id,omitempty" yaml:"id,omitempty"` + Description string `json:"description,omitempty" yaml:"description,omitempty"` + + // set custiom options such as textview, id, description + Custom map[string]interface{} `json:"custom,omitempty" yaml:"custom,omitempty"` +} + +func (o *ActionOptions) Options() []ActionOption { + options := make([]ActionOption, 0) + + if o.Identifier != "" { + options = append(options, WithIdentifier(o.Identifier)) + } + + if o.MaxRetryTimes != 0 { + options = append(options, WithMaxRetryTimes(o.MaxRetryTimes)) + } + if o.IgnoreNotFoundError { + options = append(options, WithIgnoreNotFoundError(true)) + } + if o.Interval != 0 { + options = append(options, WithInterval(o.Interval)) + } + if o.PressDuration != 0 { + options = append(options, WithPressDuration(o.PressDuration)) + } + if o.Steps != 0 { + options = append(options, WithSteps(o.Steps)) + } + + switch v := o.Direction.(type) { + case string: + options = append(options, WithDirection(v)) + case []float64: + options = append(options, WithCustomDirection( + v[0], v[1], + v[2], v[3], + )) + case []interface{}: + // loaded from json case + // custom direction: [fromX, fromY, toX, toY] + sx, _ := builtin.Interface2Float64(v[0]) + sy, _ := builtin.Interface2Float64(v[1]) + ex, _ := builtin.Interface2Float64(v[2]) + ey, _ := builtin.Interface2Float64(v[3]) + options = append(options, WithCustomDirection( + sx, sy, + ex, ey, + )) + } + + if o.Timeout != 0 { + options = append(options, WithTimeout(o.Timeout)) + } + if o.Frequency != 0 { + options = append(options, WithFrequency(o.Frequency)) + } + if len(o.Scope) != 4 { + options = append(options, WithScope(0, 0, 1, 1)) + } + if len(o.AbsScope) != 4 { + o.AbsScope = []int{0, 0, math.MaxInt64, math.MaxInt64} + } + if len(o.Offset) == 2 { + options = append(options, WithOffset(o.Offset[0], o.Offset[1])) + } + + // custom options + if o.Custom != nil { + for k, v := range o.Custom { + options = append(options, WithCustomOption(k, v)) + } + } + + return options +} + +func NewActionOptions(options ...ActionOption) *ActionOptions { + actionOptions := &ActionOptions{ + Custom: make(map[string]interface{}), + } + for _, option := range options { + option(actionOptions) + } + return actionOptions +} + +func mergeDataWithOptions(data map[string]interface{}, options ...ActionOption) map[string]interface{} { + actionOptions := NewActionOptions(options...) + + // custom options + for k, v := range actionOptions.Custom { + data[k] = v + } + + if actionOptions.Identifier != "" { + data["log"] = map[string]interface{}{ + "enable": true, + "data": actionOptions.Identifier, + } + } + + // handle point offset + if len(actionOptions.Offset) == 2 { + if x, ok := data["x"]; ok { + xf, _ := builtin.Interface2Float64(x) + data["x"] = xf + float64(actionOptions.Offset[0]) + } + if y, ok := data["y"]; ok { + yf, _ := builtin.Interface2Float64(y) + data["y"] = yf + float64(actionOptions.Offset[1]) + } + } + + if actionOptions.Steps > 0 { + data["steps"] = actionOptions.Steps + } + if _, ok := data["steps"]; !ok { + data["steps"] = 12 // default steps + } + + if actionOptions.PressDuration > 0 { + data["duration"] = actionOptions.PressDuration + } + if _, ok := data["duration"]; !ok { + data["duration"] = 0 // default duration + } + + if actionOptions.Frequency > 0 { + data["frequency"] = actionOptions.Frequency + } + if _, ok := data["frequency"]; !ok { + data["frequency"] = 60 // default frequency + } + + if _, ok := data["isReplace"]; !ok { + data["isReplace"] = true // default true + } + + return data +} + +type ActionOption func(o *ActionOptions) + +func WithCustomOption(key string, value interface{}) ActionOption { + return func(o *ActionOptions) { + o.Custom[key] = value + } +} func WithIdentifier(identifier string) ActionOption { - return func(o *MobileAction) { + return func(o *ActionOptions) { o.Identifier = identifier } } func WithIndex(index int) ActionOption { - return func(o *MobileAction) { + return func(o *ActionOptions) { o.Index = index } } -func WithWaitTime(sec float64) ActionOption { - return func(o *MobileAction) { - o.WaitTime = sec +func WithInterval(sec float64) ActionOption { + return func(o *ActionOptions) { + o.Interval = sec } } -func WithDuration(duration float64) ActionOption { - return func(o *MobileAction) { - o.Duration = duration +func WithPressDuration(duration float64) ActionOption { + return func(o *ActionOptions) { + o.PressDuration = duration } } func WithSteps(steps int) ActionOption { - return func(o *MobileAction) { + return func(o *ActionOptions) { o.Steps = steps } } // WithDirection inputs direction (up, down, left, right) func WithDirection(direction string) ActionOption { - return func(o *MobileAction) { + return func(o *ActionOptions) { o.Direction = direction } } // WithCustomDirection inputs sx, sy, ex, ey func WithCustomDirection(sx, sy, ex, ey float64) ActionOption { - return func(o *MobileAction) { + return func(o *ActionOptions) { o.Direction = []float64{sx, sy, ex, ey} } } // WithScope inputs area of [(x1,y1), (x2,y2)] func WithScope(x1, y1, x2, y2 float64) ActionOption { - return func(o *MobileAction) { + return func(o *ActionOptions) { o.Scope = []float64{x1, y1, x2, y2} } } func WithOffset(offsetX, offsetY int) ActionOption { - return func(o *MobileAction) { + return func(o *ActionOptions) { o.Offset = []int{offsetX, offsetY} } } -func WithText(text string) ActionOption { - return func(o *MobileAction) { - o.Text = text - } -} - -func WithID(id string) ActionOption { - return func(o *MobileAction) { - o.ID = id - } -} - -func WithDescription(description string) ActionOption { - return func(o *MobileAction) { - o.Description = description +func WithFrequency(frequency int) ActionOption { + return func(o *ActionOptions) { + o.Frequency = frequency } } func WithMaxRetryTimes(maxRetryTimes int) ActionOption { - return func(o *MobileAction) { + return func(o *ActionOptions) { o.MaxRetryTimes = maxRetryTimes } } func WithTimeout(timeout int) ActionOption { - return func(o *MobileAction) { + return func(o *ActionOptions) { o.Timeout = timeout } } func WithIgnoreNotFoundError(ignoreError bool) ActionOption { - return func(o *MobileAction) { + return func(o *ActionOptions) { o.IgnoreNotFoundError = ignoreError } } @@ -190,13 +335,13 @@ func (dExt *DriverExt) DoAction(action MobileAction) error { ACTION_AppLaunch, action.Params) case ACTION_SwipeToTapApp: if appName, ok := action.Params.(string); ok { - return dExt.swipeToTapApp(appName, action) + return dExt.swipeToTapApp(appName, action.Options.Options()...) } return fmt.Errorf("invalid %s params, should be app name(string), got %v", ACTION_SwipeToTapApp, action.Params) case ACTION_SwipeToTapText: if text, ok := action.Params.(string); ok { - return dExt.swipeToTapTexts([]string{text}, action) + return dExt.swipeToTapTexts([]string{text}, action.Options.Options()...) } return fmt.Errorf("invalid %s params, should be app text(string), got %v", ACTION_SwipeToTapText, action.Params) @@ -209,7 +354,7 @@ func (dExt *DriverExt) DoAction(action MobileAction) error { action.Params = textList } if texts, ok := action.Params.([]string); ok { - return dExt.swipeToTapTexts(texts, action) + return dExt.swipeToTapTexts(texts, action.Options.Options()...) } return fmt.Errorf("invalid %s params, should be app text([]string), got %v", ACTION_SwipeToTapText, action.Params) @@ -235,7 +380,7 @@ func (dExt *DriverExt) DoAction(action MobileAction) error { } x, _ := location[0].(float64) y, _ := location[1].(float64) - return dExt.TapXY(x, y, WithDataIdentifier(action.Identifier)) + return dExt.TapXY(x, y, action.Options.Options()...) } return fmt.Errorf("invalid %s params: %v", ACTION_TapXY, action.Params) case ACTION_TapAbsXY: @@ -246,37 +391,32 @@ func (dExt *DriverExt) DoAction(action MobileAction) error { } x, _ := location[0].(float64) y, _ := location[1].(float64) - if len(action.Offset) != 2 { - action.Offset = []int{0, 0} + if len(action.Options.Offset) != 2 { + action.Options.Offset = []int{0, 0} } - return dExt.TapAbsXY(x, y, WithDataIdentifier(action.Identifier), WithDataOffset(action.Offset[0], action.Offset[1])) + return dExt.TapAbsXY(x, y, action.Options.Options()...) } return fmt.Errorf("invalid %s params: %v", ACTION_TapAbsXY, action.Params) case ACTION_Tap: if param, ok := action.Params.(string); ok { - return dExt.Tap(param, WithDataIdentifier(action.Identifier), WithDataIgnoreNotFoundError(true), WithDataIndex(action.Index)) + return dExt.Tap(param, action.Options.Options()...) } return fmt.Errorf("invalid %s params: %v", ACTION_Tap, action.Params) case ACTION_TapByOCR: if ocrText, ok := action.Params.(string); ok { - if len(action.Scope) != 4 { - action.Scope = []float64{0, 0, 1, 1} + if len(action.Options.Scope) != 4 { + action.Options.Scope = []float64{0, 0, 1, 1} } - if len(action.Offset) != 2 { - action.Offset = []int{0, 0} + if len(action.Options.Offset) != 2 { + action.Options.Offset = []int{0, 0} } - indexOption := WithDataIndex(action.Index) - offsetOption := WithDataOffset(action.Offset[0], action.Offset[1]) - scopeOption := WithDataScope(dExt.getAbsScope(action.Scope[0], action.Scope[1], action.Scope[2], action.Scope[3])) - identifierOption := WithDataIdentifier(action.Identifier) - IgnoreNotFoundErrorOption := WithDataIgnoreNotFoundError(action.IgnoreNotFoundError) - return dExt.TapByOCR(ocrText, identifierOption, IgnoreNotFoundErrorOption, indexOption, scopeOption, offsetOption) + return dExt.TapByOCR(ocrText, action.Options.Options()...) } return fmt.Errorf("invalid %s params: %v", ACTION_TapByOCR, action.Params) case ACTION_TapByCV: if imagePath, ok := action.Params.(string); ok { - return dExt.TapByCV(imagePath, WithDataIdentifier(action.Identifier), WithDataIgnoreNotFoundError(true), WithDataIndex(action.Index)) + return dExt.TapByCV(imagePath, action.Options.Options()...) } return fmt.Errorf("invalid %s params: %v", ACTION_TapByCV, action.Params) case ACTION_DoubleTapXY: @@ -296,27 +436,14 @@ func (dExt *DriverExt) DoAction(action MobileAction) error { } return fmt.Errorf("invalid %s params: %v", ACTION_DoubleTap, action.Params) case ACTION_Swipe: - swipeAction := dExt.prepareSwipeAction(action) + swipeAction := dExt.prepareSwipeAction(action.Options.Options()...) return swipeAction(dExt) case ACTION_Input: // input text on current active element // append \n to send text with enter // send \b\b\b to delete 3 chars param := fmt.Sprintf("%v", action.Params) - options := []DataOption{} - if action.Text != "" { - options = append(options, WithCustomOption("textview", action.Text)) - } - if action.ID != "" { - options = append(options, WithCustomOption("id", action.ID)) - } - if action.Description != "" { - options = append(options, WithCustomOption("description", action.Description)) - } - if action.Identifier != "" { - options = append(options, WithDataIdentifier(action.Identifier)) - } - return dExt.Driver.Input(param, options...) + return dExt.Driver.Input(param, action.Options.Options()...) case ACTION_Back: return dExt.Driver.PressBack() case ACTION_Sleep: diff --git a/hrp/pkg/uixt/android_adb_driver.go b/hrp/pkg/uixt/android_adb_driver.go index 9bc6992c..858ad229 100644 --- a/hrp/pkg/uixt/android_adb_driver.go +++ b/hrp/pkg/uixt/android_adb_driver.go @@ -81,7 +81,7 @@ func (ad *adbDriver) Scale() (scale float64, err error) { } // PressBack simulates a short press on the BACK button. -func (ad *adbDriver) PressBack(options ...DataOption) (err error) { +func (ad *adbDriver) PressBack(options ...ActionOption) (err error) { // adb shell input keyevent 4 _, err = ad.adbClient.RunShellCommand("input", "keyevent", fmt.Sprintf("%d", KCBack)) return @@ -185,16 +185,16 @@ func (ad *adbDriver) AppTerminate(packageName string) (successful bool, err erro return true, nil } -func (ad *adbDriver) Tap(x, y int, options ...DataOption) error { +func (ad *adbDriver) Tap(x, y int, options ...ActionOption) error { return ad.TapFloat(float64(x), float64(y), options...) } -func (ad *adbDriver) TapFloat(x, y float64, options ...DataOption) (err error) { - dataOptions := NewDataOptions(options...) +func (ad *adbDriver) TapFloat(x, y float64, options ...ActionOption) (err error) { + actionOptions := NewActionOptions(options...) - if len(dataOptions.Offset) == 2 { - x += float64(dataOptions.Offset[0]) - y += float64(dataOptions.Offset[1]) + if len(actionOptions.Offset) == 2 { + x += float64(actionOptions.Offset[0]) + y += float64(actionOptions.Offset[1]) } // adb shell input tap x y @@ -221,20 +221,20 @@ func (ad *adbDriver) TouchAndHoldFloat(x, y float64, second ...float64) (err err return } -func (ad *adbDriver) Drag(fromX, fromY, toX, toY int, options ...DataOption) error { +func (ad *adbDriver) Drag(fromX, fromY, toX, toY int, options ...ActionOption) error { return ad.DragFloat(float64(fromX), float64(fromY), float64(toX), float64(toY), options...) } -func (ad *adbDriver) DragFloat(fromX, fromY, toX, toY float64, options ...DataOption) (err error) { +func (ad *adbDriver) DragFloat(fromX, fromY, toX, toY float64, options ...ActionOption) (err error) { err = errDriverNotImplemented return } -func (ad *adbDriver) Swipe(fromX, fromY, toX, toY int, options ...DataOption) error { +func (ad *adbDriver) Swipe(fromX, fromY, toX, toY int, options ...ActionOption) error { return ad.SwipeFloat(float64(fromX), float64(fromY), float64(toX), float64(toY), options...) } -func (ad *adbDriver) SwipeFloat(fromX, fromY, toX, toY float64, options ...DataOption) error { +func (ad *adbDriver) SwipeFloat(fromX, fromY, toX, toY float64, options ...ActionOption) error { // adb shell input swipe fromX fromY toX toY _, err := ad.adbClient.RunShellCommand( "input", "swipe", @@ -263,13 +263,13 @@ func (ad *adbDriver) GetPasteboard(contentType PasteboardType) (raw *bytes.Buffe return } -func (ad *adbDriver) SendKeys(text string, options ...DataOption) (err error) { +func (ad *adbDriver) SendKeys(text string, options ...ActionOption) (err error) { // adb shell input text _, err = ad.adbClient.RunShellCommand("input", "text", text) return } -func (ad *adbDriver) Input(text string, options ...DataOption) (err error) { +func (ad *adbDriver) Input(text string, options ...ActionOption) (err error) { return ad.SendKeys(text, options...) } diff --git a/hrp/pkg/uixt/android_uia2_driver.go b/hrp/pkg/uixt/android_uia2_driver.go index daf9e8bc..5f426e04 100644 --- a/hrp/pkg/uixt/android_uia2_driver.go +++ b/hrp/pkg/uixt/android_uia2_driver.go @@ -174,7 +174,7 @@ func (ud *uiaDriver) WindowSize() (size Size, err error) { } // PressBack simulates a short press on the BACK button. -func (ud *uiaDriver) PressBack(options ...DataOption) (err error) { +func (ud *uiaDriver) PressBack(options ...ActionOption) (err error) { // register(postHandler, new PressBack("/wd/hub/session/:sessionId/back")) _, err = ud.httpPOST(nil, "/session", ud.sessionId, "back") return @@ -199,18 +199,18 @@ func (ud *uiaDriver) PressKeyCode(keyCode KeyCode, metaState KeyMeta, flags ...K return } -func (ud *uiaDriver) Tap(x, y int, options ...DataOption) error { +func (ud *uiaDriver) Tap(x, y int, options ...ActionOption) error { return ud.TapFloat(float64(x), float64(y), options...) } -func (ud *uiaDriver) TapFloat(x, y float64, options ...DataOption) (err error) { +func (ud *uiaDriver) TapFloat(x, y float64, options ...ActionOption) (err error) { // register(postHandler, new Tap("/wd/hub/session/:sessionId/appium/tap")) data := map[string]interface{}{ "x": x, "y": y, } // new data options in post data for extra uiautomator configurations - newData := NewData(data, options...) + newData := mergeDataWithOptions(data, options...) _, err = ud.httpPOST(newData, "/session", ud.sessionId, "appium/tap") return @@ -240,11 +240,11 @@ func (ud *uiaDriver) TouchAndHoldFloat(x, y float64, second ...float64) (err err // the smoothness and speed of the swipe by specifying the number of steps. // Each step execution is throttled to 5 milliseconds per step, so for a 100 // steps, the swipe will take around 0.5 seconds to complete. -func (ud *uiaDriver) Drag(fromX, fromY, toX, toY int, options ...DataOption) error { +func (ud *uiaDriver) Drag(fromX, fromY, toX, toY int, options ...ActionOption) error { return ud.DragFloat(float64(fromX), float64(fromY), float64(toX), float64(toY), options...) } -func (ud *uiaDriver) DragFloat(fromX, fromY, toX, toY float64, options ...DataOption) (err error) { +func (ud *uiaDriver) DragFloat(fromX, fromY, toX, toY float64, options ...ActionOption) (err error) { data := map[string]interface{}{ "startX": fromX, "startY": fromY, @@ -253,7 +253,7 @@ func (ud *uiaDriver) DragFloat(fromX, fromY, toX, toY float64, options ...DataOp } // new data options in post data for extra uiautomator configurations - newData := NewData(data, options...) + newData := mergeDataWithOptions(data, options...) // register(postHandler, new Drag("/wd/hub/session/:sessionId/touch/drag")) _, err = ud.httpPOST(newData, "/session", ud.sessionId, "touch/drag") @@ -264,11 +264,11 @@ func (ud *uiaDriver) DragFloat(fromX, fromY, toX, toY float64, options ...DataOp // to determine smoothness and speed. Each step execution is throttled to 5ms // per step. So for a 100 steps, the swipe will take about 1/2 second to complete. // `steps` is the number of move steps sent to the system -func (ud *uiaDriver) Swipe(fromX, fromY, toX, toY int, options ...DataOption) error { +func (ud *uiaDriver) Swipe(fromX, fromY, toX, toY int, options ...ActionOption) error { return ud.SwipeFloat(float64(fromX), float64(fromY), float64(toX), float64(toY), options...) } -func (ud *uiaDriver) SwipeFloat(fromX, fromY, toX, toY float64, options ...DataOption) error { +func (ud *uiaDriver) SwipeFloat(fromX, fromY, toX, toY float64, options ...ActionOption) error { // register(postHandler, new Swipe("/wd/hub/session/:sessionId/touch/perform")) data := map[string]interface{}{ "startX": fromX, @@ -278,7 +278,7 @@ func (ud *uiaDriver) SwipeFloat(fromX, fromY, toX, toY float64, options ...DataO } // new data options in post data for extra uiautomator configurations - newData := NewData(data, options...) + newData := mergeDataWithOptions(data, options...) _, err := ud.httpPOST(newData, "/session", ud.sessionId, "touch/perform") return err @@ -327,20 +327,20 @@ func (ud *uiaDriver) GetPasteboard(contentType PasteboardType) (raw *bytes.Buffe return } -func (ud *uiaDriver) SendKeys(text string, options ...DataOption) (err error) { +func (ud *uiaDriver) SendKeys(text string, options ...ActionOption) (err error) { // register(postHandler, new SendKeysToElement("/wd/hub/session/:sessionId/keys")) // https://github.com/appium/appium-uiautomator2-server/blob/master/app/src/main/java/io/appium/uiautomator2/handler/SendKeysToElement.java#L76-L85 data := map[string]interface{}{ "text": text, } // new data options in post data for extra uiautomator configurations - newData := NewData(data, options...) + newData := mergeDataWithOptions(data, options...) _, err = ud.httpPOST(newData, "/session", ud.sessionId, "keys") return } -func (ud *uiaDriver) Input(text string, options ...DataOption) (err error) { +func (ud *uiaDriver) Input(text string, options ...ActionOption) (err error) { return ud.SendKeys(text, options...) } diff --git a/hrp/pkg/uixt/drag.go b/hrp/pkg/uixt/drag.go index f583f9cf..3f3dd1f6 100644 --- a/hrp/pkg/uixt/drag.go +++ b/hrp/pkg/uixt/drag.go @@ -24,5 +24,5 @@ func (dExt *DriverExt) DragOffsetFloat(pathname string, toX, toY, xOffset, yOffs // FIXME: handle offset return dExt.Driver.DragFloat(point.X+xOffset, point.Y+yOffset, toX, toY, - WithDataPressDuration(pressForDuration[0])) + WithPressDuration(pressForDuration[0])) } diff --git a/hrp/pkg/uixt/ext.go b/hrp/pkg/uixt/ext.go index 901d2553..40610319 100644 --- a/hrp/pkg/uixt/ext.go +++ b/hrp/pkg/uixt/ext.go @@ -200,7 +200,7 @@ func init() { rand.Seed(time.Now().UnixNano()) } -func (dExt *DriverExt) FindUIRectInUIKit(search string, options ...DataOption) (point PointF, err error) { +func (dExt *DriverExt) FindUIRectInUIKit(search string, options ...ActionOption) (point PointF, err error) { // click on text, using OCR if !isPathExists(search) { return dExt.FindScreenTextByOCR(search, options...) diff --git a/hrp/pkg/uixt/interface.go b/hrp/pkg/uixt/interface.go index d996dfb0..d19ea31b 100644 --- a/hrp/pkg/uixt/interface.go +++ b/hrp/pkg/uixt/interface.go @@ -2,11 +2,8 @@ package uixt import ( "bytes" - "math" "strings" "time" - - "github.com/httprunner/httprunner/v4/hrp/internal/builtin" ) var ( @@ -431,144 +428,6 @@ type Rect struct { Size } -type DataOptions struct { - Data map[string]interface{} // configurations used by ios/android driver - Scope []int // used by ocr to get text position in the scope - Offset []int // used to tap offset of point - Index int // index of the target element, should start from 1 - IgnoreNotFoundError bool // ignore error if target element not found - MaxRetryTimes int // max retry times if target element not found - Interval float64 // interval between retries in seconds -} - -type DataOption func(data *DataOptions) - -func WithCustomOption(key string, value interface{}) DataOption { - return func(data *DataOptions) { - data.Data[key] = value - } -} - -func WithDataPressDuration(duration float64) DataOption { - return func(data *DataOptions) { - data.Data["duration"] = duration - } -} - -func WithDataSteps(steps int) DataOption { - return func(data *DataOptions) { - data.Data["steps"] = steps - } -} - -func WithDataFrequency(frequency int) DataOption { - return func(data *DataOptions) { - data.Data["frequency"] = frequency - } -} - -func WithDataIndex(index int) DataOption { - return func(data *DataOptions) { - data.Index = index - } -} - -func WithDataScope(x1, x2, y1, y2 int) DataOption { - return func(data *DataOptions) { - data.Scope = []int{x1, x2, y1, y2} - } -} - -func WithDataOffset(offsetX, offsetY int) DataOption { - return func(data *DataOptions) { - data.Offset = []int{offsetX, offsetY} - } -} - -func WithDataIdentifier(identifier string) DataOption { - if identifier == "" { - return func(data *DataOptions) {} - } - return func(data *DataOptions) { - data.Data["log"] = map[string]interface{}{ - "enable": true, - "data": identifier, - } - } -} - -func WithDataIgnoreNotFoundError(ignoreError bool) DataOption { - return func(data *DataOptions) { - data.IgnoreNotFoundError = ignoreError - } -} - -func WithDataMaxRetryTimes(maxRetryTimes int) DataOption { - return func(data *DataOptions) { - data.MaxRetryTimes = maxRetryTimes - } -} - -func WithDataWaitTime(sec float64) DataOption { - return func(data *DataOptions) { - data.Interval = sec - } -} - -func NewDataOptions(options ...DataOption) *DataOptions { - dataOptions := &DataOptions{ - Data: make(map[string]interface{}), - } - for _, option := range options { - option(dataOptions) - } - - if len(dataOptions.Scope) == 0 { - dataOptions.Scope = []int{0, 0, math.MaxInt64, math.MaxInt64} // default scope - } - return dataOptions -} - -func NewData(data map[string]interface{}, options ...DataOption) map[string]interface{} { - dataOptions := NewDataOptions(options...) - - // merge with data options - for k, v := range dataOptions.Data { - data[k] = v - } - - // handle point offset - if len(dataOptions.Offset) == 2 { - if x, ok := data["x"]; ok { - xf, _ := builtin.Interface2Float64(x) - data["x"] = xf + float64(dataOptions.Offset[0]) - } - if y, ok := data["y"]; ok { - yf, _ := builtin.Interface2Float64(y) - data["y"] = yf + float64(dataOptions.Offset[1]) - } - } - - // add default options - if _, ok := data["steps"]; !ok { - data["steps"] = 12 // default steps - } - - if _, ok := data["duration"]; !ok { - data["duration"] = 0 // default duration - } - - if _, ok := data["frequency"]; !ok { - data["frequency"] = 60 // default frequency - } - - if _, ok := data["isReplace"]; !ok { - data["isReplace"] = true // default true - } - - return data -} - // current implemeted device: IOSDevice, AndroidDevice type Device interface { UUID() string // ios udid or android serial @@ -640,8 +499,8 @@ type WebDriver interface { StopCamera() error // Tap Sends a tap event at the coordinate. - Tap(x, y int, options ...DataOption) error - TapFloat(x, y float64, options ...DataOption) error + Tap(x, y int, options ...ActionOption) error + TapFloat(x, y float64, options ...ActionOption) error // DoubleTap Sends a double tap event at the coordinate. DoubleTap(x, y int) error @@ -654,12 +513,12 @@ type WebDriver interface { // Drag Initiates a press-and-hold gesture at the coordinate, then drags to another coordinate. // WithPressDurationOption option can be used to set pressForDuration (default to 1 second). - Drag(fromX, fromY, toX, toY int, options ...DataOption) error - DragFloat(fromX, fromY, toX, toY float64, options ...DataOption) error + Drag(fromX, fromY, toX, toY int, options ...ActionOption) error + DragFloat(fromX, fromY, toX, toY float64, options ...ActionOption) error // Swipe works like Drag, but `pressForDuration` value is 0 - Swipe(fromX, fromY, toX, toY int, options ...DataOption) error - SwipeFloat(fromX, fromY, toX, toY float64, options ...DataOption) error + Swipe(fromX, fromY, toX, toY int, options ...ActionOption) error + SwipeFloat(fromX, fromY, toX, toY float64, options ...ActionOption) error // SetPasteboard Sets data to the general pasteboard SetPasteboard(contentType PasteboardType, content string) error @@ -670,16 +529,16 @@ type WebDriver interface { // SendKeys Types a string into active element. There must be element with keyboard focus, // otherwise an error is raised. // WithFrequency option can be used to set frequency of typing (letters per sec). The default value is 60 - SendKeys(text string, options ...DataOption) error + SendKeys(text string, options ...ActionOption) error // Input works like SendKeys - Input(text string, options ...DataOption) error + Input(text string, options ...ActionOption) error // PressButton Presses the corresponding hardware button on the device PressButton(devBtn DeviceButton) error // PressBack Presses the back button - PressBack(options ...DataOption) error + PressBack(options ...ActionOption) error Screenshot() (*bytes.Buffer, error) diff --git a/hrp/pkg/uixt/ios_driver.go b/hrp/pkg/uixt/ios_driver.go index 6acd6a99..5507de6e 100644 --- a/hrp/pkg/uixt/ios_driver.go +++ b/hrp/pkg/uixt/ios_driver.go @@ -377,18 +377,18 @@ func (wd *wdaDriver) GetForegroundApp() (app AppInfo, err error) { return AppInfo{}, nil } -func (wd *wdaDriver) Tap(x, y int, options ...DataOption) error { +func (wd *wdaDriver) Tap(x, y int, options ...ActionOption) error { return wd.TapFloat(float64(x), float64(y), options...) } -func (wd *wdaDriver) TapFloat(x, y float64, options ...DataOption) (err error) { +func (wd *wdaDriver) TapFloat(x, y float64, options ...ActionOption) (err error) { // [[FBRoute POST:@"/wda/tap/:uuid"] respondWithTarget:self action:@selector(handleTap:)] data := map[string]interface{}{ "x": wd.toScale(x), "y": wd.toScale(y), } // new data options in post data for extra WDA configurations - newData := NewData(data, options...) + newData := mergeDataWithOptions(data, options...) _, err = wd.httpPOST(newData, "/session", wd.sessionId, "/wda/tap/0") return @@ -426,11 +426,11 @@ func (wd *wdaDriver) TouchAndHoldFloat(x, y float64, second ...float64) (err err return } -func (wd *wdaDriver) Drag(fromX, fromY, toX, toY int, options ...DataOption) error { +func (wd *wdaDriver) Drag(fromX, fromY, toX, toY int, options ...ActionOption) error { return wd.DragFloat(float64(fromX), float64(fromY), float64(toX), float64(toY), options...) } -func (wd *wdaDriver) DragFloat(fromX, fromY, toX, toY float64, options ...DataOption) (err error) { +func (wd *wdaDriver) DragFloat(fromX, fromY, toX, toY float64, options ...ActionOption) (err error) { // [[FBRoute POST:@"/wda/dragfromtoforduration"] respondWithTarget:self action:@selector(handleDragCoordinate:)] data := map[string]interface{}{ "fromX": wd.toScale(fromX), @@ -440,17 +440,17 @@ func (wd *wdaDriver) DragFloat(fromX, fromY, toX, toY float64, options ...DataOp } // new data options in post data for extra WDA configurations - newData := NewData(data, options...) + newData := mergeDataWithOptions(data, options...) _, err = wd.httpPOST(newData, "/session", wd.sessionId, "/wda/dragfromtoforduration") return } -func (wd *wdaDriver) Swipe(fromX, fromY, toX, toY int, options ...DataOption) error { +func (wd *wdaDriver) Swipe(fromX, fromY, toX, toY int, options ...ActionOption) error { return wd.SwipeFloat(float64(fromX), float64(fromY), float64(toX), float64(toY), options...) } -func (wd *wdaDriver) SwipeFloat(fromX, fromY, toX, toY float64, options ...DataOption) error { +func (wd *wdaDriver) SwipeFloat(fromX, fromY, toX, toY float64, options ...ActionOption) error { return wd.DragFloat(fromX, fromY, toX, toY, options...) } @@ -477,23 +477,23 @@ func (wd *wdaDriver) GetPasteboard(contentType PasteboardType) (raw *bytes.Buffe return } -func (wd *wdaDriver) SendKeys(text string, options ...DataOption) (err error) { +func (wd *wdaDriver) SendKeys(text string, options ...ActionOption) (err error) { // [[FBRoute POST:@"/wda/keys"] respondWithTarget:self action:@selector(handleKeys:)] data := map[string]interface{}{"value": strings.Split(text, "")} // new data options in post data for extra WDA configurations - newData := NewData(data, options...) + newData := mergeDataWithOptions(data, options...) _, err = wd.httpPOST(newData, "/session", wd.sessionId, "/wda/keys") return } -func (wd *wdaDriver) Input(text string, options ...DataOption) (err error) { +func (wd *wdaDriver) Input(text string, options ...ActionOption) (err error) { return wd.SendKeys(text, options...) } // PressBack simulates a short press on the BACK button. -func (wd *wdaDriver) PressBack(options ...DataOption) (err error) { +func (wd *wdaDriver) PressBack(options ...ActionOption) (err error) { windowSize, err := wd.WindowSize() if err != nil { return @@ -507,7 +507,7 @@ func (wd *wdaDriver) PressBack(options ...DataOption) (err error) { } // new data options in post data for extra WDA configurations - newData := NewData(data, options...) + newData := mergeDataWithOptions(data, options...) _, err = wd.httpPOST(newData, "/session", wd.sessionId, "/wda/dragfromtoforduration") return diff --git a/hrp/pkg/uixt/ocr_vedem.go b/hrp/pkg/uixt/ocr_vedem.go index 939ffcbd..b88cb85f 100644 --- a/hrp/pkg/uixt/ocr_vedem.go +++ b/hrp/pkg/uixt/ocr_vedem.go @@ -48,10 +48,10 @@ func (t OCRTexts) texts() (texts []string) { return texts } -func (t OCRTexts) FindText(text string, options ...DataOption) ( +func (t OCRTexts) FindText(text string, options ...ActionOption) ( point PointF, err error) { - dataOptions := NewDataOptions(options...) + actionOptions := NewActionOptions(options...) var rects []image.Rectangle for _, ocrText := range t { @@ -63,8 +63,8 @@ func (t OCRTexts) FindText(text string, options ...DataOption) ( } // check if text in scope - if rect.Min.X < dataOptions.Scope[0] || rect.Max.X > dataOptions.Scope[2] || - rect.Min.Y < dataOptions.Scope[1] || rect.Max.Y > dataOptions.Scope[3] { + if rect.Min.X < actionOptions.AbsScope[0] || rect.Min.Y < actionOptions.AbsScope[1] || + rect.Max.X > actionOptions.AbsScope[2] || rect.Max.Y > actionOptions.AbsScope[3] { // not in scope continue } @@ -77,7 +77,7 @@ func (t OCRTexts) FindText(text string, options ...DataOption) ( } // match exactly, and not specify index, return the first one - if dataOptions.Index == 0 { + if actionOptions.Index == 0 { return getRectangleCenterPoint(rect), nil } } @@ -88,7 +88,7 @@ func (t OCRTexts) FindText(text string, options ...DataOption) ( } // get index - idx := dataOptions.Index + idx := actionOptions.Index if idx > 0 { // NOTICE: index start from 1 idx = idx - 1 @@ -105,7 +105,7 @@ func (t OCRTexts) FindText(text string, options ...DataOption) ( return getRectangleCenterPoint(rects[idx]), nil } -func (t OCRTexts) FindTexts(texts []string, options ...DataOption) (points []PointF, err error) { +func (t OCRTexts) FindTexts(texts []string, options ...ActionOption) (points []PointF, err error) { for _, text := range texts { point, err := t.FindText(text, options...) if err != nil { @@ -296,7 +296,7 @@ func (dExt *DriverExt) GetScreenTextsByOCR() (texts OCRTexts, err error) { return ocrTexts, nil } -func (dExt *DriverExt) FindScreenTextByOCR(text string, options ...DataOption) (point PointF, err error) { +func (dExt *DriverExt) FindScreenTextByOCR(text string, options ...ActionOption) (point PointF, err error) { ocrTexts, err := dExt.GetScreenTextsByOCR() if err != nil { return diff --git a/hrp/pkg/uixt/opencv.go b/hrp/pkg/uixt/opencv.go index d3888128..e72f0cde 100644 --- a/hrp/pkg/uixt/opencv.go +++ b/hrp/pkg/uixt/opencv.go @@ -111,7 +111,7 @@ func (dExt *DriverExt) FindAllImageRect(search string) (rects []image.Rectangle, return } -func (dExt *DriverExt) FindImageRectInUIKit(imagePath string, options ...DataOption) (point PointF, err error) { +func (dExt *DriverExt) FindImageRectInUIKit(imagePath string, options ...ActionOption) (point PointF, err error) { var bufSource, bufSearch *bytes.Buffer if bufSearch, err = getBufFromDisk(imagePath); err != nil { return PointF{}, err diff --git a/hrp/pkg/uixt/opencv_off.go b/hrp/pkg/uixt/opencv_off.go index 394f9666..981d97ea 100644 --- a/hrp/pkg/uixt/opencv_off.go +++ b/hrp/pkg/uixt/opencv_off.go @@ -17,7 +17,7 @@ func (dExt *DriverExt) FindAllImageRect(search string) (rects []image.Rectangle, return } -func (dExt *DriverExt) FindImageRectInUIKit(imagePath string, options ...DataOption) (point PointF, err error) { +func (dExt *DriverExt) FindImageRectInUIKit(imagePath string, options ...ActionOption) (point PointF, err error) { log.Fatal().Msg("opencv is not supported") return } diff --git a/hrp/pkg/uixt/swipe.go b/hrp/pkg/uixt/swipe.go index 2636f86d..a654a6c8 100644 --- a/hrp/pkg/uixt/swipe.go +++ b/hrp/pkg/uixt/swipe.go @@ -7,7 +7,6 @@ import ( "github.com/pkg/errors" "github.com/rs/zerolog/log" - "github.com/httprunner/httprunner/v4/hrp/internal/builtin" "github.com/httprunner/httprunner/v4/hrp/internal/code" ) @@ -16,7 +15,7 @@ func assertRelative(p float64) bool { } // SwipeRelative swipe from relative position [fromX, fromY] to relative position [toX, toY] -func (dExt *DriverExt) SwipeRelative(fromX, fromY, toX, toY float64, options ...DataOption) error { +func (dExt *DriverExt) SwipeRelative(fromX, fromY, toX, toY float64, options ...ActionOption) error { width := dExt.windowSize.Width height := dExt.windowSize.Height @@ -34,7 +33,7 @@ func (dExt *DriverExt) SwipeRelative(fromX, fromY, toX, toY float64, options ... return dExt.Driver.SwipeFloat(fromX, fromY, toX, toY, options...) } -func (dExt *DriverExt) SwipeTo(direction string, options ...DataOption) (err error) { +func (dExt *DriverExt) SwipeTo(direction string, options ...ActionOption) (err error) { switch direction { case "up": return dExt.SwipeUp(options...) @@ -48,28 +47,28 @@ func (dExt *DriverExt) SwipeTo(direction string, options ...DataOption) (err err return fmt.Errorf("unexpected direction: %s", direction) } -func (dExt *DriverExt) SwipeUp(options ...DataOption) (err error) { +func (dExt *DriverExt) SwipeUp(options ...ActionOption) (err error) { return dExt.SwipeRelative(0.5, 0.5, 0.5, 0.1, options...) } -func (dExt *DriverExt) SwipeDown(options ...DataOption) (err error) { +func (dExt *DriverExt) SwipeDown(options ...ActionOption) (err error) { return dExt.SwipeRelative(0.5, 0.5, 0.5, 0.9, options...) } -func (dExt *DriverExt) SwipeLeft(options ...DataOption) (err error) { +func (dExt *DriverExt) SwipeLeft(options ...ActionOption) (err error) { return dExt.SwipeRelative(0.5, 0.5, 0.1, 0.5, options...) } -func (dExt *DriverExt) SwipeRight(options ...DataOption) (err error) { +func (dExt *DriverExt) SwipeRight(options ...ActionOption) (err error) { return dExt.SwipeRelative(0.5, 0.5, 0.9, 0.5, options...) } type Action func(driver *DriverExt) error -func (dExt *DriverExt) LoopUntil(findAction, findCondition, foundAction Action, options ...DataOption) error { - dataOptions := NewDataOptions(options...) - maxRetryTimes := dataOptions.MaxRetryTimes - interval := dataOptions.Interval +func (dExt *DriverExt) LoopUntil(findAction, findCondition, foundAction Action, options ...ActionOption) error { + actionOptions := NewActionOptions(options...) + maxRetryTimes := actionOptions.MaxRetryTimes + interval := actionOptions.Interval for i := 0; i < maxRetryTimes; i++ { if err := findCondition(dExt); err == nil { @@ -89,56 +88,38 @@ func (dExt *DriverExt) LoopUntil(findAction, findCondition, foundAction Action, fmt.Sprintf("loop %d times, match find condition failed", maxRetryTimes)) } -func (dExt *DriverExt) prepareSwipeAction(action MobileAction) func(d *DriverExt) error { - identifierOption := WithDataIdentifier(action.Identifier) - durationOption := WithDataPressDuration(action.Duration) - - if action.Steps == 0 { - action.Steps = 10 - } - stepsOption := WithDataSteps(action.Steps) - - dataOptions := make([]DataOption, 3) - dataOptions = append(dataOptions, identifierOption, durationOption, stepsOption) - +func (dExt *DriverExt) prepareSwipeAction(options ...ActionOption) func(d *DriverExt) error { + actionOptions := NewActionOptions(options...) var swipeDirection interface{} - if action.Direction != nil { - swipeDirection = action.Direction + if actionOptions.Direction != nil { + swipeDirection = actionOptions.Direction } else { swipeDirection = "up" // default swipe up } + if actionOptions.Steps == 0 { + actionOptions.Steps = 10 + } + return func(d *DriverExt) error { defer func() { // wait for swipe action to completed and content to load completely - time.Sleep(time.Duration(1000*action.WaitTime) * time.Millisecond) + time.Sleep(time.Duration(1000*actionOptions.Interval) * time.Millisecond) }() if d, ok := swipeDirection.(string); ok { // enum direction: up, down, left, right - if err := dExt.SwipeTo(d, dataOptions...); err != nil { + if err := dExt.SwipeTo(d, options...); err != nil { log.Error().Err(err).Msgf("swipe %s failed", d) return err } } else if d, ok := swipeDirection.([]float64); ok { // custom direction: [fromX, fromY, toX, toY] - if err := dExt.SwipeRelative(d[0], d[1], d[2], d[3], dataOptions...); err != nil { + if err := dExt.SwipeRelative(d[0], d[1], d[2], d[3], options...); err != nil { log.Error().Err(err).Msgf("swipe from (%v, %v) to (%v, %v) failed", d[0], d[1], d[2], d[3]) return err } - } else if d, ok := swipeDirection.([]interface{}); ok { - // loaded from json case - // custom direction: [fromX, fromY, toX, toY] - sx, _ := builtin.Interface2Float64(d[0]) - sy, _ := builtin.Interface2Float64(d[1]) - ex, _ := builtin.Interface2Float64(d[2]) - ey, _ := builtin.Interface2Float64(d[3]) - if err := dExt.SwipeRelative(sx, sy, ex, ey, dataOptions...); err != nil { - log.Error().Err(err).Msgf("swipe from (%v, %v) to (%v, %v) failed", - sx, sy, ex, ey) - return err - } } else { return fmt.Errorf("invalid swipe params %v", swipeDirection) } @@ -146,28 +127,13 @@ func (dExt *DriverExt) prepareSwipeAction(action MobileAction) func(d *DriverExt } } -func (dExt *DriverExt) swipeToTapTexts(texts []string, action MobileAction) error { +func (dExt *DriverExt) swipeToTapTexts(texts []string, options ...ActionOption) error { if len(texts) == 0 { return errors.New("no text to tap") } - if len(action.Scope) != 4 { - action.Scope = []float64{0, 0, 1, 1} - } - if len(action.Offset) != 2 { - action.Offset = []int{0, 0} - } - - identifierOption := WithDataIdentifier(action.Identifier) - offsetOption := WithDataOffset(action.Offset[0], action.Offset[1]) - indexOption := WithDataIndex(action.Index) - scopeOption := WithDataScope(dExt.getAbsScope(action.Scope[0], action.Scope[1], action.Scope[2], action.Scope[3])) // default to retry 10 times - if action.MaxRetryTimes == 0 { - action.MaxRetryTimes = 10 - } - maxRetryOption := WithDataMaxRetryTimes(action.MaxRetryTimes) - waitTimeOption := WithDataWaitTime(action.WaitTime) + options = append(options, WithMaxRetryTimes(10)) var point PointF findTexts := func(d *DriverExt) error { @@ -176,7 +142,7 @@ func (dExt *DriverExt) swipeToTapTexts(texts []string, action MobileAction) erro if err != nil { return err } - points, err := ocrTexts.FindTexts(texts, indexOption, scopeOption) + points, err := ocrTexts.FindTexts(texts, options...) if err != nil { return err } @@ -185,14 +151,14 @@ func (dExt *DriverExt) swipeToTapTexts(texts []string, action MobileAction) erro } foundTextAction := func(d *DriverExt) error { // tap text - return d.TapAbsXY(point.X, point.Y, identifierOption, offsetOption) + return d.TapAbsXY(point.X, point.Y, options...) } - findAction := dExt.prepareSwipeAction(action) - return dExt.LoopUntil(findAction, findTexts, foundTextAction, maxRetryOption, waitTimeOption) + findAction := dExt.prepareSwipeAction(options...) + return dExt.LoopUntil(findAction, findTexts, foundTextAction, options...) } -func (dExt *DriverExt) swipeToTapApp(appName string, action MobileAction) error { +func (dExt *DriverExt) swipeToTapApp(appName string, options ...ActionOption) error { // go to home screen if err := dExt.Driver.Homescreen(); err != nil { return errors.Wrap(err, "go to home screen failed") @@ -203,8 +169,8 @@ func (dExt *DriverExt) swipeToTapApp(appName string, action MobileAction) error dExt.SwipeRight() } - action.Offset = []int{0, -25} // tap app icon above the text - action.Direction = "left" + options = append(options, WithOffset(0, -25)) // tap app icon above the text + options = append(options, WithDirection("left")) - return dExt.swipeToTapTexts([]string{appName}, action) + return dExt.swipeToTapTexts([]string{appName}, options...) } diff --git a/hrp/pkg/uixt/swipe_test.go b/hrp/pkg/uixt/swipe_test.go index 32cb3033..e40c41c5 100644 --- a/hrp/pkg/uixt/swipe_test.go +++ b/hrp/pkg/uixt/swipe_test.go @@ -9,21 +9,11 @@ import ( func TestAndroidSwipeAction(t *testing.T) { setupAndroid(t) - action := MobileAction{ - Method: ACTION_Swipe, - Params: "up", - } - swipeAction := driverExt.prepareSwipeAction(action) - + swipeAction := driverExt.prepareSwipeAction(WithDirection("up")) err := swipeAction(driverExt) checkErr(t, err) - action = MobileAction{ - Method: ACTION_Swipe, - Params: []float64{0.5, 0.5, 0.5, 0.9}, - } - swipeAction = driverExt.prepareSwipeAction(action) - + swipeAction = driverExt.prepareSwipeAction(WithCustomDirection(0.5, 0.5, 0.5, 0.9)) err = swipeAction(driverExt) checkErr(t, err) } @@ -31,7 +21,7 @@ func TestAndroidSwipeAction(t *testing.T) { func TestAndroidSwipeToTapApp(t *testing.T) { setupAndroid(t) - err := driverExt.swipeToTapApp("抖音", MobileAction{}) + err := driverExt.swipeToTapApp("抖音") checkErr(t, err) } @@ -41,9 +31,6 @@ func TestAndroidSwipeToTapTexts(t *testing.T) { err := driverExt.Driver.AppLaunch("com.ss.android.ugc.aweme") checkErr(t, err) - action := MobileAction{ - Params: "up", - } - err = driverExt.swipeToTapTexts([]string{"点击进入直播间", "直播中"}, action) + err = driverExt.swipeToTapTexts([]string{"点击进入直播间", "直播中"}, WithDirection("up")) checkErr(t, err) } diff --git a/hrp/pkg/uixt/tap.go b/hrp/pkg/uixt/tap.go index d5d80cbe..b4707839 100644 --- a/hrp/pkg/uixt/tap.go +++ b/hrp/pkg/uixt/tap.go @@ -4,12 +4,12 @@ import ( "fmt" ) -func (dExt *DriverExt) TapAbsXY(x, y float64, options ...DataOption) error { +func (dExt *DriverExt) TapAbsXY(x, y float64, options ...ActionOption) error { // tap on absolute coordinate [x, y] return dExt.Driver.TapFloat(x, y, options...) } -func (dExt *DriverExt) TapXY(x, y float64, options ...DataOption) error { +func (dExt *DriverExt) TapXY(x, y float64, options ...ActionOption) error { // tap on [x, y] percent of window size if x > 1 || y > 1 { return fmt.Errorf("x, y percentage should be < 1, got x=%v, y=%v", x, y) @@ -21,12 +21,12 @@ func (dExt *DriverExt) TapXY(x, y float64, options ...DataOption) error { return dExt.TapAbsXY(x, y, options...) } -func (dExt *DriverExt) TapByOCR(ocrText string, options ...DataOption) error { - dataOptions := NewDataOptions(options...) +func (dExt *DriverExt) TapByOCR(ocrText string, options ...ActionOption) error { + actionOptions := NewActionOptions(options...) point, err := dExt.FindScreenTextByOCR(ocrText, options...) if err != nil { - if dataOptions.IgnoreNotFoundError { + if actionOptions.IgnoreNotFoundError { return nil } return err @@ -35,12 +35,12 @@ func (dExt *DriverExt) TapByOCR(ocrText string, options ...DataOption) error { return dExt.TapAbsXY(point.X, point.Y, options...) } -func (dExt *DriverExt) TapByCV(imagePath string, options ...DataOption) error { - dataOptions := NewDataOptions(options...) +func (dExt *DriverExt) TapByCV(imagePath string, options ...ActionOption) error { + actionOptions := NewActionOptions(options...) point, err := dExt.FindImageRectInUIKit(imagePath, options...) if err != nil { - if dataOptions.IgnoreNotFoundError { + if actionOptions.IgnoreNotFoundError { return nil } return err @@ -49,16 +49,16 @@ func (dExt *DriverExt) TapByCV(imagePath string, options ...DataOption) error { return dExt.TapAbsXY(point.X, point.Y, options...) } -func (dExt *DriverExt) Tap(param string, options ...DataOption) error { +func (dExt *DriverExt) Tap(param string, options ...ActionOption) error { return dExt.TapOffset(param, 0.5, 0.5, options...) } -func (dExt *DriverExt) TapOffset(param string, xOffset, yOffset float64, options ...DataOption) (err error) { - dataOptions := NewDataOptions(options...) +func (dExt *DriverExt) TapOffset(param string, xOffset, yOffset float64, options ...ActionOption) (err error) { + actionOptions := NewActionOptions(options...) point, err := dExt.FindUIRectInUIKit(param, options...) if err != nil { - if dataOptions.IgnoreNotFoundError { + if actionOptions.IgnoreNotFoundError { return nil } return err diff --git a/hrp/step_mobile_ui.go b/hrp/step_mobile_ui.go index 2a4f1f6a..a36afe09 100644 --- a/hrp/step_mobile_ui.go +++ b/hrp/step_mobile_ui.go @@ -69,12 +69,11 @@ func (s *StepMobile) Home() *StepMobile { // TapXY taps the point {X,Y}, X & Y is percentage of coordinates func (s *StepMobile) TapXY(x, y float64, options ...uixt.ActionOption) *StepMobile { action := uixt.MobileAction{ - Method: uixt.ACTION_TapXY, - Params: []float64{x, y}, - } - for _, option := range options { - option(&action) + Method: uixt.ACTION_TapXY, + Params: []float64{x, y}, + Options: uixt.NewActionOptions(options...), } + s.mobileStep().Actions = append(s.mobileStep().Actions, action) return &StepMobile{step: s.step} } @@ -82,12 +81,11 @@ func (s *StepMobile) TapXY(x, y float64, options ...uixt.ActionOption) *StepMobi // TapAbsXY taps the point {X,Y}, X & Y is absolute coordinates func (s *StepMobile) TapAbsXY(x, y float64, options ...uixt.ActionOption) *StepMobile { action := uixt.MobileAction{ - Method: uixt.ACTION_TapAbsXY, - Params: []float64{x, y}, - } - for _, option := range options { - option(&action) + Method: uixt.ACTION_TapAbsXY, + Params: []float64{x, y}, + Options: uixt.NewActionOptions(options...), } + s.mobileStep().Actions = append(s.mobileStep().Actions, action) return &StepMobile{step: s.step} } @@ -95,12 +93,11 @@ func (s *StepMobile) TapAbsXY(x, y float64, options ...uixt.ActionOption) *StepM // Tap taps on the target element func (s *StepMobile) Tap(params string, options ...uixt.ActionOption) *StepMobile { action := uixt.MobileAction{ - Method: uixt.ACTION_Tap, - Params: params, - } - for _, option := range options { - option(&action) + Method: uixt.ACTION_Tap, + Params: params, + Options: uixt.NewActionOptions(options...), } + s.mobileStep().Actions = append(s.mobileStep().Actions, action) return &StepMobile{step: s.step} } @@ -108,12 +105,11 @@ func (s *StepMobile) Tap(params string, options ...uixt.ActionOption) *StepMobil // Tap taps on the target element by OCR recognition func (s *StepMobile) TapByOCR(ocrText string, options ...uixt.ActionOption) *StepMobile { action := uixt.MobileAction{ - Method: uixt.ACTION_TapByOCR, - Params: ocrText, - } - for _, option := range options { - option(&action) + Method: uixt.ACTION_TapByOCR, + Params: ocrText, + Options: uixt.NewActionOptions(options...), } + s.mobileStep().Actions = append(s.mobileStep().Actions, action) return &StepMobile{step: s.step} } @@ -121,153 +117,142 @@ func (s *StepMobile) TapByOCR(ocrText string, options ...uixt.ActionOption) *Ste // Tap taps on the target element by CV recognition func (s *StepMobile) TapByCV(imagePath string, options ...uixt.ActionOption) *StepMobile { action := uixt.MobileAction{ - Method: uixt.ACTION_TapByCV, - Params: imagePath, - } - for _, option := range options { - option(&action) + Method: uixt.ACTION_TapByCV, + Params: imagePath, + Options: uixt.NewActionOptions(options...), } + s.mobileStep().Actions = append(s.mobileStep().Actions, action) return &StepMobile{step: s.step} } // DoubleTapXY double taps the point {X,Y}, X & Y is percentage of coordinates -func (s *StepMobile) DoubleTapXY(x, y float64) *StepMobile { +func (s *StepMobile) DoubleTapXY(x, y float64, options ...uixt.ActionOption) *StepMobile { s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{ - Method: uixt.ACTION_DoubleTapXY, - Params: []float64{x, y}, + Method: uixt.ACTION_DoubleTapXY, + Params: []float64{x, y}, + Options: uixt.NewActionOptions(options...), }) return &StepMobile{step: s.step} } func (s *StepMobile) DoubleTap(params string, options ...uixt.ActionOption) *StepMobile { action := uixt.MobileAction{ - Method: uixt.ACTION_DoubleTap, - Params: params, - } - for _, option := range options { - option(&action) + Method: uixt.ACTION_DoubleTap, + Params: params, + Options: uixt.NewActionOptions(options...), } + s.mobileStep().Actions = append(s.mobileStep().Actions, action) return &StepMobile{step: s.step} } func (s *StepMobile) Back(options ...uixt.ActionOption) *StepMobile { action := uixt.MobileAction{ - Method: uixt.ACTION_Back, - Params: nil, - } - for _, option := range options { - option(&action) + Method: uixt.ACTION_Back, + Params: nil, + Options: uixt.NewActionOptions(options...), } + s.mobileStep().Actions = append(s.mobileStep().Actions, action) return &StepMobile{step: s.step} } func (s *StepMobile) Swipe(sx, sy, ex, ey float64, options ...uixt.ActionOption) *StepMobile { action := uixt.MobileAction{ - Method: uixt.ACTION_Swipe, - Params: []float64{sx, sy, ex, ey}, - } - for _, option := range options { - option(&action) + Method: uixt.ACTION_Swipe, + Params: []float64{sx, sy, ex, ey}, + Options: uixt.NewActionOptions(options...), } + s.mobileStep().Actions = append(s.mobileStep().Actions, action) return &StepMobile{step: s.step} } func (s *StepMobile) SwipeUp(options ...uixt.ActionOption) *StepMobile { action := uixt.MobileAction{ - Method: uixt.ACTION_Swipe, - Params: "up", - } - for _, option := range options { - option(&action) + Method: uixt.ACTION_Swipe, + Params: "up", + Options: uixt.NewActionOptions(options...), } + s.mobileStep().Actions = append(s.mobileStep().Actions, action) return &StepMobile{step: s.step} } func (s *StepMobile) SwipeDown(options ...uixt.ActionOption) *StepMobile { action := uixt.MobileAction{ - Method: uixt.ACTION_Swipe, - Params: "down", - } - for _, option := range options { - option(&action) + Method: uixt.ACTION_Swipe, + Params: "down", + Options: uixt.NewActionOptions(options...), } + s.mobileStep().Actions = append(s.mobileStep().Actions, action) return &StepMobile{step: s.step} } func (s *StepMobile) SwipeLeft(options ...uixt.ActionOption) *StepMobile { action := uixt.MobileAction{ - Method: uixt.ACTION_Swipe, - Params: "left", - } - for _, option := range options { - option(&action) + Method: uixt.ACTION_Swipe, + Params: "left", + Options: uixt.NewActionOptions(options...), } + s.mobileStep().Actions = append(s.mobileStep().Actions, action) return &StepMobile{step: s.step} } func (s *StepMobile) SwipeRight(options ...uixt.ActionOption) *StepMobile { action := uixt.MobileAction{ - Method: uixt.ACTION_Swipe, - Params: "right", - } - for _, option := range options { - option(&action) + Method: uixt.ACTION_Swipe, + Params: "right", + Options: uixt.NewActionOptions(options...), } + s.mobileStep().Actions = append(s.mobileStep().Actions, action) return &StepMobile{step: s.step} } func (s *StepMobile) SwipeToTapApp(appName string, options ...uixt.ActionOption) *StepMobile { action := uixt.MobileAction{ - Method: uixt.ACTION_SwipeToTapApp, - Params: appName, - } - for _, option := range options { - option(&action) + Method: uixt.ACTION_SwipeToTapApp, + Params: appName, + Options: uixt.NewActionOptions(options...), } + s.mobileStep().Actions = append(s.mobileStep().Actions, action) return &StepMobile{step: s.step} } func (s *StepMobile) SwipeToTapText(text string, options ...uixt.ActionOption) *StepMobile { action := uixt.MobileAction{ - Method: uixt.ACTION_SwipeToTapText, - Params: text, - } - for _, option := range options { - option(&action) + Method: uixt.ACTION_SwipeToTapText, + Params: text, + Options: uixt.NewActionOptions(options...), } + s.mobileStep().Actions = append(s.mobileStep().Actions, action) return &StepMobile{step: s.step} } func (s *StepMobile) SwipeToTapTexts(texts interface{}, options ...uixt.ActionOption) *StepMobile { action := uixt.MobileAction{ - Method: uixt.ACTION_SwipeToTapTexts, - Params: texts, - } - for _, option := range options { - option(&action) + Method: uixt.ACTION_SwipeToTapTexts, + Params: texts, + Options: uixt.NewActionOptions(options...), } + s.mobileStep().Actions = append(s.mobileStep().Actions, action) return &StepMobile{step: s.step} } func (s *StepMobile) Input(text string, options ...uixt.ActionOption) *StepMobile { action := uixt.MobileAction{ - Method: uixt.ACTION_Input, - Params: text, - } - for _, option := range options { - option(&action) + Method: uixt.ACTION_Input, + Params: text, + Options: uixt.NewActionOptions(options...), } + s.mobileStep().Actions = append(s.mobileStep().Actions, action) return &StepMobile{step: s.step} }