mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-21 16:23:16 +08:00
refactor: convertToAbsoluteCoordinates
This commit is contained in:
@@ -1 +1 @@
|
||||
v5.0.0+2502111217
|
||||
v5.0.0+2502111347
|
||||
|
||||
@@ -417,18 +417,12 @@ func (ad *ADBDriver) TouchAndHold(x, y float64, opts ...option.ActionOption) (er
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) Drag(fromX, fromY, toX, toY float64, opts ...option.ActionOption) (err error) {
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
|
||||
if len(actionOptions.Offset) == 4 {
|
||||
fromX += float64(actionOptions.Offset[0])
|
||||
fromY += float64(actionOptions.Offset[1])
|
||||
toX += float64(actionOptions.Offset[2])
|
||||
toY += float64(actionOptions.Offset[3])
|
||||
absFromX, absFromY, absToX, absToY, err := convertToAbsoluteCoordinates(ad, fromX, fromY, toX, toY)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fromX += actionOptions.GetRandomOffset()
|
||||
fromY += actionOptions.GetRandomOffset()
|
||||
toX += actionOptions.GetRandomOffset()
|
||||
toY += actionOptions.GetRandomOffset()
|
||||
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
duration := 200.0
|
||||
if actionOptions.Duration > 0 {
|
||||
duration = actionOptions.Duration * 1000
|
||||
@@ -440,38 +434,30 @@ func (ad *ADBDriver) Drag(fromX, fromY, toX, toY float64, opts ...option.ActionO
|
||||
// adb shell input swipe fromX fromY toX toY
|
||||
_, err = ad.runShellCommand(
|
||||
"input", command,
|
||||
fmt.Sprintf("%.1f", fromX), fmt.Sprintf("%.1f", fromY),
|
||||
fmt.Sprintf("%.1f", toX), fmt.Sprintf("%.1f", toY),
|
||||
fmt.Sprintf("%.1f", absFromX), fmt.Sprintf("%.1f", absFromY),
|
||||
fmt.Sprintf("%.1f", absToX), fmt.Sprintf("%.1f", absToY),
|
||||
fmt.Sprintf("%d", int(duration)),
|
||||
)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "drag failed")
|
||||
return errors.Wrap(err, "adb drag failed")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) Swipe(fromX, fromY, toX, toY float64, opts ...option.ActionOption) error {
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
|
||||
if len(actionOptions.Offset) == 4 {
|
||||
fromX += float64(actionOptions.Offset[0])
|
||||
fromY += float64(actionOptions.Offset[1])
|
||||
toX += float64(actionOptions.Offset[2])
|
||||
toY += float64(actionOptions.Offset[3])
|
||||
absFromX, absFromY, absToX, absToY, err := convertToAbsoluteCoordinates(ad, fromX, fromY, toX, toY)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fromX += actionOptions.GetRandomOffset()
|
||||
fromY += actionOptions.GetRandomOffset()
|
||||
toX += actionOptions.GetRandomOffset()
|
||||
toY += actionOptions.GetRandomOffset()
|
||||
|
||||
// adb shell input swipe fromX fromY toX toY
|
||||
_, err := ad.runShellCommand(
|
||||
_, err = ad.runShellCommand(
|
||||
"input", "swipe",
|
||||
fmt.Sprintf("%.1f", fromX), fmt.Sprintf("%.1f", fromY),
|
||||
fmt.Sprintf("%.1f", toX), fmt.Sprintf("%.1f", toY),
|
||||
fmt.Sprintf("%.1f", absFromX), fmt.Sprintf("%.1f", absFromY),
|
||||
fmt.Sprintf("%.1f", absToX), fmt.Sprintf("%.1f", absToY),
|
||||
)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "swipe failed")
|
||||
return errors.Wrap(err, "adb swipe failed")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
package uixt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/code"
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/option"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func (dExt *XTDriver) Drag(fromX, fromY, toX, toY float64, opts ...option.ActionOption) (err error) {
|
||||
windowSize, err := dExt.Driver.WindowSize()
|
||||
if err != nil {
|
||||
return errors.Wrap(code.DeviceGetInfoError, err.Error())
|
||||
}
|
||||
width := windowSize.Width
|
||||
height := windowSize.Height
|
||||
|
||||
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
|
||||
|
||||
return dExt.Driver.Drag(fromX, fromY, toX, toY, opts...)
|
||||
}
|
||||
@@ -14,31 +14,14 @@ import (
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/option"
|
||||
)
|
||||
|
||||
func assertRelative(p float64) bool {
|
||||
return p >= 0 && p <= 1
|
||||
}
|
||||
|
||||
// SwipeRelative swipe from relative position [fromX, fromY] to relative position [toX, toY]
|
||||
func (dExt *XTDriver) SwipeRelative(fromX, fromY, toX, toY float64, opts ...option.ActionOption) error {
|
||||
if !assertRelative(fromX) || !assertRelative(fromY) ||
|
||||
!assertRelative(toX) || !assertRelative(toY) {
|
||||
return errors.Wrap(code.InvalidCaseError,
|
||||
fmt.Sprintf("fromX(%f), fromY(%f), toX(%f), toY(%f) must be less than 1",
|
||||
fromX, fromY, toX, toY))
|
||||
}
|
||||
|
||||
windowSize, err := dExt.Driver.WindowSize()
|
||||
absFromX, absFromY, absToX, absToY, err := convertToAbsoluteCoordinates(dExt.Driver, fromX, fromY, toX, toY)
|
||||
if err != nil {
|
||||
return errors.Wrap(code.DeviceGetInfoError, err.Error())
|
||||
return err
|
||||
}
|
||||
width := windowSize.Width
|
||||
height := windowSize.Height
|
||||
|
||||
fromX = float64(width) * fromX
|
||||
fromY = float64(height) * fromY
|
||||
toX = float64(width) * toX
|
||||
toY = float64(height) * toY
|
||||
err = dExt.Driver.Swipe(fromX, fromY, toX, toY, opts...)
|
||||
err = dExt.Driver.Swipe(absFromX, absFromY, absToX, absToY, opts...)
|
||||
if err != nil {
|
||||
return errors.Wrap(code.MobileUISwipeError, err.Error())
|
||||
}
|
||||
|
||||
47
pkg/uixt/driver_utils.go
Normal file
47
pkg/uixt/driver_utils.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package uixt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/code"
|
||||
)
|
||||
|
||||
func convertToAbsoluteCoordinates(driver IDriver, fromX, fromY, toX, toY float64) (
|
||||
absFromX, absFromY, absToX, absToY float64, err error) {
|
||||
|
||||
err = assertCoordinatesRelative(fromX, fromY, toX, toY)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
windowSize, err := driver.WindowSize()
|
||||
if err != nil {
|
||||
err = errors.Wrap(code.DeviceGetInfoError, err.Error())
|
||||
return
|
||||
}
|
||||
width := windowSize.Width
|
||||
height := windowSize.Height
|
||||
|
||||
absFromX = float64(width) * fromX
|
||||
absFromY = float64(height) * fromY
|
||||
absToX = float64(width) * toX
|
||||
absToY = float64(height) * toY
|
||||
|
||||
return absFromX, absFromY, absToX, absToY, nil
|
||||
}
|
||||
|
||||
func assertCoordinatesRelative(fromX, fromY, toX, toY float64) error {
|
||||
if !assertRelative(fromX) || !assertRelative(fromY) ||
|
||||
!assertRelative(toX) || !assertRelative(toY) {
|
||||
return errors.Wrap(code.InvalidCaseError,
|
||||
fmt.Sprintf("fromX(%f), fromY(%f), toX(%f), toY(%f) must be less than 1",
|
||||
fromX, fromY, toX, toY))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func assertRelative(p float64) bool {
|
||||
return p >= 0 && p <= 1
|
||||
}
|
||||
Reference in New Issue
Block a user