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
+9 -16
View File
@@ -315,11 +315,9 @@ 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)
@@ -362,11 +360,9 @@ 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
@@ -392,12 +388,9 @@ 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...) fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(ad, fromX, fromY, toX, toY)
if actionOptions.Relative { if err != nil {
fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(ad, fromX, fromY, toX, toY) return err
if err != nil {
return err
}
} }
// adb shell input swipe fromX fromY toX toY // adb shell input swipe fromX fromY toX toY
+9 -16
View File
@@ -255,11 +255,9 @@ 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)
@@ -351,12 +349,9 @@ 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...) fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(ud, fromX, fromY, toX, toY)
if actionOptions.Relative { if err != nil {
fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(ud, fromX, fromY, toX, toY) return err
if err != nil {
return err
}
} }
data := map[string]interface{}{ data := map[string]interface{}{
"startX": fromX, "startX": fromX,
@@ -381,11 +376,9 @@ 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
+42 -28
View File
@@ -40,48 +40,62 @@ 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
} }
windowSize, err := driver.WindowSize() // relative coordinates
if err != nil { if assertRelative(x) && assertRelative(y) {
err = errors.Wrap(code.DeviceGetInfoError, err.Error()) windowSize, err := driver.WindowSize()
return if err != nil {
err = errors.Wrap(code.DeviceGetInfoError, err.Error())
return 0, 0, err
}
absX = math.Round(float64(windowSize.Width)*x*10) / 10
absY = math.Round(float64(windowSize.Height)*y*10) / 10
return absX, absY, nil
} }
absX = math.Round(float64(windowSize.Width)*x*10) / 10 // invalid coordinates
absY = math.Round(float64(windowSize.Height)*y*10) / 10 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
} }
windowSize, err := driver.WindowSize() // relative coordinates
if err != nil { if assertRelative(fromX) && assertRelative(fromY) &&
err = errors.Wrap(code.DeviceGetInfoError, err.Error()) assertRelative(toX) && assertRelative(toY) {
return windowSize, err := driver.WindowSize()
if err != nil {
err = errors.Wrap(code.DeviceGetInfoError, err.Error())
return 0, 0, 0, 0, err
}
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
} }
width := windowSize.Width
height := windowSize.Height
absFromX = float64(width) * fromX // invalid coordinates
absFromY = float64(height) * fromY err = errors.Wrap(code.InvalidCaseError,
absToX = float64(width) * toX fmt.Sprintf("invalid coordinates fromX(%f), fromY(%f), toX(%f), toY(%f)",
absToY = float64(height) * toY fromX, fromY, toX, toY))
return
return absFromX, absFromY, absToX, absToY, nil
} }
func assertRelative(p float64) bool { func assertRelative(p float64) bool {
+3 -5
View File
@@ -177,11 +177,9 @@ 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
+6 -11
View File
@@ -615,11 +615,9 @@ 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)
@@ -647,12 +645,9 @@ 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...) fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(wd, fromX, fromY, toX, toY)
if actionOptions.Relative { if err != nil {
fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(wd, fromX, fromY, toX, toY) return err
if err != nil {
return err
}
} }
fromX = wd.toScale(fromX) fromX = wd.toScale(fromX)
fromY = wd.toScale(fromY) fromY = wd.toScale(fromY)
-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) {
+1 -6
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)
err = driver.DoubleTap(tapReq.X, tapReq.Y, option.WithRelative(true))
} else {
err = driver.DoubleTap(tapReq.X, tapReq.Y)
}
if err != nil { if err != nil {
RenderError(c, err) RenderError(c, err)
return return