mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-11 18:11:21 +08:00
refactor: use WithRelative to set relative coordinate, absolute by default
This commit is contained in:
BIN
pkg/uixt/1234.jpeg
Normal file
BIN
pkg/uixt/1234.jpeg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.3 MiB |
@@ -315,7 +315,7 @@ func (ad *ADBDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
|
||||
func (ad *ADBDriver) DoubleTap(x, y float64, opts ...option.ActionOption) error {
|
||||
var err error
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
if !actionOptions.AbsCoordinate {
|
||||
if actionOptions.Relative {
|
||||
x, y, err = convertToAbsolutePoint(ad, x, y)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -362,7 +362,7 @@ 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 !actionOptions.AbsCoordinate {
|
||||
if actionOptions.Relative {
|
||||
fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(ad, fromX, fromY, toX, toY)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -393,7 +393,7 @@ 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 {
|
||||
var err error
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
if !actionOptions.AbsCoordinate {
|
||||
if actionOptions.Relative {
|
||||
fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(ad, fromX, fromY, toX, toY)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -255,7 +255,7 @@ func (ud *UIA2Driver) Orientation() (orientation types.Orientation, err error) {
|
||||
func (ud *UIA2Driver) DoubleTap(x, y float64, opts ...option.ActionOption) error {
|
||||
var err error
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
if !actionOptions.AbsCoordinate {
|
||||
if actionOptions.Relative {
|
||||
x, y, err = convertToAbsolutePoint(ud, x, y)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -352,7 +352,7 @@ func (ud *UIA2Driver) TouchAndHold(x, y float64, opts ...option.ActionOption) (e
|
||||
func (ud *UIA2Driver) Drag(fromX, fromY, toX, toY float64, opts ...option.ActionOption) error {
|
||||
var err error
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
if !actionOptions.AbsCoordinate {
|
||||
if actionOptions.Relative {
|
||||
fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(ud, fromX, fromY, toX, toY)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -381,7 +381,7 @@ func (ud *UIA2Driver) Swipe(fromX, fromY, toX, toY float64, opts ...option.Actio
|
||||
// register(postHandler, new Swipe("/wd/hub/session/:sessionId/touch/perform"))
|
||||
var err error
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
if !actionOptions.AbsCoordinate {
|
||||
if actionOptions.Relative {
|
||||
fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(ud, fromX, fromY, toX, toY)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -52,7 +52,7 @@ type IDriver interface {
|
||||
// tap
|
||||
TapXY(x, y float64, opts ...option.ActionOption) error // by percentage
|
||||
TapAbsXY(x, y float64, opts ...option.ActionOption) error // by absolute coordinate
|
||||
DoubleTap(x, y float64, opts ...option.ActionOption) error // by percentage
|
||||
DoubleTap(x, y float64, opts ...option.ActionOption) error // by absolute coordinate
|
||||
TouchAndHold(x, y float64, opts ...option.ActionOption) error
|
||||
// swipe
|
||||
Drag(fromX, fromY, toX, toY float64, opts ...option.ActionOption) error
|
||||
|
||||
@@ -4,6 +4,7 @@ package uixt
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/ai"
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/option"
|
||||
@@ -82,6 +83,34 @@ func setupDriverExt(t *testing.T) *XTDriver {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDriverExt_FindScreenText(t *testing.T) {
|
||||
driver := setupDriverExt(t)
|
||||
point, err := driver.FindScreenText("首页")
|
||||
assert.Nil(t, err)
|
||||
t.Log(point)
|
||||
}
|
||||
|
||||
func TestDriverExt_Seek(t *testing.T) {
|
||||
driver := setupDriverExt(t)
|
||||
|
||||
point, err := driver.FindScreenText("首页")
|
||||
assert.Nil(t, err)
|
||||
|
||||
size, err := driver.WindowSize()
|
||||
assert.Nil(t, err)
|
||||
width := size.Width
|
||||
|
||||
y := point.Y - 40
|
||||
for i := 0; i < 5; i++ {
|
||||
err := driver.Swipe(0.5, 0.8, 0.5, 0.2)
|
||||
assert.Nil(t, err)
|
||||
time.Sleep(1 * time.Second)
|
||||
err = driver.Drag(20, y, float64(width)*0.75, y)
|
||||
assert.Nil(t, err)
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDriverExt_TapByOCR(t *testing.T) {
|
||||
driver := setupDriverExt(t)
|
||||
err := driver.TapByOCR("天气")
|
||||
|
||||
@@ -177,7 +177,7 @@ 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 {
|
||||
var err error
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
if !actionOptions.AbsCoordinate {
|
||||
if actionOptions.Relative {
|
||||
fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(hd, fromX, fromY, toX, toY)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -615,7 +615,7 @@ func (wd *WDADriver) DoubleTap(x, y float64, opts ...option.ActionOption) error
|
||||
// [[FBRoute POST:@"/wda/doubleTap"] respondWithTarget:self action:@selector(handleDoubleTapCoordinate:)]
|
||||
var err error
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
if !actionOptions.AbsCoordinate {
|
||||
if actionOptions.Relative {
|
||||
x, y, err = convertToAbsolutePoint(wd, x, y)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -648,7 +648,7 @@ func (wd *WDADriver) Drag(fromX, fromY, toX, toY float64, opts ...option.ActionO
|
||||
// [[FBRoute POST:@"/wda/dragfromtoforduration"] respondWithTarget:self action:@selector(handleDragCoordinate:)]
|
||||
var err error
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
if !actionOptions.AbsCoordinate {
|
||||
if actionOptions.Relative {
|
||||
fromX, fromY, toX, toY, err = convertToAbsoluteCoordinates(wd, fromX, fromY, toX, toY)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -19,7 +19,7 @@ type ActionOptions struct {
|
||||
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
|
||||
Frequency int `json:"frequency,omitempty" yaml:"frequency,omitempty"`
|
||||
AbsCoordinate bool `json:"abs,omitempty" yaml:"abs,omitempty"` // use absolute coordinate
|
||||
Relative bool `json:"relative,omitempty" yaml:"relative,omitempty"` // use relative coordinate
|
||||
|
||||
ScreenOptions
|
||||
|
||||
@@ -56,8 +56,8 @@ func (o *ActionOptions) Options() []ActionOption {
|
||||
if o.Steps != 0 {
|
||||
options = append(options, WithSteps(o.Steps))
|
||||
}
|
||||
if o.AbsCoordinate {
|
||||
options = append(options, WithAbsoluteCoordinate(true))
|
||||
if o.Relative {
|
||||
options = append(options, WithRelative(true))
|
||||
}
|
||||
|
||||
switch v := o.Direction.(type) {
|
||||
@@ -260,9 +260,10 @@ func WithDirection(direction string) ActionOption {
|
||||
}
|
||||
}
|
||||
|
||||
func WithAbsoluteCoordinate(abs bool) 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.AbsCoordinate = abs
|
||||
o.Relative = relative
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user