feat: SwipeUntil

This commit is contained in:
debugtalk
2022-08-31 20:15:29 +08:00
parent c180502666
commit 6d418ff795
3 changed files with 66 additions and 7 deletions
+22
View File
@@ -1,5 +1,11 @@
package uixt
import (
"fmt"
"github.com/rs/zerolog/log"
)
func (dExt *DriverExt) SwipeTo(direction string) (err error) {
width := dExt.windowSize.Width
height := dExt.windowSize.Height
@@ -18,6 +24,22 @@ func (dExt *DriverExt) SwipeTo(direction string) (err error) {
return dExt.WebDriver.Swipe(fromX, fromY, toX, toY)
}
type Condition func(driver *DriverExt) error
func (dExt *DriverExt) SwipeUntil(direction string, condition Condition, maxTimes int) error {
for i := 0; i < maxTimes; i++ {
err := condition(dExt)
if err == nil {
return nil
}
err = dExt.SwipeTo(direction)
if err != nil {
log.Error().Err(err).Msgf("swipe %s failed", direction)
}
}
return fmt.Errorf("swipe %s %d times, run condition failed", direction, maxTimes)
}
func (dExt *DriverExt) Swipe(pathname string, toX, toY int) (err error) {
return dExt.SwipeFloat(pathname, float64(toX), float64(toY))
}