fix: fix rotate tap swipe error

This commit is contained in:
余泓铮
2024-03-19 14:44:22 +08:00
parent 4979d4e1e2
commit 9f7795f703
6 changed files with 76 additions and 10 deletions

View File

@@ -185,6 +185,11 @@ func (ad *adbDriver) StopCamera() (err error) {
return
}
func (ad *adbDriver) Orientation() (orientation Orientation, err error) {
err = errDriverNotImplemented
return
}
func (ad *adbDriver) Homescreen() (err error) {
return ad.PressKeyCode(KCHome, KMEmpty)
}

View File

@@ -11,13 +11,14 @@ import (
)
var (
uiaServerURL = "http://localhost:6790/wd/hub"
uiaServerURL = "http://forward-to-6790:6790/wd/hub"
driverExt *DriverExt
)
func setupAndroid(t *testing.T) {
device, err := NewAndroidDevice()
checkErr(t, err)
device.UIA2 = true
driverExt, err = device.NewDriver()
checkErr(t, err)
}
@@ -215,6 +216,14 @@ func TestDriver_Swipe(t *testing.T) {
}
}
func TestDriver_Swipe_Relative(t *testing.T) {
setupAndroid(t)
err := driverExt.SwipeRelative(0.5, 0.7, 0.5, 0.5)
if err != nil {
t.Fatal(err)
}
}
func TestDriver_Drag(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {

View File

@@ -253,6 +253,20 @@ func (ud *uiaDriver) PressKeyCode(keyCode KeyCode, metaState KeyMeta, flags ...K
return
}
func (ud *uiaDriver) Orientation() (orientation Orientation, err error) {
// [[FBRoute GET:@"/orientation"] respondWithTarget:self action:@selector(handleGetOrientation:)]
var rawResp rawResponse
if rawResp, err = ud.httpGET("/session", ud.sessionId, "/orientation"); err != nil {
return "", err
}
reply := new(struct{ Value Orientation })
if err = json.Unmarshal(rawResp, reply); err != nil {
return "", err
}
orientation = reply.Value
return
}
func (ud *uiaDriver) Tap(x, y int, options ...ActionOption) error {
return ud.TapFloat(float64(x), float64(y), options...)
}

View File

@@ -537,6 +537,8 @@ type WebDriver interface {
// StopCamera Stops the camera for recording
StopCamera() error
Orientation() (orientation Orientation, err error)
// Tap Sends a tap event at the coordinate.
Tap(x, y int, options ...ActionOption) error
TapFloat(x, y float64, options ...ActionOption) error

View File

@@ -19,17 +19,30 @@ func assertRelative(p float64) bool {
func (dExt *DriverExt) SwipeRelative(fromX, fromY, toX, toY float64, options ...ActionOption) error {
width := dExt.windowSize.Width
height := dExt.windowSize.Height
orientation, err := dExt.Driver.Orientation()
if err != nil {
log.Warn().Err(err).Msgf("swipe from (%v, %v) to (%v, %v) get orientation failed, use default orientation",
fromX, fromY, toX, toY)
orientation = OrientationPortrait
}
if !assertRelative(fromX) || !assertRelative(fromY) ||
!assertRelative(toX) || !assertRelative(toY) {
return fmt.Errorf("fromX(%f), fromY(%f), toX(%f), toY(%f) must be less than 1",
fromX, fromY, toX, toY)
}
fromX = float64(width) * fromX
fromY = float64(height) * fromY
toX = float64(width) * toX
toY = float64(height) * toY
// 左转和右转都是"LANDSCAPE"
if orientation == OrientationPortrait {
fromX = float64(width) * fromX
fromY = float64(height) * fromY
toX = float64(width) * toX
toY = float64(height) * toY
} else {
fromX = float64(height) * fromX
fromY = float64(width) * fromY
toX = float64(height) * toX
toY = float64(width) * toY
}
return dExt.Driver.SwipeFloat(fromX, fromY, toX, toY, options...)
}

View File

@@ -2,6 +2,7 @@ package uixt
import (
"fmt"
"github.com/rs/zerolog/log"
)
func (dExt *DriverExt) TapAbsXY(x, y float64, options ...ActionOption) error {
@@ -15,9 +16,19 @@ func (dExt *DriverExt) TapXY(x, y float64, options ...ActionOption) error {
return fmt.Errorf("x, y percentage should be <= 1, got x=%v, y=%v", x, y)
}
x = x * float64(dExt.windowSize.Width)
y = y * float64(dExt.windowSize.Height)
orientation, err := dExt.Driver.Orientation()
if err != nil {
log.Warn().Err(err).Msgf("tap (%v, %v) get orientation failed, use default orientation",
x, y)
orientation = OrientationPortrait
}
if orientation == OrientationPortrait {
x = x * float64(dExt.windowSize.Width)
y = y * float64(dExt.windowSize.Height)
} else {
x = x * float64(dExt.windowSize.Height)
y = y * float64(dExt.windowSize.Width)
}
return dExt.TapAbsXY(x, y, options...)
}
@@ -86,7 +97,19 @@ func (dExt *DriverExt) DoubleTapXY(x, y float64) error {
if x > 1 || y > 1 {
return fmt.Errorf("x, y percentage should be < 1, got x=%v, y=%v", x, y)
}
orientation, err := dExt.Driver.Orientation()
if err != nil {
log.Warn().Err(err).Msgf("tap (%v, %v) get orientation failed, use default orientation",
x, y)
orientation = OrientationPortrait
}
if orientation == OrientationPortrait {
x = x * float64(dExt.windowSize.Width)
y = y * float64(dExt.windowSize.Height)
} else {
x = x * float64(dExt.windowSize.Height)
y = y * float64(dExt.windowSize.Width)
}
x = x * float64(dExt.windowSize.Width)
y = y * float64(dExt.windowSize.Height)
return dExt.Driver.DoubleTapFloat(x, y)