mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-10 17:43:00 +08:00
refactor: driver TapXY, TapAbsXY
This commit is contained in:
@@ -283,15 +283,14 @@ func (ad *ADBDriver) AppTerminate(packageName string) (successful bool, err erro
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) TapXY(x, y float64, opts ...option.ActionOption) error {
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
|
||||
if len(actionOptions.Offset) == 2 {
|
||||
x += float64(actionOptions.Offset[0])
|
||||
y += float64(actionOptions.Offset[1])
|
||||
absX, absY, err := convertToAbsolutePoint(ad, x, y)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
x += actionOptions.GetRandomOffset()
|
||||
y += actionOptions.GetRandomOffset()
|
||||
return ad.TapAbsXY(absX, absY, opts...)
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
|
||||
// adb shell input tap x y
|
||||
xStr := fmt.Sprintf("%.1f", x)
|
||||
yStr := fmt.Sprintf("%.1f", y)
|
||||
|
||||
@@ -263,17 +263,19 @@ func (ud *UIA2Driver) DoubleFloatTap(x, y float64) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (ud *UIA2Driver) TapXY(x, y float64, opts ...option.ActionOption) (err error) {
|
||||
func (ud *UIA2Driver) TapXY(x, y float64, opts ...option.ActionOption) error {
|
||||
// register(postHandler, new Tap("/wd/hub/session/:sessionId/appium/tap"))
|
||||
absX, absY, err := convertToAbsolutePoint(ud, x, y)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ud.TapAbsXY(absX, absY, opts...)
|
||||
}
|
||||
|
||||
func (ud *UIA2Driver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
|
||||
// register(postHandler, new Tap("/wd/hub/session/:sessionId/appium/tap"))
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
|
||||
if len(actionOptions.Offset) == 2 {
|
||||
x += float64(actionOptions.Offset[0])
|
||||
y += float64(actionOptions.Offset[1])
|
||||
}
|
||||
x += actionOptions.GetRandomOffset()
|
||||
y += actionOptions.GetRandomOffset()
|
||||
|
||||
duration := 100.0
|
||||
if actionOptions.PressDuration > 0 {
|
||||
duration = actionOptions.PressDuration * 1000
|
||||
@@ -297,7 +299,7 @@ func (ud *UIA2Driver) TapXY(x, y float64, opts ...option.ActionOption) (err erro
|
||||
// update data options in post data for extra uiautomator configurations
|
||||
actionOptions.UpdateData(data)
|
||||
|
||||
_, err = ud.httpPOST(data, "/session", ud.Session.ID, "actions/tap")
|
||||
_, err := ud.httpPOST(data, "/session", ud.Session.ID, "actions/tap")
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -58,7 +58,8 @@ type IDriver interface {
|
||||
Unlock() error
|
||||
Back() error
|
||||
// tap
|
||||
TapXY(x, y float64, opts ...option.ActionOption) error
|
||||
TapXY(x, y float64, opts ...option.ActionOption) error // tap on [x, y] percent of window size
|
||||
TapAbsXY(x, y float64, opts ...option.ActionOption) error
|
||||
DoubleTapXY(x, y float64, opts ...option.ActionOption) error
|
||||
TouchAndHold(x, y float64, opts ...option.ActionOption) error
|
||||
TapByText(text string, opts ...option.ActionOption) error // TODO: remove
|
||||
|
||||
@@ -9,30 +9,6 @@ import (
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/option"
|
||||
)
|
||||
|
||||
func (dExt *XTDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
|
||||
// tap on absolute coordinate [x, y]
|
||||
err := dExt.TapXY(x, y, opts...)
|
||||
if err != nil {
|
||||
return errors.Wrap(code.MobileUITapError, err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dExt *XTDriver) TapXY(x, y float64, opts ...option.ActionOption) error {
|
||||
// tap on [x, y] percent of window size
|
||||
if x > 1 || y > 1 {
|
||||
return fmt.Errorf("x, y percentage should be <= 1, got x=%v, y=%v", x, y)
|
||||
}
|
||||
|
||||
windowSize, err := dExt.WindowSize()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
x = x * float64(windowSize.Width)
|
||||
y = y * float64(windowSize.Height)
|
||||
return dExt.TapAbsXY(x, y, opts...)
|
||||
}
|
||||
|
||||
func (dExt *XTDriver) TapByOCR(ocrText string, opts ...option.ActionOption) error {
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
if actionOptions.ScreenShotFileName == "" {
|
||||
|
||||
@@ -26,4 +26,5 @@ func TestNewDriverExt(t *testing.T) {
|
||||
|
||||
// get device
|
||||
device = driver.GetDevice().(*AndroidDevice)
|
||||
t.Log(device)
|
||||
}
|
||||
|
||||
@@ -8,11 +8,32 @@ import (
|
||||
"github.com/httprunner/httprunner/v5/code"
|
||||
)
|
||||
|
||||
func convertToAbsolutePoint(driver IDriver, x, y float64) (absX, absY float64, err error) {
|
||||
if !assertRelative(x) || !assertRelative(y) {
|
||||
err = errors.Wrap(code.InvalidCaseError,
|
||||
fmt.Sprintf("x(%f), y(%f) must be less than 1", x, y))
|
||||
return
|
||||
}
|
||||
|
||||
windowSize, err := driver.WindowSize()
|
||||
if err != nil {
|
||||
err = errors.Wrap(code.DeviceGetInfoError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
absX = float64(windowSize.Width) * x
|
||||
absY = float64(windowSize.Height) * y
|
||||
return
|
||||
}
|
||||
|
||||
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 {
|
||||
if !assertRelative(fromX) || !assertRelative(fromY) ||
|
||||
!assertRelative(toX) || !assertRelative(toY) {
|
||||
err = errors.Wrap(code.InvalidCaseError,
|
||||
fmt.Sprintf("fromX(%f), fromY(%f), toX(%f), toY(%f) must be less than 1",
|
||||
fromX, fromY, toX, toY))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -32,16 +53,6 @@ func convertToAbsoluteCoordinates(driver IDriver, fromX, fromY, toX, toY float64
|
||||
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
|
||||
}
|
||||
|
||||
@@ -138,20 +138,22 @@ func (hd *HDCDriver) Orientation() (orientation types.Orientation, err error) {
|
||||
}
|
||||
|
||||
func (hd *HDCDriver) TapXY(x, y float64, opts ...option.ActionOption) error {
|
||||
absX, absY, err := convertToAbsolutePoint(hd, x, y)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return hd.TapAbsXY(absX, absY, opts...)
|
||||
}
|
||||
|
||||
func (hd *HDCDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
|
||||
if len(actionOptions.Offset) == 2 {
|
||||
x += float64(actionOptions.Offset[0])
|
||||
y += float64(actionOptions.Offset[1])
|
||||
}
|
||||
|
||||
x += actionOptions.GetRandomOffset()
|
||||
y += actionOptions.GetRandomOffset()
|
||||
if actionOptions.Identifier != "" {
|
||||
startTime := int(time.Now().UnixMilli())
|
||||
hd.points = append(hd.points, ExportPoint{Start: startTime, End: startTime + 100, Ext: actionOptions.Identifier, RunTime: 100})
|
||||
}
|
||||
return hd.uiDriver.InjectGesture(ghdc.NewGesture().Start(ghdc.Point{X: int(x), Y: int(y)}).Pause(100))
|
||||
return hd.uiDriver.InjectGesture(
|
||||
ghdc.NewGesture().Start(ghdc.Point{X: int(x), Y: int(y)}).Pause(100))
|
||||
}
|
||||
|
||||
func (hd *HDCDriver) DoubleTapXY(x, y float64, opts ...option.ActionOption) error {
|
||||
|
||||
@@ -514,28 +514,23 @@ func (wd *WDADriver) ForegroundInfo() (appInfo types.AppInfo, err error) {
|
||||
return appInfo, err
|
||||
}
|
||||
|
||||
func (wd *WDADriver) TapXY(x, y float64, opts ...option.ActionOption) (err error) {
|
||||
func (wd *WDADriver) TapXY(x, y float64, opts ...option.ActionOption) error {
|
||||
// [[FBRoute POST:@"/wda/tap/:uuid"] respondWithTarget:self action:@selector(handleTap:)]
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
|
||||
x = wd.toScale(x)
|
||||
y = wd.toScale(y)
|
||||
if len(actionOptions.Offset) == 2 {
|
||||
x += float64(actionOptions.Offset[0])
|
||||
y += float64(actionOptions.Offset[1])
|
||||
absX, absY, err := convertToAbsolutePoint(wd, x, y)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
x += actionOptions.GetRandomOffset()
|
||||
y += actionOptions.GetRandomOffset()
|
||||
return wd.TapAbsXY(absX, absY, opts...)
|
||||
}
|
||||
|
||||
func (wd *WDADriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
|
||||
// [[FBRoute POST:@"/wda/tap/:uuid"] respondWithTarget:self action:@selector(handleTap:)]
|
||||
data := map[string]interface{}{
|
||||
"x": x,
|
||||
"y": y,
|
||||
"x": wd.toScale(x),
|
||||
"y": wd.toScale(y),
|
||||
}
|
||||
// update data options in post data for extra WDA configurations
|
||||
actionOptions.UpdateData(data)
|
||||
|
||||
_, err = wd.httpPOST(data, "/session", wd.Session.ID, "/wda/tap/0")
|
||||
return
|
||||
_, err := wd.httpPOST(data, "/session", wd.Session.ID, "/wda/tap/0")
|
||||
return err
|
||||
}
|
||||
|
||||
func (wd *WDADriver) DoubleTapXY(x, y float64, opts ...option.ActionOption) (err error) {
|
||||
|
||||
Reference in New Issue
Block a user