feat: SwipeUntil

This commit is contained in:
debugtalk
2022-08-31 20:15:29 +08:00
parent 3aed97eab1
commit 6573c94240
3 changed files with 66 additions and 7 deletions

View File

@@ -9,6 +9,7 @@ import (
"io/ioutil"
"mime/multipart"
"net/http"
"strings"
"time"
)
@@ -92,10 +93,12 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte) (rect image.Rec
}
for _, ocrResult := range ocrResults {
if ocrResult.Text != text {
// not contains text
if !strings.Contains(ocrResult.Text, text) {
continue
}
// contains text
// only find the first matched one
rect = image.Rectangle{
// ocrResult.Points 顺序:左上 -> 右上 -> 右下 -> 左下

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))
}

View File

@@ -2,15 +2,10 @@ package uixt
import (
"testing"
"github.com/electricbubble/gwda"
)
func TestDriverExt_Swipe(t *testing.T) {
driver, err := gwda.NewUSBDriver(nil)
checkErr(t, err)
driverExt, err := Extend(driver, 0.95)
driverExt, err := InitWDAClient()
checkErr(t, err)
pathSearch := "/Users/hero/Documents/temp/2020-05/opencv/flag7.png"
@@ -31,3 +26,42 @@ func TestDriverExt_Swipe(t *testing.T) {
err = driverExt.OnlyOnceThreshold(0.92).SwipeOffsetFloat(pathSearch, 300.9, 499.1, 0.2, 0.5)
checkErr(t, err)
}
func TestSwipeUntil(t *testing.T) {
driverExt, err := InitWDAClient()
checkErr(t, err)
var x, y, width, height float64
findApp := func(d *DriverExt) error {
var err error
x, y, width, height, err = d.FindTextByOCR("抖音")
return err
}
driverExt.Homescreen()
// swipe to first screen
for i := 0; i < 5; i++ {
driverExt.SwipeTo("right")
}
// swipe until app found
err = driverExt.SwipeUntil("left", findApp, 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
}
// swipe until live room found
err = driverExt.SwipeUntil("up", findLive, 20)
checkErr(t, err)
// enter live room
driverExt.TapFloat(x+width*0.5, y+height*0.5)
}