From f13291cf95f3d8d5fa4ac0063d6ea6788d29f653 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 31 Aug 2022 20:36:34 +0800 Subject: [PATCH] feat: add FoundAction to SwipeUntil --- hrp/internal/uixt/swipe.go | 19 +++++++++++-------- hrp/internal/uixt/swipe_test.go | 18 ++++++++++-------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/hrp/internal/uixt/swipe.go b/hrp/internal/uixt/swipe.go index 93b0fccd..afae7550 100644 --- a/hrp/internal/uixt/swipe.go +++ b/hrp/internal/uixt/swipe.go @@ -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) { diff --git a/hrp/internal/uixt/swipe_test.go b/hrp/internal/uixt/swipe_test.go index fb570dc1..77d5d39e 100644 --- a/hrp/internal/uixt/swipe_test.go +++ b/hrp/internal/uixt/swipe_test.go @@ -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) }