feat: tap the first one matches text from given texts by ocr

This commit is contained in:
xucong.053
2022-10-11 14:53:21 +08:00
parent 0ff5fb762f
commit cd0ab6dfdb
12 changed files with 485 additions and 29 deletions

View File

@@ -63,14 +63,15 @@ type MobileAction struct {
Method MobileMethod `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
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"`
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
Direction interface{} `json:"direction,omitempty" yaml:"direction,omitempty"` // used by swipe to tap text or app
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"`
}
type ActionOption func(o *MobileAction)
@@ -87,6 +88,13 @@ func WithIndex(index int) ActionOption {
}
}
// WithDirection inputs direction (up, down, left, right, []float64{sx, sy, ex, ey})
func WithDirection(direction interface{}) ActionOption {
return func(o *MobileAction) {
o.Direction = direction
}
}
func WithText(text string) ActionOption {
return func(o *MobileAction) {
o.Text = text
@@ -363,7 +371,7 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
}
foundAppAction := func(d *DriverExt) error {
// click app to launch
return d.TapAbsXY(point.X, point.Y-20, action.Identifier)
return d.TapAbsXY(point.X, point.Y-25, action.Identifier)
}
// go to home screen
@@ -386,27 +394,52 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
return fmt.Errorf("invalid %s params, should be app name(string), got %v",
ACTION_SwipeToTapApp, action.Params)
case ACTION_SwipeToTapText:
var point PointF
var findText func(d *DriverExt) error
if text, ok := action.Params.(string); ok {
var point PointF
findText := func(d *DriverExt) error {
findText = func(d *DriverExt) error {
var err error
point, err = d.GetTextXY(text, action.Index)
return err
}
foundTextAction := func(d *DriverExt) error {
// tap text
return d.TapAbsXY(point.X, point.Y, action.Identifier)
} else if texts, ok := action.Params.([]interface{}); ok {
findText = func(d *DriverExt) error {
var err error
var ts []string
for _, t := range texts {
ts = append(ts, t.(string))
}
points, err := d.GetTextXYs(ts)
if err != nil {
return err
}
for _, point = range points {
if point != (PointF{}) {
return nil
}
}
return errors.New("failed to find text position")
}
// default to retry 10 times
if action.MaxRetryTimes == 0 {
action.MaxRetryTimes = 10
}
// swipe until live room found
return dExt.SwipeUntil("up", findText, foundTextAction, action.MaxRetryTimes)
} else {
return fmt.Errorf("invalid %s params, should be app text(string or []string), got %v",
ACTION_SwipeToTapText, action.Params)
}
return fmt.Errorf("invalid %s params, should be app text(string), got %v",
ACTION_SwipeToTapText, action.Params)
foundTextAction := func(d *DriverExt) error {
// tap text
return d.TapAbsXY(point.X, point.Y, action.Identifier)
}
// default to retry 10 times
if action.MaxRetryTimes == 0 {
action.MaxRetryTimes = 10
}
if action.Direction != nil {
return dExt.SwipeUntil(action.Direction, findText, foundTextAction, action.MaxRetryTimes)
}
// swipe until live room found
return dExt.SwipeUntil("up", findText, foundTextAction, action.MaxRetryTimes)
case AppTerminate:
if bundleId, ok := action.Params.(string); ok {
success, err := dExt.Driver.AppTerminate(bundleId)
@@ -497,7 +530,7 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
param := fmt.Sprintf("%v", action.Params)
options := []DataOption{}
if action.Text != "" {
options = append(options, WithCustomOption("text", action.Text))
options = append(options, WithCustomOption("textview", action.Text))
}
if action.ID != "" {
options = append(options, WithCustomOption("id", action.ID))