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