mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +08:00
refactor: swipe actions
This commit is contained in:
@@ -10,7 +10,17 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var uiaServerURL = "http://localhost:6790/wd/hub"
|
||||
var (
|
||||
uiaServerURL = "http://localhost:6790/wd/hub"
|
||||
driverExt *DriverExt
|
||||
)
|
||||
|
||||
func setupAndroid(t *testing.T) {
|
||||
device, err := NewAndroidDevice()
|
||||
checkErr(t, err)
|
||||
driverExt, err = device.NewDriver(nil)
|
||||
checkErr(t, err)
|
||||
}
|
||||
|
||||
func TestDriver_NewSession(t *testing.T) {
|
||||
driver, err := NewUIADriver(nil, uiaServerURL)
|
||||
|
||||
@@ -280,7 +280,7 @@ type IOCRService interface {
|
||||
func (dExt *DriverExt) GetScreenTextsByOCR() (texts OCRTexts, err error) {
|
||||
var bufSource *bytes.Buffer
|
||||
if bufSource, err = dExt.TakeScreenShot(
|
||||
builtin.GenNameWithTimestamp("screenshot_%d_ocr")); err != nil {
|
||||
builtin.GenNameWithTimestamp("%d_ocr")); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -140,6 +140,10 @@ func (dExt *DriverExt) prepareSwipeAction(action MobileAction) func(d *DriverExt
|
||||
}
|
||||
|
||||
func (dExt *DriverExt) swipeToTapTexts(texts []string, action MobileAction) error {
|
||||
if len(texts) == 0 {
|
||||
return errors.New("no text to tap")
|
||||
}
|
||||
|
||||
if len(action.Scope) != 4 {
|
||||
action.Scope = []float64{0, 0, 1, 1}
|
||||
}
|
||||
@@ -169,13 +173,8 @@ func (dExt *DriverExt) swipeToTapTexts(texts []string, action MobileAction) erro
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// FIXME: handle index
|
||||
for _, point = range points {
|
||||
if point != (PointF{X: 0, Y: 0}) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return errors.New("failed to find text position")
|
||||
point = points[0] // FIXME
|
||||
return nil
|
||||
}
|
||||
foundTextAction := func(d *DriverExt) error {
|
||||
// tap text
|
||||
@@ -197,7 +196,7 @@ func (dExt *DriverExt) swipeToTapApp(appName string, action MobileAction) error
|
||||
dExt.SwipeRight()
|
||||
}
|
||||
|
||||
action.Offset = []int{0, -25}
|
||||
action.Offset = []int{0, -25} // tap app icon above the text
|
||||
action.Params = "left"
|
||||
|
||||
return dExt.swipeToTapTexts([]string{appName}, action)
|
||||
|
||||
@@ -6,43 +6,44 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSwipeUntil(t *testing.T) {
|
||||
driverExt, err := iosDevice.NewDriver(nil)
|
||||
func TestAndroidSwipeAction(t *testing.T) {
|
||||
setupAndroid(t)
|
||||
|
||||
action := MobileAction{
|
||||
Method: ACTION_Swipe,
|
||||
Params: "up",
|
||||
}
|
||||
swipeAction := driverExt.prepareSwipeAction(action)
|
||||
|
||||
err := swipeAction(driverExt)
|
||||
checkErr(t, err)
|
||||
|
||||
var point PointF
|
||||
findApp := func(d *DriverExt) error {
|
||||
var err error
|
||||
point, err = d.FindScreenTextByOCR("抖音")
|
||||
return err
|
||||
}
|
||||
foundAppAction := func(d *DriverExt) error {
|
||||
// click app, launch douyin
|
||||
return d.TapAbsXY(point.X, point.Y)
|
||||
action = MobileAction{
|
||||
Method: ACTION_Swipe,
|
||||
Params: []float64{0.5, 0.5, 0.5, 0.9},
|
||||
}
|
||||
swipeAction = driverExt.prepareSwipeAction(action)
|
||||
|
||||
driverExt.Driver.Homescreen()
|
||||
|
||||
// swipe to first screen
|
||||
for i := 0; i < 5; i++ {
|
||||
driverExt.SwipeRight()
|
||||
}
|
||||
|
||||
// swipe until app found
|
||||
err = driverExt.SwipeUntil("left", findApp, foundAppAction, WithDataMaxRetryTimes(10))
|
||||
checkErr(t, err)
|
||||
|
||||
findLive := func(d *DriverExt) error {
|
||||
var err error
|
||||
point, err = d.FindScreenTextByOCR("点击进入直播间")
|
||||
return err
|
||||
}
|
||||
foundLiveAction := func(d *DriverExt) error {
|
||||
// enter live room
|
||||
return d.TapAbsXY(point.X, point.Y)
|
||||
}
|
||||
|
||||
// swipe until live room found
|
||||
err = driverExt.SwipeUntil("up", findLive, foundLiveAction, WithDataMaxRetryTimes(20))
|
||||
err = swipeAction(driverExt)
|
||||
checkErr(t, err)
|
||||
}
|
||||
|
||||
func TestAndroidSwipeToTapApp(t *testing.T) {
|
||||
setupAndroid(t)
|
||||
|
||||
err := driverExt.swipeToTapApp("抖音", MobileAction{})
|
||||
checkErr(t, err)
|
||||
}
|
||||
|
||||
func TestAndroidSwipeToTapTexts(t *testing.T) {
|
||||
setupAndroid(t)
|
||||
|
||||
err := driverExt.Driver.AppLaunch("com.ss.android.ugc.aweme")
|
||||
checkErr(t, err)
|
||||
|
||||
action := MobileAction{
|
||||
Params: "up",
|
||||
}
|
||||
err = driverExt.swipeToTapTexts([]string{"点击进入直播间", "直播中"}, action)
|
||||
checkErr(t, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user