change: convertToAbsolutePoint, convertToAbsoluteCoordinates

This commit is contained in:
lilong.129
2025-03-04 20:02:59 +08:00
parent e35aa782c2
commit 65564b958f
8 changed files with 71 additions and 94 deletions
+1 -1
View File
@@ -1 +1 @@
v5.0.0+2503041939 v5.0.0+2503042003
-7
View File
@@ -315,12 +315,10 @@ func (ad *ADBDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
func (ad *ADBDriver) DoubleTap(x, y float64, opts ...option.ActionOption) error { func (ad *ADBDriver) DoubleTap(x, y float64, opts ...option.ActionOption) error {
var err error var err error
actionOptions := option.NewActionOptions(opts...) actionOptions := option.NewActionOptions(opts...)
if actionOptions.Relative {
x, y, err = convertToAbsolutePoint(ad, x, y) x, y, err = convertToAbsolutePoint(ad, x, y)
if err != nil { if err != nil {
return err return err
} }
}
x, y = actionOptions.ApplyOffset(x, y) x, y = actionOptions.ApplyOffset(x, y)
// adb shell input tap x y // adb shell input tap x y
@@ -362,12 +360,10 @@ 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) { func (ad *ADBDriver) Drag(fromX, fromY, toX, toY float64, opts ...option.ActionOption) (err error) {
actionOptions := option.NewActionOptions(opts...) actionOptions := option.NewActionOptions(opts...)
if actionOptions.Relative {
fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(ad, fromX, fromY, toX, toY) fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(ad, fromX, fromY, toX, toY)
if err != nil { if err != nil {
return err return err
} }
}
duration := 200.0 duration := 200.0
if actionOptions.Duration > 0 { if actionOptions.Duration > 0 {
@@ -392,13 +388,10 @@ func (ad *ADBDriver) Drag(fromX, fromY, toX, toY float64, opts ...option.ActionO
func (ad *ADBDriver) Swipe(fromX, fromY, toX, toY float64, opts ...option.ActionOption) error { func (ad *ADBDriver) Swipe(fromX, fromY, toX, toY float64, opts ...option.ActionOption) error {
var err error var err error
actionOptions := option.NewActionOptions(opts...)
if actionOptions.Relative {
fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(ad, fromX, fromY, toX, toY) fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(ad, fromX, fromY, toX, toY)
if err != nil { if err != nil {
return err return err
} }
}
// adb shell input swipe fromX fromY toX toY // adb shell input swipe fromX fromY toX toY
_, err = ad.runShellCommand( _, err = ad.runShellCommand(
-7
View File
@@ -255,12 +255,10 @@ func (ud *UIA2Driver) Orientation() (orientation types.Orientation, err error) {
func (ud *UIA2Driver) DoubleTap(x, y float64, opts ...option.ActionOption) error { func (ud *UIA2Driver) DoubleTap(x, y float64, opts ...option.ActionOption) error {
var err error var err error
actionOptions := option.NewActionOptions(opts...) actionOptions := option.NewActionOptions(opts...)
if actionOptions.Relative {
x, y, err = convertToAbsolutePoint(ud, x, y) x, y, err = convertToAbsolutePoint(ud, x, y)
if err != nil { if err != nil {
return err return err
} }
}
x, y = actionOptions.ApplyOffset(x, y) x, y = actionOptions.ApplyOffset(x, y)
data := map[string]interface{}{ data := map[string]interface{}{
@@ -351,13 +349,10 @@ func (ud *UIA2Driver) TouchAndHold(x, y float64, opts ...option.ActionOption) (e
// steps, the swipe will take around 0.5 seconds to complete. // steps, the swipe will take around 0.5 seconds to complete.
func (ud *UIA2Driver) Drag(fromX, fromY, toX, toY float64, opts ...option.ActionOption) error { func (ud *UIA2Driver) Drag(fromX, fromY, toX, toY float64, opts ...option.ActionOption) error {
var err error var err error
actionOptions := option.NewActionOptions(opts...)
if actionOptions.Relative {
fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(ud, fromX, fromY, toX, toY) fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(ud, fromX, fromY, toX, toY)
if err != nil { if err != nil {
return err return err
} }
}
data := map[string]interface{}{ data := map[string]interface{}{
"startX": fromX, "startX": fromX,
"startY": fromY, "startY": fromY,
@@ -381,12 +376,10 @@ func (ud *UIA2Driver) Swipe(fromX, fromY, toX, toY float64, opts ...option.Actio
// register(postHandler, new Swipe("/wd/hub/session/:sessionId/touch/perform")) // register(postHandler, new Swipe("/wd/hub/session/:sessionId/touch/perform"))
var err error var err error
actionOptions := option.NewActionOptions(opts...) actionOptions := option.NewActionOptions(opts...)
if actionOptions.Relative {
fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(ud, fromX, fromY, toX, toY) fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(ud, fromX, fromY, toX, toY)
if err != nil { if err != nil {
return err return err
} }
}
duration := 200.0 duration := 200.0
if actionOptions.PressDuration > 0 { if actionOptions.PressDuration > 0 {
+26 -12
View File
@@ -40,38 +40,45 @@ func convertToAbsoluteScope(driver IDriver, opts ...option.ActionOption) []optio
} }
func convertToAbsolutePoint(driver IDriver, x, y float64) (absX, absY float64, err error) { func convertToAbsolutePoint(driver IDriver, x, y float64) (absX, absY float64, err error) {
if !assertRelative(x) || !assertRelative(y) { // absolute coordinates
err = errors.Wrap(code.InvalidCaseError, if x > 1 || y > 1 {
fmt.Sprintf("x(%f), y(%f) must be less than 1", x, y)) return x, y, nil
return
} }
// relative coordinates
if assertRelative(x) && assertRelative(y) {
windowSize, err := driver.WindowSize() windowSize, err := driver.WindowSize()
if err != nil { if err != nil {
err = errors.Wrap(code.DeviceGetInfoError, err.Error()) err = errors.Wrap(code.DeviceGetInfoError, err.Error())
return return 0, 0, err
} }
absX = math.Round(float64(windowSize.Width)*x*10) / 10 absX = math.Round(float64(windowSize.Width)*x*10) / 10
absY = math.Round(float64(windowSize.Height)*y*10) / 10 absY = math.Round(float64(windowSize.Height)*y*10) / 10
return absX, absY, nil
}
// invalid coordinates
err = errors.Wrap(code.InvalidCaseError,
fmt.Sprintf("invalid coordinates x(%f), y(%f)", x, y))
return return
} }
func convertToAbsoluteCoordinates(driver IDriver, fromX, fromY, toX, toY float64) ( func convertToAbsoluteCoordinates(driver IDriver, fromX, fromY, toX, toY float64) (
absFromX, absFromY, absToX, absToY float64, err error) { absFromX, absFromY, absToX, absToY float64, err error) {
if !assertRelative(fromX) || !assertRelative(fromY) || // absolute coordinates
!assertRelative(toX) || !assertRelative(toY) { if fromX > 1 || toX > 1 || fromY > 1 || toY > 1 {
err = errors.Wrap(code.InvalidCaseError, return fromX, fromY, toX, toY, nil
fmt.Sprintf("fromX(%f), fromY(%f), toX(%f), toY(%f) must be less than 1",
fromX, fromY, toX, toY))
return
} }
// relative coordinates
if assertRelative(fromX) && assertRelative(fromY) &&
assertRelative(toX) && assertRelative(toY) {
windowSize, err := driver.WindowSize() windowSize, err := driver.WindowSize()
if err != nil { if err != nil {
err = errors.Wrap(code.DeviceGetInfoError, err.Error()) err = errors.Wrap(code.DeviceGetInfoError, err.Error())
return return 0, 0, 0, 0, err
} }
width := windowSize.Width width := windowSize.Width
height := windowSize.Height height := windowSize.Height
@@ -84,6 +91,13 @@ func convertToAbsoluteCoordinates(driver IDriver, fromX, fromY, toX, toY float64
return absFromX, absFromY, absToX, absToY, nil return absFromX, absFromY, absToX, absToY, nil
} }
// invalid coordinates
err = errors.Wrap(code.InvalidCaseError,
fmt.Sprintf("invalid coordinates fromX(%f), fromY(%f), toX(%f), toY(%f)",
fromX, fromY, toX, toY))
return
}
func assertRelative(p float64) bool { func assertRelative(p float64) bool {
return p >= 0 && p <= 1 return p >= 0 && p <= 1
} }
-2
View File
@@ -177,12 +177,10 @@ func (hd *HDCDriver) Drag(fromX, fromY, toX, toY float64, opts ...option.ActionO
func (hd *HDCDriver) Swipe(fromX, fromY, toX, toY float64, opts ...option.ActionOption) error { func (hd *HDCDriver) Swipe(fromX, fromY, toX, toY float64, opts ...option.ActionOption) error {
var err error var err error
actionOptions := option.NewActionOptions(opts...) actionOptions := option.NewActionOptions(opts...)
if actionOptions.Relative {
fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(hd, fromX, fromY, toX, toY) fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(hd, fromX, fromY, toX, toY)
if err != nil { if err != nil {
return err return err
} }
}
duration := 200 duration := 200
if actionOptions.PressDuration > 0 { if actionOptions.PressDuration > 0 {
-5
View File
@@ -615,12 +615,10 @@ func (wd *WDADriver) DoubleTap(x, y float64, opts ...option.ActionOption) error
// [[FBRoute POST:@"/wda/doubleTap"] respondWithTarget:self action:@selector(handleDoubleTapCoordinate:)] // [[FBRoute POST:@"/wda/doubleTap"] respondWithTarget:self action:@selector(handleDoubleTapCoordinate:)]
var err error var err error
actionOptions := option.NewActionOptions(opts...) actionOptions := option.NewActionOptions(opts...)
if actionOptions.Relative {
x, y, err = convertToAbsolutePoint(wd, x, y) x, y, err = convertToAbsolutePoint(wd, x, y)
if err != nil { if err != nil {
return err return err
} }
}
x, y = actionOptions.ApplyOffset(x, y) x, y = actionOptions.ApplyOffset(x, y)
x = wd.toScale(x) x = wd.toScale(x)
@@ -647,13 +645,10 @@ func (wd *WDADriver) TouchAndHold(x, y float64, opts ...option.ActionOption) (er
func (wd *WDADriver) Drag(fromX, fromY, toX, toY float64, opts ...option.ActionOption) error { func (wd *WDADriver) Drag(fromX, fromY, toX, toY float64, opts ...option.ActionOption) error {
// [[FBRoute POST:@"/wda/dragfromtoforduration"] respondWithTarget:self action:@selector(handleDragCoordinate:)] // [[FBRoute POST:@"/wda/dragfromtoforduration"] respondWithTarget:self action:@selector(handleDragCoordinate:)]
var err error var err error
actionOptions := option.NewActionOptions(opts...)
if actionOptions.Relative {
fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(wd, fromX, fromY, toX, toY) fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(wd, fromX, fromY, toX, toY)
if err != nil { if err != nil {
return err return err
} }
}
fromX = wd.toScale(fromX) fromX = wd.toScale(fromX)
fromY = wd.toScale(fromY) fromY = wd.toScale(fromY)
toX = wd.toScale(toX) toX = wd.toScale(toX)
-11
View File
@@ -19,7 +19,6 @@ type ActionOptions struct {
Direction interface{} `json:"direction,omitempty" yaml:"direction,omitempty"` // used by swipe to tap text or app Direction interface{} `json:"direction,omitempty" yaml:"direction,omitempty"` // used by swipe to tap text or app
Timeout int `json:"timeout,omitempty" yaml:"timeout,omitempty"` // TODO: wait timeout in seconds for mobile action Timeout int `json:"timeout,omitempty" yaml:"timeout,omitempty"` // TODO: wait timeout in seconds for mobile action
Frequency int `json:"frequency,omitempty" yaml:"frequency,omitempty"` Frequency int `json:"frequency,omitempty" yaml:"frequency,omitempty"`
Relative bool `json:"relative,omitempty" yaml:"relative,omitempty"` // use relative coordinate
ScreenOptions ScreenOptions
@@ -56,9 +55,6 @@ func (o *ActionOptions) Options() []ActionOption {
if o.Steps != 0 { if o.Steps != 0 {
options = append(options, WithSteps(o.Steps)) options = append(options, WithSteps(o.Steps))
} }
if o.Relative {
options = append(options, WithRelative(true))
}
switch v := o.Direction.(type) { switch v := o.Direction.(type) {
case string: case string:
@@ -260,13 +256,6 @@ func WithDirection(direction string) ActionOption {
} }
} }
// WithRelative set relative coordinate, (0, 0) is the top-left corner of the screen
func WithRelative(relative bool) ActionOption {
return func(o *ActionOptions) {
o.Relative = relative
}
}
// WithCustomDirection inputs sx, sy, ex, ey // WithCustomDirection inputs sx, sy, ex, ey
func WithCustomDirection(sx, sy, ex, ey float64) ActionOption { func WithCustomDirection(sx, sy, ex, ey float64) ActionOption {
return func(o *ActionOptions) { return func(o *ActionOptions) {
-5
View File
@@ -128,12 +128,7 @@ func (r *Router) doubleTapHandler(c *gin.Context) {
return return
} }
if tapReq.X < 1 && tapReq.Y < 1 {
err = driver.DoubleTap(tapReq.X, tapReq.Y, option.WithRelative(true))
} else {
err = driver.DoubleTap(tapReq.X, tapReq.Y) err = driver.DoubleTap(tapReq.X, tapReq.Y)
}
if err != nil { if err != nil {
RenderError(c, err) RenderError(c, err)
return return