fix: swipe to find texts

This commit is contained in:
lilong.129
2023-04-28 13:14:30 +08:00
parent 9981dad1f8
commit 37a984c243
3 changed files with 28 additions and 23 deletions
@@ -47,9 +47,6 @@ func TestIOSDouyinFollowLive(t *testing.T) {
if err := testCase.Dump2JSON("demo_douyin_follow_live.json"); err != nil { if err := testCase.Dump2JSON("demo_douyin_follow_live.json"); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := testCase.Dump2YAML("demo_douyin_follow_live.yaml"); err != nil {
t.Fatal(err)
}
runner := hrp.NewRunner(t).SetSaveTests(true) runner := hrp.NewRunner(t).SetSaveTests(true)
err := runner.Run(testCase) err := runner.Run(testCase)
+16 -15
View File
@@ -64,19 +64,20 @@ type MobileAction struct {
Method ActionMethod `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
MaxRetryTimes int `json:"max_retry_times,omitempty" yaml:"max_retry_times,omitempty"` // max retry times 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 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 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 Steps int `json:"steps,omitempty" yaml:"steps,omitempty"` // used to set steps of android swipe action
Scope []float64 `json:"scope,omitempty" yaml:"scope,omitempty"` // used by ocr to get text position in the scope Direction interface{} `json:"direction,omitempty" yaml:"direction,omitempty"` // used by swipe to tap text or app
Offset []int `json:"offset,omitempty" yaml:"offset,omitempty"` // used to tap offset of point Scope []float64 `json:"scope,omitempty" yaml:"scope,omitempty"` // used by ocr to get text position in the scope
Index int `json:"index,omitempty" yaml:"index,omitempty"` // index of the target element, should start from 1 Offset []int `json:"offset,omitempty" yaml:"offset,omitempty"` // used to tap offset of point
Timeout int `json:"timeout,omitempty" yaml:"timeout,omitempty"` // TODO: wait timeout in seconds for mobile action Index int `json:"index,omitempty" yaml:"index,omitempty"` // index of the target element, should start from 1
IgnoreNotFoundError bool `json:"ignore_NotFoundError,omitempty" yaml:"ignore_NotFoundError,omitempty"` // ignore error if target element not found Timeout int `json:"timeout,omitempty" yaml:"timeout,omitempty"` // TODO: wait timeout in seconds for mobile action
Text string `json:"text,omitempty" yaml:"text,omitempty"` IgnoreNotFoundError bool `json:"ignore_NotFoundError,omitempty" yaml:"ignore_NotFoundError,omitempty"` // ignore error if target element not found
ID string `json:"id,omitempty" yaml:"id,omitempty"` Text string `json:"text,omitempty" yaml:"text,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"` ID string `json:"id,omitempty" yaml:"id,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
} }
type ActionOption func(o *MobileAction) type ActionOption func(o *MobileAction)
@@ -114,14 +115,14 @@ func WithSteps(steps int) ActionOption {
// WithDirection inputs direction (up, down, left, right) // WithDirection inputs direction (up, down, left, right)
func WithDirection(direction string) ActionOption { func WithDirection(direction string) ActionOption {
return func(o *MobileAction) { return func(o *MobileAction) {
o.Params = direction o.Direction = direction
} }
} }
// WithCustomDirection inputs sx, sy, ex, ey // WithCustomDirection inputs sx, sy, ex, ey
func WithCustomDirection(sx, sy, ex, ey float64) ActionOption { func WithCustomDirection(sx, sy, ex, ey float64) ActionOption {
return func(o *MobileAction) { return func(o *MobileAction) {
o.Params = []float64{sx, sy, ex, ey} o.Direction = []float64{sx, sy, ex, ey}
} }
} }
+12 -5
View File
@@ -101,26 +101,33 @@ func (dExt *DriverExt) prepareSwipeAction(action MobileAction) func(d *DriverExt
dataOptions := make([]DataOption, 3) dataOptions := make([]DataOption, 3)
dataOptions = append(dataOptions, identifierOption, durationOption, stepsOption) dataOptions = append(dataOptions, identifierOption, durationOption, stepsOption)
var swipeDirection interface{}
if action.Direction != nil {
swipeDirection = action.Direction
} else {
swipeDirection = "up" // default swipe up
}
return func(d *DriverExt) error { return func(d *DriverExt) error {
defer func() { defer func() {
// wait for swipe action to completed and content to load completely // 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*action.WaitTime) * time.Millisecond)
}() }()
if d, ok := action.Params.(string); ok { if d, ok := swipeDirection.(string); ok {
// enum direction: up, down, left, right // enum direction: up, down, left, right
if err := dExt.SwipeTo(d, dataOptions...); err != nil { if err := dExt.SwipeTo(d, dataOptions...); err != nil {
log.Error().Err(err).Msgf("swipe %s failed", d) log.Error().Err(err).Msgf("swipe %s failed", d)
return err return err
} }
} else if d, ok := action.Params.([]float64); ok { } else if d, ok := swipeDirection.([]float64); ok {
// custom direction: [fromX, fromY, toX, toY] // 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], dataOptions...); err != nil {
log.Error().Err(err).Msgf("swipe from (%v, %v) to (%v, %v) failed", log.Error().Err(err).Msgf("swipe from (%v, %v) to (%v, %v) failed",
d[0], d[1], d[2], d[3]) d[0], d[1], d[2], d[3])
return err return err
} }
} else if d, ok := action.Params.([]interface{}); ok { } else if d, ok := swipeDirection.([]interface{}); ok {
// loaded from json case // loaded from json case
// custom direction: [fromX, fromY, toX, toY] // custom direction: [fromX, fromY, toX, toY]
sx, _ := builtin.Interface2Float64(d[0]) sx, _ := builtin.Interface2Float64(d[0])
@@ -133,7 +140,7 @@ func (dExt *DriverExt) prepareSwipeAction(action MobileAction) func(d *DriverExt
return err return err
} }
} else { } else {
return fmt.Errorf("invalid swipe params %v", action.Params) return fmt.Errorf("invalid swipe params %v", swipeDirection)
} }
return nil return nil
} }
@@ -197,7 +204,7 @@ func (dExt *DriverExt) swipeToTapApp(appName string, action MobileAction) error
} }
action.Offset = []int{0, -25} // tap app icon above the text action.Offset = []int{0, -25} // tap app icon above the text
action.Params = "left" action.Direction = "left"
return dExt.swipeToTapTexts([]string{appName}, action) return dExt.swipeToTapTexts([]string{appName}, action)
} }