refactor: NewUIA2Driver

This commit is contained in:
lilong.129
2025-02-06 22:25:32 +08:00
parent f87d37d5c6
commit be2cb59452
6 changed files with 44 additions and 169 deletions

View File

@@ -47,8 +47,6 @@ var (
//go:embed evalite
var evalite embed.FS
const forwardToPrefix = "forward-to-"
func NewAndroidDevice(opts ...option.AndroidDeviceOption) (device *AndroidDevice, err error) {
androidOptions := &option.AndroidDeviceConfig{
UIA2IP: UIA2ServerHost,
@@ -179,7 +177,7 @@ func (dev *AndroidDevice) NewDriver(opts ...option.DriverOption) (driverExt *Dri
var driver IWebDriver
if dev.UIA2 || dev.LogOn {
driver, err = dev.NewUSBDriver(options.Capabilities)
driver, err = NewUIA2Driver(dev, opts...)
} else if dev.STUB {
driver, err = dev.NewStubDriver(options.Capabilities)
} else {
@@ -204,28 +202,6 @@ func (dev *AndroidDevice) NewDriver(opts ...option.DriverOption) (driverExt *Dri
return driverExt, nil
}
// NewUSBDriver creates new client via USB connected device, this will also start a new session.
func (dev *AndroidDevice) NewUSBDriver(capabilities option.Capabilities) (driver IWebDriver, err error) {
localPort, err := dev.d.Forward(dev.UIA2Port)
if err != nil {
return nil, errors.Wrap(code.DeviceConnectionError,
fmt.Sprintf("forward port %d->%d failed: %v",
localPort, dev.UIA2Port, err))
}
rawURL := fmt.Sprintf("http://%s%d:%d/wd/hub",
forwardToPrefix, localPort, dev.UIA2Port)
uiaDriver, err := NewUIADriver(capabilities, rawURL)
if err != nil {
_ = dev.d.ForwardKill(localPort)
return nil, errors.Wrap(code.DeviceConnectionError, err.Error())
}
uiaDriver.adbClient = dev.d
uiaDriver.logcat = dev.logcat
return uiaDriver, nil
}
func (dev *AndroidDevice) NewStubDriver(capabilities option.Capabilities) (driver *StubAndroidDriver, err error) {
socketLocalPort, err := dev.d.Forward(StubSocketName)
if err != nil {
@@ -241,8 +217,8 @@ func (dev *AndroidDevice) NewStubDriver(capabilities option.Capabilities) (drive
serverLocalPort, DouyinServerPort, err))
}
rawURL := fmt.Sprintf("http://%s%d:%d",
forwardToPrefix, serverLocalPort, DouyinServerPort)
rawURL := fmt.Sprintf("http://forward-to-%d:%d",
serverLocalPort, DouyinServerPort)
stubDriver, err := newStubAndroidDriver(fmt.Sprintf("127.0.0.1:%d", socketLocalPort), rawURL)
if err != nil {
@@ -256,19 +232,6 @@ func (dev *AndroidDevice) NewStubDriver(capabilities option.Capabilities) (drive
return stubDriver, nil
}
// NewHTTPDriver creates new remote HTTP client, this will also start a new session.
func (dev *AndroidDevice) NewHTTPDriver(capabilities option.Capabilities) (driver IWebDriver, err error) {
rawURL := fmt.Sprintf("http://%s:%d/wd/hub", dev.UIA2IP, dev.UIA2Port)
uiaDriver, err := NewUIADriver(capabilities, rawURL)
if err != nil {
return nil, err
}
uiaDriver.adbClient = dev.d
uiaDriver.logcat = dev.logcat
return uiaDriver, nil
}
func (dev *AndroidDevice) StartPerf() error {
// TODO
return nil

View File

@@ -8,55 +8,49 @@ import (
"fmt"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v5/code"
"github.com/httprunner/httprunner/v5/internal/utf7"
"github.com/httprunner/httprunner/v5/pkg/uixt/option"
)
var errDriverNotImplemented = errors.New("driver method not implemented")
type UIA2Driver struct {
ADBDriver
}
const forwardToPrefix = "forward-to-"
func NewUIADriver(capabilities option.Capabilities, urlPrefix string) (driver *UIA2Driver, err error) {
log.Info().Msg("init uiautomator2 driver")
if capabilities == nil {
capabilities = option.NewCapabilities()
capabilities.WithWaitForIdleTimeout(0)
}
driver = new(UIA2Driver)
if driver.urlPrefix, err = url.Parse(urlPrefix); err != nil {
return nil, err
}
var localPort int
{
tmpURL, _ := url.Parse(driver.urlPrefix.String())
hostname := tmpURL.Hostname()
if strings.HasPrefix(hostname, forwardToPrefix) {
localPort, _ = strconv.Atoi(strings.TrimPrefix(hostname, forwardToPrefix))
}
func NewUIA2Driver(device *AndroidDevice, opts ...option.DriverOption) (*UIA2Driver, error) {
log.Info().Interface("device", device).Msg("init android UIA2 driver")
localPort, err := device.d.Forward(device.UIA2Port)
if err != nil {
return nil, errors.Wrap(code.DeviceConnectionError,
fmt.Sprintf("forward port %d->%d failed: %v",
localPort, device.UIA2Port, err))
}
conn, err := net.Dial("tcp", fmt.Sprintf(":%d", localPort))
if err != nil {
return nil, fmt.Errorf("adb forward: %w", err)
}
driver := new(UIA2Driver)
driver.client = convertToHTTPClient(conn)
driver.adbClient = device.d
driver.logcat = device.logcat
_, err = driver.NewSession(capabilities)
_, err = driver.NewSession(nil)
if err != nil {
return nil, errors.Wrap(err, "create UIAutomator session failed")
return nil, errors.Wrap(err, "create UIA2 session failed")
}
return driver, nil
}
type UIA2Driver struct {
ADBDriver
}
type BatteryStatus int
const (
@@ -86,12 +80,11 @@ func (bs BatteryStatus) String() string {
}
func (ud *UIA2Driver) resetDriver() error {
newUIADriver, err := NewUIADriver(option.NewCapabilities(), ud.urlPrefix.String())
session, err := ud.NewSession(option.NewCapabilities())
if err != nil {
return err
}
ud.client = newUIADriver.client
ud.session.ID = newUIADriver.session.ID
ud.session.ID = session.SessionId
return nil
}

View File

@@ -12,10 +12,7 @@ import (
"github.com/httprunner/httprunner/v5/pkg/uixt/option"
)
var (
uiaServerURL = "http://forward-to-6790:6790/wd/hub"
driverExt *DriverExt
)
var driverExt *DriverExt
func setupAndroidAdbDriver(t *testing.T) {
device, err := NewAndroidDevice()
@@ -51,66 +48,20 @@ func TestAndroidDevice_GetCurrentWindow(t *testing.T) {
t.Logf("packageName: %s\tactivityName: %s", windowInfo.PackageName, windowInfo.Activity)
}
func TestDriver_NewSession(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
firstMatchEntry := make(map[string]interface{})
firstMatchEntry["package"] = "com.android.settings"
firstMatchEntry["activity"] = "com.android.settings/.Settings"
caps := option.Capabilities{
"firstMatch": []interface{}{firstMatchEntry},
"alwaysMatch": struct{}{},
}
session, err := driver.NewSession(caps)
if err != nil {
t.Fatal(err)
}
if len(session.SessionId) == 0 {
t.Fatal("should not be empty")
}
}
func TestNewDriver(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
t.Log(driver.session.ID)
}
func TestDriver_Quit(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
if err = driver.DeleteSession(); err != nil {
if err := driver.DeleteSession(); err != nil {
t.Fatal(err)
}
}
func TestDriver_Status(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
_, err = driver.Status()
_, err := driver.Status()
if err != nil {
t.Fatal(err)
}
}
func TestDriver_Screenshot(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
screenshot, err := driver.Screenshot()
if err != nil {
t.Fatal(err)
@@ -120,11 +71,6 @@ func TestDriver_Screenshot(t *testing.T) {
}
func TestDriver_Rotation(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
rotation, err := driver.Rotation()
if err != nil {
t.Fatal(err)
@@ -134,11 +80,6 @@ func TestDriver_Rotation(t *testing.T) {
}
func TestDriver_DeviceSize(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
deviceSize, err := driver.WindowSize()
if err != nil {
t.Fatal(err)
@@ -159,23 +100,13 @@ func TestDriver_Source(t *testing.T) {
}
func TestDriver_TapByText(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
err = driver.TapByText("安装")
err := driver.TapByText("安装")
if err != nil {
t.Fatal(err)
}
}
func TestDriver_BatteryInfo(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
batteryInfo, err := driver.BatteryInfo()
if err != nil {
t.Fatal(err)
@@ -185,11 +116,6 @@ func TestDriver_BatteryInfo(t *testing.T) {
}
func TestDriver_GetAppiumSettings(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
appiumSettings, err := driver.GetAppiumSettings()
if err != nil {
t.Fatal(err)
@@ -202,11 +128,6 @@ func TestDriver_GetAppiumSettings(t *testing.T) {
}
func TestDriver_DeviceInfo(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
devInfo, err := driver.DeviceInfo()
if err != nil {
t.Fatal(err)
@@ -255,12 +176,7 @@ func TestDriver_Swipe_Relative(t *testing.T) {
}
func TestDriver_Drag(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
err = driver.Drag(400, 260, 400, 500)
err := driver.Drag(400, 260, 400, 500)
if err != nil {
t.Fatal(err)
}
@@ -298,25 +214,15 @@ func TestDriver_SendKeys(t *testing.T) {
}
func TestDriver_PressBack(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
err = driver.PressBack()
err := driver.PressBack()
if err != nil {
t.Fatal(err)
}
}
func TestDriver_SetRotation(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
// err = driver.SetRotation(Rotation{Z: 0})
err = driver.SetRotation(Rotation{Z: 270})
err := driver.SetRotation(Rotation{Z: 270})
if err != nil {
t.Fatal(err)
}

View File

@@ -89,6 +89,9 @@ type IWebDriver interface {
Orientation() (orientation Orientation, err error)
SetRotation(rotation Rotation) (err error)
Rotation() (rotation Rotation, err error)
// Tap Sends a tap event at the coordinate.
Tap(x, y float64, opts ...option.ActionOption) error

View File

@@ -335,3 +335,13 @@ func (hd *hdcDriver) RecordScreen(folderPath string, duration time.Duration) (vi
func (hd *hdcDriver) TearDown() error {
return nil
}
func (hd *hdcDriver) Rotation() (rotation Rotation, err error) {
err = errDriverNotImplemented
return
}
func (hd *hdcDriver) SetRotation(rotation Rotation) (err error) {
err = errDriverNotImplemented
return
}