mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-15 04:19:28 +08:00
feat: add FoundAction to SwipeUntil
This commit is contained in:
@@ -24,20 +24,23 @@ func (dExt *DriverExt) SwipeTo(direction string) (err error) {
|
|||||||
return dExt.WebDriver.Swipe(fromX, fromY, toX, toY)
|
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++ {
|
for i := 0; i < maxTimes; i++ {
|
||||||
err := condition(dExt)
|
if err := condition(dExt); err == nil {
|
||||||
if err == nil {
|
// do action after found
|
||||||
return nil
|
return action(dExt)
|
||||||
}
|
}
|
||||||
err = dExt.SwipeTo(direction)
|
if err := dExt.SwipeTo(direction); err != nil {
|
||||||
if err != nil {
|
|
||||||
log.Error().Err(err).Msgf("swipe %s failed", direction)
|
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) {
|
func (dExt *DriverExt) Swipe(pathname string, toX, toY int) (err error) {
|
||||||
|
|||||||
@@ -37,6 +37,10 @@ func TestSwipeUntil(t *testing.T) {
|
|||||||
x, y, width, height, err = d.FindTextByOCR("抖音")
|
x, y, width, height, err = d.FindTextByOCR("抖音")
|
||||||
return err
|
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()
|
driverExt.Homescreen()
|
||||||
|
|
||||||
@@ -46,22 +50,20 @@ func TestSwipeUntil(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// swipe until app found
|
// swipe until app found
|
||||||
err = driverExt.SwipeUntil("left", findApp, 10)
|
err = driverExt.SwipeUntil("left", findApp, foundAppAction, 10)
|
||||||
checkErr(t, err)
|
checkErr(t, err)
|
||||||
|
|
||||||
// click app, launch douyin
|
|
||||||
driverExt.TapFloat(x+width*0.5, y+height*0.5-20)
|
|
||||||
|
|
||||||
findLive := func(d *DriverExt) error {
|
findLive := func(d *DriverExt) error {
|
||||||
var err error
|
var err error
|
||||||
x, y, width, height, err = d.FindTextByOCR("点击进入直播间")
|
x, y, width, height, err = d.FindTextByOCR("点击进入直播间")
|
||||||
return err
|
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
|
// swipe until live room found
|
||||||
err = driverExt.SwipeUntil("up", findLive, 20)
|
err = driverExt.SwipeUntil("up", findLive, foundLiveAction, 20)
|
||||||
checkErr(t, err)
|
checkErr(t, err)
|
||||||
|
|
||||||
// enter live room
|
|
||||||
driverExt.TapFloat(x+width*0.5, y+height*0.5)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user