change: update function optional parameters to DataOptions

This commit is contained in:
xucong.053
2022-10-16 23:31:13 +08:00
parent 1ed4fcd1e0
commit 2df2792fe4
13 changed files with 149 additions and 150 deletions

View File

@@ -307,13 +307,13 @@ func (dExt *DriverExt) FindUIElement(param string) (ele WebElement, err error) {
return dExt.Driver.FindElement(selector)
}
func (dExt *DriverExt) FindUIRectInUIKit(search string, index ...int) (x, y, width, height float64, err error) {
func (dExt *DriverExt) FindUIRectInUIKit(search string, options ...DataOption) (x, y, width, height float64, err error) {
// click on text, using OCR
if !isPathExists(search) {
return dExt.FindTextByOCR(search, WithCustomOption("index", index))
return dExt.FindTextByOCR(search, options...)
}
// click on image, using opencv
return dExt.FindImageRectInUIKit(search, index...)
return dExt.FindImageRectInUIKit(search, options...)
}
func (dExt *DriverExt) MappingToRectInUIKit(rect image.Rectangle) (x, y, width, height float64) {
@@ -383,26 +383,19 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
action.Scope = []float64{0, 0, 1, 1}
}
var options []DataOption
options = append(options,
WithCustomOption("index", []int{action.Index}),
WithCustomOption("scope", []int{
int(action.Scope[0] * float64(dExt.windowSize.Width) * dExt.scale),
int(action.Scope[1] * float64(dExt.windowSize.Height) * dExt.scale),
int(action.Scope[2] * float64(dExt.windowSize.Width) * dExt.scale),
int(action.Scope[3] * float64(dExt.windowSize.Height) * dExt.scale),
}),
)
identifierOption := WithIdentifierOption(action.Identifier)
indexOption := WithIndexOption(action.Index)
scopeOption := WithScopeOption(dExt.GetAbsScope(action.Scope[0], action.Scope[1], action.Scope[2], action.Scope[3]))
var point PointF
findApp := func(d *DriverExt) error {
var err error
point, err = d.GetTextXY(appName, options...)
point, err = d.GetTextXY(appName, scopeOption, indexOption)
return err
}
foundAppAction := func(d *DriverExt) error {
// click app to launch
return d.TapAbsXY(point.X, point.Y-25, action.Identifier)
return d.TapAbsXY(point.X, point.Y-25, identifierOption)
}
// go to home screen
@@ -430,26 +423,19 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
action.Scope = []float64{0, 0, 1, 1}
}
var options []DataOption
options = append(options,
WithCustomOption("index", []int{action.Index}),
WithCustomOption("scope", []int{
int(action.Scope[0] * float64(dExt.windowSize.Width) * dExt.scale),
int(action.Scope[1] * float64(dExt.windowSize.Height) * dExt.scale),
int(action.Scope[2] * float64(dExt.windowSize.Width) * dExt.scale),
int(action.Scope[3] * float64(dExt.windowSize.Height) * dExt.scale),
}),
)
identifierOption := WithIdentifierOption(action.Identifier)
indexOption := WithIndexOption(action.Index)
scopeOption := WithScopeOption(dExt.GetAbsScope(action.Scope[0], action.Scope[1], action.Scope[2], action.Scope[3]))
var point PointF
findText := func(d *DriverExt) error {
var err error
point, err = d.GetTextXY(text, options...)
point, err = d.GetTextXY(text, indexOption, scopeOption)
return err
}
foundTextAction := func(d *DriverExt) error {
// tap text
return d.TapAbsXY(point.X, point.Y, action.Identifier)
return d.TapAbsXY(point.X, point.Y, identifierOption)
}
// default to retry 10 times
@@ -478,20 +464,12 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
action.Scope = []float64{0, 0, 1, 1}
}
var options []DataOption
options = append(options,
WithCustomOption("scope", []int{
int(action.Scope[0] * float64(dExt.windowSize.Width) * dExt.scale),
int(action.Scope[1] * float64(dExt.windowSize.Height) * dExt.scale),
int(action.Scope[2] * float64(dExt.windowSize.Width) * dExt.scale),
int(action.Scope[3] * float64(dExt.windowSize.Height) * dExt.scale),
}),
)
scopeOption := WithScopeOption(dExt.GetAbsScope(action.Scope[0], action.Scope[1], action.Scope[2], action.Scope[3]))
var point PointF
findText := func(d *DriverExt) error {
var err error
points, err := d.GetTextXYs(texts, options...)
points, err := d.GetTextXYs(texts, scopeOption)
if err != nil {
return err
}
@@ -504,7 +482,7 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
}
foundTextAction := func(d *DriverExt) error {
// tap text
return d.TapAbsXY(point.X, point.Y, action.Identifier)
return d.TapAbsXY(point.X, point.Y, WithIdentifierOption(action.Identifier))
}
// default to retry 10 times
@@ -542,7 +520,7 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
}
x, _ := location[0].(float64)
y, _ := location[1].(float64)
return dExt.TapXY(x, y, action.Identifier)
return dExt.TapXY(x, y, WithIdentifierOption(action.Identifier))
}
return fmt.Errorf("invalid %s params: %v", ACTION_TapXY, action.Params)
case ACTION_TapAbsXY:
@@ -553,12 +531,12 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
}
x, _ := location[0].(float64)
y, _ := location[1].(float64)
return dExt.TapAbsXY(x, y, action.Identifier)
return dExt.TapAbsXY(x, y, WithIdentifierOption(action.Identifier))
}
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, action.Identifier, action.IgnoreNotFoundError, action.Index)
return dExt.Tap(param, WithIdentifierOption(action.Identifier), WithIgnoreNotFoundErrorOption(true), WithIndexOption(action.Index))
}
return fmt.Errorf("invalid %s params: %v", ACTION_Tap, action.Params)
case ACTION_TapByOCR:
@@ -567,22 +545,16 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
action.Scope = []float64{0, 0, 1, 1}
}
var options []DataOption
options = append(options,
WithCustomOption("index", []int{action.Index}),
WithCustomOption("scope", []int{
int(action.Scope[0] * float64(dExt.windowSize.Width) * dExt.scale),
int(action.Scope[1] * float64(dExt.windowSize.Height) * dExt.scale),
int(action.Scope[2] * float64(dExt.windowSize.Width) * dExt.scale),
int(action.Scope[3] * float64(dExt.windowSize.Height) * dExt.scale),
}),
)
return dExt.TapByOCR(ocrText, action.Identifier, action.IgnoreNotFoundError, options...)
indexOption := WithIndexOption(action.Index)
scopeOption := WithScopeOption(dExt.GetAbsScope(action.Scope[0], action.Scope[1], action.Scope[2], action.Scope[3]))
identifierOption := WithIdentifierOption(action.Identifier)
IgnoreNotFoundErrorOption := WithIgnoreNotFoundErrorOption(action.IgnoreNotFoundError)
return dExt.TapByOCR(ocrText, identifierOption, IgnoreNotFoundErrorOption, indexOption, scopeOption)
}
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, action.Identifier, action.IgnoreNotFoundError, action.Index)
return dExt.TapByCV(imagePath, WithIdentifierOption(action.Identifier), WithIgnoreNotFoundErrorOption(true), WithIndexOption(action.Index))
}
return fmt.Errorf("invalid %s params: %v", ACTION_TapByCV, action.Params)
case ACTION_DoubleTapXY:
@@ -602,6 +574,7 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
}
return fmt.Errorf("invalid %s params: %v", ACTION_DoubleTap, action.Params)
case ACTION_Swipe:
identifierOption := WithIdentifierOption(action.Identifier)
if positions, ok := action.Params.([]interface{}); ok {
// relative fromX, fromY, toX, toY of window size: [0.5, 0.9, 0.5, 0.1]
if len(positions) != 4 {
@@ -611,10 +584,10 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
fromY, _ := positions[1].(float64)
toX, _ := positions[2].(float64)
toY, _ := positions[3].(float64)
return dExt.SwipeRelative(fromX, fromY, toX, toY, action.Identifier)
return dExt.SwipeRelative(fromX, fromY, toX, toY, identifierOption)
}
if direction, ok := action.Params.(string); ok {
return dExt.SwipeTo(direction, action.Identifier)
return dExt.SwipeTo(direction, identifierOption)
}
return fmt.Errorf("invalid %s params: %v", ACTION_Swipe, action.Params)
case ACTION_Input:
@@ -633,10 +606,7 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
options = append(options, WithCustomOption("description", action.Description))
}
if action.Identifier != "" {
options = append(options, WithCustomOption("log", map[string]interface{}{
"enable": true,
"data": action.Identifier,
}))
options = append(options, WithIdentifierOption(action.Identifier))
}
return dExt.Driver.Input(param, options...)
case CtlSleep:
@@ -671,6 +641,13 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
return nil
}
func (dExt *DriverExt) GetAbsScope(x1, y1, x2, y2 float64) (int, int, int, int) {
return int(x1 * float64(dExt.windowSize.Width) * dExt.scale),
int(y1 * float64(dExt.windowSize.Height) * dExt.scale),
int(x2 * float64(dExt.windowSize.Width) * dExt.scale),
int(y2 * float64(dExt.windowSize.Height) * dExt.scale)
}
func (dExt *DriverExt) DoValidation(check, assert, expected string, message ...string) bool {
var exists bool
if assert == AssertionExists {