feat: add FoundAction to SwipeUntil

This commit is contained in:
debugtalk
2022-08-31 20:36:34 +08:00
parent 6573c94240
commit f13291cf95
2 changed files with 21 additions and 16 deletions

View File

@@ -24,20 +24,23 @@ func (dExt *DriverExt) SwipeTo(direction string) (err error) {
return dExt.WebDriver.Swipe(fromX, fromY, toX, toY)
}
type Condition func(driver *DriverExt) error
// FindCondition indicates the condition to find a UI element
type FindCondition func(driver *DriverExt) error
func (dExt *DriverExt) SwipeUntil(direction string, condition Condition, maxTimes int) error {
// FoundAction indicates the action to do after a UI element is found
type FoundAction func(driver *DriverExt) error
func (dExt *DriverExt) SwipeUntil(direction string, condition FindCondition, action FoundAction, maxTimes int) error {
for i := 0; i < maxTimes; i++ {
err := condition(dExt)
if err == nil {
return nil
if err := condition(dExt); err == nil {
// do action after found
return action(dExt)
}
err = dExt.SwipeTo(direction)
if err != nil {
if err := dExt.SwipeTo(direction); err != nil {
log.Error().Err(err).Msgf("swipe %s failed", direction)
}
}
return fmt.Errorf("swipe %s %d times, run condition failed", direction, maxTimes)
return fmt.Errorf("swipe %s %d times, match condition failed", direction, maxTimes)
}
func (dExt *DriverExt) Swipe(pathname string, toX, toY int) (err error) {

View File

@@ -37,6 +37,10 @@ func TestSwipeUntil(t *testing.T) {
x, y, width, height, err = d.FindTextByOCR("抖音")
return err
}
foundAppAction := func(d *DriverExt) error {
// click app, launch douyin
return d.TapFloat(x+width*0.5, y+height*0.5-20)
}
driverExt.Homescreen()
@@ -46,22 +50,20 @@ func TestSwipeUntil(t *testing.T) {
}
// swipe until app found
err = driverExt.SwipeUntil("left", findApp, 10)
err = driverExt.SwipeUntil("left", findApp, foundAppAction, 10)
checkErr(t, err)
// click app, launch douyin
driverExt.TapFloat(x+width*0.5, y+height*0.5-20)
findLive := func(d *DriverExt) error {
var err error
x, y, width, height, err = d.FindTextByOCR("点击进入直播间")
return err
}
foundLiveAction := func(d *DriverExt) error {
// enter live room
return d.TapFloat(x+width*0.5, y+height*0.5)
}
// swipe until live room found
err = driverExt.SwipeUntil("up", findLive, 20)
err = driverExt.SwipeUntil("up", findLive, foundLiveAction, 20)
checkErr(t, err)
// enter live room
driverExt.TapFloat(x+width*0.5, y+height*0.5)
}