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
+1 -1
View File
@@ -1 +1 @@
v5.0.0+2502062139 v5.0.0+2502062225
+3 -40
View File
@@ -47,8 +47,6 @@ var (
//go:embed evalite //go:embed evalite
var evalite embed.FS var evalite embed.FS
const forwardToPrefix = "forward-to-"
func NewAndroidDevice(opts ...option.AndroidDeviceOption) (device *AndroidDevice, err error) { func NewAndroidDevice(opts ...option.AndroidDeviceOption) (device *AndroidDevice, err error) {
androidOptions := &option.AndroidDeviceConfig{ androidOptions := &option.AndroidDeviceConfig{
UIA2IP: UIA2ServerHost, UIA2IP: UIA2ServerHost,
@@ -179,7 +177,7 @@ func (dev *AndroidDevice) NewDriver(opts ...option.DriverOption) (driverExt *Dri
var driver IWebDriver var driver IWebDriver
if dev.UIA2 || dev.LogOn { if dev.UIA2 || dev.LogOn {
driver, err = dev.NewUSBDriver(options.Capabilities) driver, err = NewUIA2Driver(dev, opts...)
} else if dev.STUB { } else if dev.STUB {
driver, err = dev.NewStubDriver(options.Capabilities) driver, err = dev.NewStubDriver(options.Capabilities)
} else { } else {
@@ -204,28 +202,6 @@ func (dev *AndroidDevice) NewDriver(opts ...option.DriverOption) (driverExt *Dri
return driverExt, nil 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) { func (dev *AndroidDevice) NewStubDriver(capabilities option.Capabilities) (driver *StubAndroidDriver, err error) {
socketLocalPort, err := dev.d.Forward(StubSocketName) socketLocalPort, err := dev.d.Forward(StubSocketName)
if err != nil { if err != nil {
@@ -241,8 +217,8 @@ func (dev *AndroidDevice) NewStubDriver(capabilities option.Capabilities) (drive
serverLocalPort, DouyinServerPort, err)) serverLocalPort, DouyinServerPort, err))
} }
rawURL := fmt.Sprintf("http://%s%d:%d", rawURL := fmt.Sprintf("http://forward-to-%d:%d",
forwardToPrefix, serverLocalPort, DouyinServerPort) serverLocalPort, DouyinServerPort)
stubDriver, err := newStubAndroidDriver(fmt.Sprintf("127.0.0.1:%d", socketLocalPort), rawURL) stubDriver, err := newStubAndroidDriver(fmt.Sprintf("127.0.0.1:%d", socketLocalPort), rawURL)
if err != nil { if err != nil {
@@ -256,19 +232,6 @@ func (dev *AndroidDevice) NewStubDriver(capabilities option.Capabilities) (drive
return stubDriver, nil 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 { func (dev *AndroidDevice) StartPerf() error {
// TODO // TODO
return nil return nil
+20 -27
View File
@@ -8,55 +8,49 @@ import (
"fmt" "fmt"
"net" "net"
"net/http" "net/http"
"net/url"
"strconv"
"strings" "strings"
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v5/code"
"github.com/httprunner/httprunner/v5/internal/utf7" "github.com/httprunner/httprunner/v5/internal/utf7"
"github.com/httprunner/httprunner/v5/pkg/uixt/option" "github.com/httprunner/httprunner/v5/pkg/uixt/option"
) )
var errDriverNotImplemented = errors.New("driver method not implemented") var errDriverNotImplemented = errors.New("driver method not implemented")
type UIA2Driver struct { const forwardToPrefix = "forward-to-"
ADBDriver
}
func NewUIADriver(capabilities option.Capabilities, urlPrefix string) (driver *UIA2Driver, err error) { func NewUIA2Driver(device *AndroidDevice, opts ...option.DriverOption) (*UIA2Driver, error) {
log.Info().Msg("init uiautomator2 driver") log.Info().Interface("device", device).Msg("init android UIA2 driver")
if capabilities == nil { localPort, err := device.d.Forward(device.UIA2Port)
capabilities = option.NewCapabilities() if err != nil {
capabilities.WithWaitForIdleTimeout(0) return nil, errors.Wrap(code.DeviceConnectionError,
} fmt.Sprintf("forward port %d->%d failed: %v",
driver = new(UIA2Driver) localPort, device.UIA2Port, err))
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))
}
} }
conn, err := net.Dial("tcp", fmt.Sprintf(":%d", localPort)) conn, err := net.Dial("tcp", fmt.Sprintf(":%d", localPort))
if err != nil { if err != nil {
return nil, fmt.Errorf("adb forward: %w", err) return nil, fmt.Errorf("adb forward: %w", err)
} }
driver := new(UIA2Driver)
driver.client = convertToHTTPClient(conn) driver.client = convertToHTTPClient(conn)
driver.adbClient = device.d
driver.logcat = device.logcat
_, err = driver.NewSession(capabilities) _, err = driver.NewSession(nil)
if err != 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 return driver, nil
} }
type UIA2Driver struct {
ADBDriver
}
type BatteryStatus int type BatteryStatus int
const ( const (
@@ -86,12 +80,11 @@ func (bs BatteryStatus) String() string {
} }
func (ud *UIA2Driver) resetDriver() error { func (ud *UIA2Driver) resetDriver() error {
newUIADriver, err := NewUIADriver(option.NewCapabilities(), ud.urlPrefix.String()) session, err := ud.NewSession(option.NewCapabilities())
if err != nil { if err != nil {
return err return err
} }
ud.client = newUIADriver.client ud.session.ID = session.SessionId
ud.session.ID = newUIADriver.session.ID
return nil return nil
} }
+7 -101
View File
@@ -12,10 +12,7 @@ import (
"github.com/httprunner/httprunner/v5/pkg/uixt/option" "github.com/httprunner/httprunner/v5/pkg/uixt/option"
) )
var ( var driverExt *DriverExt
uiaServerURL = "http://forward-to-6790:6790/wd/hub"
driverExt *DriverExt
)
func setupAndroidAdbDriver(t *testing.T) { func setupAndroidAdbDriver(t *testing.T) {
device, err := NewAndroidDevice() device, err := NewAndroidDevice()
@@ -51,66 +48,20 @@ func TestAndroidDevice_GetCurrentWindow(t *testing.T) {
t.Logf("packageName: %s\tactivityName: %s", windowInfo.PackageName, windowInfo.Activity) 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) { func TestDriver_Quit(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL) if err := driver.DeleteSession(); err != nil {
if err != nil {
t.Fatal(err)
}
if err = driver.DeleteSession(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
func TestDriver_Status(t *testing.T) { func TestDriver_Status(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL) _, err := driver.Status()
if err != nil {
t.Fatal(err)
}
_, err = driver.Status()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
func TestDriver_Screenshot(t *testing.T) { func TestDriver_Screenshot(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
screenshot, err := driver.Screenshot() screenshot, err := driver.Screenshot()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -120,11 +71,6 @@ func TestDriver_Screenshot(t *testing.T) {
} }
func TestDriver_Rotation(t *testing.T) { func TestDriver_Rotation(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
rotation, err := driver.Rotation() rotation, err := driver.Rotation()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -134,11 +80,6 @@ func TestDriver_Rotation(t *testing.T) {
} }
func TestDriver_DeviceSize(t *testing.T) { func TestDriver_DeviceSize(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
deviceSize, err := driver.WindowSize() deviceSize, err := driver.WindowSize()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -159,23 +100,13 @@ func TestDriver_Source(t *testing.T) {
} }
func TestDriver_TapByText(t *testing.T) { func TestDriver_TapByText(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL) err := driver.TapByText("安装")
if err != nil {
t.Fatal(err)
}
err = driver.TapByText("安装")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
func TestDriver_BatteryInfo(t *testing.T) { func TestDriver_BatteryInfo(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
batteryInfo, err := driver.BatteryInfo() batteryInfo, err := driver.BatteryInfo()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -185,11 +116,6 @@ func TestDriver_BatteryInfo(t *testing.T) {
} }
func TestDriver_GetAppiumSettings(t *testing.T) { func TestDriver_GetAppiumSettings(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
appiumSettings, err := driver.GetAppiumSettings() appiumSettings, err := driver.GetAppiumSettings()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -202,11 +128,6 @@ func TestDriver_GetAppiumSettings(t *testing.T) {
} }
func TestDriver_DeviceInfo(t *testing.T) { func TestDriver_DeviceInfo(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL)
if err != nil {
t.Fatal(err)
}
devInfo, err := driver.DeviceInfo() devInfo, err := driver.DeviceInfo()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -255,12 +176,7 @@ func TestDriver_Swipe_Relative(t *testing.T) {
} }
func TestDriver_Drag(t *testing.T) { func TestDriver_Drag(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL) err := driver.Drag(400, 260, 400, 500)
if err != nil {
t.Fatal(err)
}
err = driver.Drag(400, 260, 400, 500)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -298,25 +214,15 @@ func TestDriver_SendKeys(t *testing.T) {
} }
func TestDriver_PressBack(t *testing.T) { func TestDriver_PressBack(t *testing.T) {
driver, err := NewUIADriver(nil, uiaServerURL) err := driver.PressBack()
if err != nil {
t.Fatal(err)
}
err = driver.PressBack()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
func TestDriver_SetRotation(t *testing.T) { 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: 0})
err = driver.SetRotation(Rotation{Z: 270}) err := driver.SetRotation(Rotation{Z: 270})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
+3
View File
@@ -89,6 +89,9 @@ type IWebDriver interface {
Orientation() (orientation Orientation, err error) Orientation() (orientation Orientation, err error)
SetRotation(rotation Rotation) (err error)
Rotation() (rotation Rotation, err error)
// Tap Sends a tap event at the coordinate. // Tap Sends a tap event at the coordinate.
Tap(x, y float64, opts ...option.ActionOption) error Tap(x, y float64, opts ...option.ActionOption) error
+10
View File
@@ -335,3 +335,13 @@ func (hd *hdcDriver) RecordScreen(folderPath string, duration time.Duration) (vi
func (hd *hdcDriver) TearDown() error { func (hd *hdcDriver) TearDown() error {
return nil return nil
} }
func (hd *hdcDriver) Rotation() (rotation Rotation, err error) {
err = errDriverNotImplemented
return
}
func (hd *hdcDriver) SetRotation(rotation Rotation) (err error) {
err = errDriverNotImplemented
return
}