feat: ApplySwipeOffset

This commit is contained in:
lilong.129 2025-03-17 17:57:10 +08:00
parent 9fb53590ca
commit 182de16751
7 changed files with 33 additions and 2 deletions

View File

@ -1 +1 @@
v5.0.0-beta-2503171744
v5.0.0-beta-2503171757

View File

@ -365,6 +365,7 @@ func (ad *ADBDriver) Drag(fromX, fromY, toX, toY float64, opts ...option.ActionO
if err != nil {
return err
}
fromX, fromY, toX, toY = actionOptions.ApplySwipeOffset(fromX, fromY, toX, toY)
duration := 200.0
if actionOptions.Duration > 0 {
@ -393,6 +394,8 @@ func (ad *ADBDriver) Swipe(fromX, fromY, toX, toY float64, opts ...option.Action
if err != nil {
return err
}
actionOptions := option.NewActionOptions(opts...)
fromX, fromY, toX, toY = actionOptions.ApplySwipeOffset(fromX, fromY, toX, toY)
// adb shell input swipe fromX fromY toX toY
_, err = ad.runShellCommand(

View File

@ -353,6 +353,9 @@ func (ud *UIA2Driver) Drag(fromX, fromY, toX, toY float64, opts ...option.Action
if err != nil {
return err
}
actionOptions := option.NewActionOptions(opts...)
fromX, fromY, toX, toY = actionOptions.ApplySwipeOffset(fromX, fromY, toX, toY)
data := map[string]interface{}{
"startX": fromX,
"startY": fromY,
@ -380,6 +383,7 @@ func (ud *UIA2Driver) Swipe(fromX, fromY, toX, toY float64, opts ...option.Actio
if err != nil {
return err
}
fromX, fromY, toX, toY = actionOptions.ApplySwipeOffset(fromX, fromY, toX, toY)
duration := 200.0
if actionOptions.PressDuration > 0 {

View File

@ -181,11 +181,16 @@ func TestDriverExt_Action_Risk(t *testing.T) {
err = driver.TapXY(0.5, 0.5, option.WithOffsetRandomRange(-10, 10))
assert.Nil(t, err)
// swipe direction with offset
// swipe direction with constant offset
err = driver.Swipe(0.5, 0.5, 0.5, 0.9,
option.WithSwipeOffset(-50, 50, -50, 50))
assert.Nil(t, err)
// swipe direction with random offset
err = driver.Swipe(0.5, 0.5, 0.5, 0.9,
option.WithOffsetRandomRange(-50, 50))
assert.Nil(t, err)
// tap random point in ocr text rect
err = driver.TapByOCR("首页", option.WithTapRandomRect(true))
}

View File

@ -181,6 +181,7 @@ func (hd *HDCDriver) Swipe(fromX, fromY, toX, toY float64, opts ...option.Action
if err != nil {
return err
}
fromX, fromY, toX, toY = actionOptions.ApplySwipeOffset(fromX, fromY, toX, toY)
duration := 200
if actionOptions.PressDuration > 0 {

View File

@ -641,6 +641,9 @@ func (wd *WDADriver) Drag(fromX, fromY, toX, toY float64, opts ...option.ActionO
fromY = wd.toScale(fromY)
toX = wd.toScale(toX)
toY = wd.toScale(toY)
actionOptions := option.NewActionOptions(opts...)
fromX, fromY, toX, toY = actionOptions.ApplySwipeOffset(fromX, fromY, toX, toY)
data := map[string]interface{}{
"fromX": math.Round(fromX*10) / 10,
"fromY": math.Round(fromY*10) / 10,

View File

@ -146,6 +146,21 @@ func (o *ActionOptions) ApplyTapOffset(absX, absY float64) (float64, float64) {
return absX, absY
}
func (o *ActionOptions) ApplySwipeOffset(absFromX, absFromY, absToX, absToY float64) (
float64, float64, float64, float64) {
if len(o.SwipeOffset) == 4 {
absFromX += float64(o.SwipeOffset[0])
absFromY += float64(o.SwipeOffset[1])
absToX += float64(o.SwipeOffset[2])
absToY += float64(o.SwipeOffset[3])
}
absFromX += o.generateRandomOffset()
absFromY += o.generateRandomOffset()
absToX += o.generateRandomOffset()
absToY += o.generateRandomOffset()
return absFromX, absFromY, absToX, absToY
}
func (o *ActionOptions) generateRandomOffset() float64 {
if len(o.OffsetRandomRange) != 2 {
// invalid offset random range, should be [min, max]