From 08cb50b698097c7869fa3991ee5dc2e829f79fa8 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Mon, 2 Sep 2024 14:11:06 +0800 Subject: [PATCH] refactor: rename IDevice, IWebDriver --- examples/worldcup/cli.go | 2 +- examples/worldcup/main.go | 6 +++--- hrp/pkg/uixt/android_device.go | 8 ++++---- hrp/pkg/uixt/client.go | 6 ------ hrp/pkg/uixt/ext.go | 6 +++--- hrp/pkg/uixt/interface.go | 8 ++++---- hrp/pkg/uixt/ios_device.go | 6 +++--- hrp/pkg/uixt/ios_test.go | 2 +- hrp/step_mobile_ui.go | 2 +- 9 files changed, 20 insertions(+), 26 deletions(-) diff --git a/examples/worldcup/cli.go b/examples/worldcup/cli.go index 0950f63d..fb822814 100644 --- a/examples/worldcup/cli.go +++ b/examples/worldcup/cli.go @@ -15,7 +15,7 @@ var rootCmd = &cobra.Command{ Short: "Monitor FIFA World Cup Live", Version: "2022.12.03.0018", RunE: func(cmd *cobra.Command, args []string) error { - var device uixt.Device + var device uixt.IDevice var bundleID string if iosApp != "" { log.Info().Str("bundleID", iosApp).Msg("init ios device") diff --git a/examples/worldcup/main.go b/examples/worldcup/main.go index f626bf62..eddbfbef 100644 --- a/examples/worldcup/main.go +++ b/examples/worldcup/main.go @@ -36,7 +36,7 @@ func convertTimeToSeconds(timeStr string) (int, error) { return seconds, nil } -func initIOSDevice(uuid string) uixt.Device { +func initIOSDevice(uuid string) uixt.IDevice { perfOptions := []uixt.IOSPerfOption{} for _, p := range perf { switch p { @@ -71,7 +71,7 @@ func initIOSDevice(uuid string) uixt.Device { return device } -func initAndroidDevice(uuid string) uixt.Device { +func initAndroidDevice(uuid string) uixt.IDevice { device, err := uixt.NewAndroidDevice(uixt.WithSerialNumber(uuid)) if err != nil { log.Fatal().Err(err).Msg("failed to init android device") @@ -101,7 +101,7 @@ type WorldCupLive struct { PerfFile string `json:"perf"` } -func NewWorldCupLive(device uixt.Device, matchName, bundleID string, duration, interval int) *WorldCupLive { +func NewWorldCupLive(device uixt.IDevice, matchName, bundleID string, duration, interval int) *WorldCupLive { driverExt, err := device.NewDriver() if err != nil { log.Fatal().Err(err).Msg("failed to init driver") diff --git a/hrp/pkg/uixt/android_device.go b/hrp/pkg/uixt/android_device.go index 8a1eb038..c0008665 100644 --- a/hrp/pkg/uixt/android_device.go +++ b/hrp/pkg/uixt/android_device.go @@ -209,7 +209,7 @@ func (dev *AndroidDevice) NewDriver(options ...DriverOption) (driverExt *DriverE option(driverOptions) } - var driver WebDriver + var driver IWebDriver if dev.UIA2 || dev.LogOn { driver, err = dev.NewUSBDriver(driverOptions.capabilities) } else if dev.STUB { @@ -237,7 +237,7 @@ func (dev *AndroidDevice) NewDriver(options ...DriverOption) (driverExt *DriverE } // NewUSBDriver creates new client via USB connected device, this will also start a new session. -func (dev *AndroidDevice) NewUSBDriver(capabilities Capabilities) (driver WebDriver, err error) { +func (dev *AndroidDevice) NewUSBDriver(capabilities Capabilities) (driver IWebDriver, err error) { localPort, err := dev.d.Forward(UIA2ServerPort) if err != nil { return nil, errors.Wrap(code.AndroidDeviceConnectionError, @@ -289,7 +289,7 @@ func (dev *AndroidDevice) NewStubDriver(capabilities Capabilities) (driver *stub } // NewHTTPDriver creates new remote HTTP client, this will also start a new session. -func (dev *AndroidDevice) NewHTTPDriver(capabilities Capabilities) (driver WebDriver, err error) { +func (dev *AndroidDevice) NewHTTPDriver(capabilities 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 { @@ -301,7 +301,7 @@ func (dev *AndroidDevice) NewHTTPDriver(capabilities Capabilities) (driver WebDr return uiaDriver, nil } -func (dev *AndroidDevice) NewAdbDriver() (driver WebDriver, err error) { +func (dev *AndroidDevice) NewAdbDriver() (driver IWebDriver, err error) { adbDriver := NewAdbDriver() adbDriver.adbClient = dev.d adbDriver.logcat = dev.logcat diff --git a/hrp/pkg/uixt/client.go b/hrp/pkg/uixt/client.go index 632761f7..44d8078f 100644 --- a/hrp/pkg/uixt/client.go +++ b/hrp/pkg/uixt/client.go @@ -25,16 +25,10 @@ type DriverSession struct { } func (d *DriverSession) addScreenResult(screenResult *ScreenResult) { - if screenResult == nil { - return - } d.screenResults[screenResult.imagePath] = screenResult } func (d *DriverSession) addRequestResult(driverResult *DriverResult) { - if driverResult == nil { - return - } d.requests = append(d.requests, driverResult) } diff --git a/hrp/pkg/uixt/ext.go b/hrp/pkg/uixt/ext.go index 7ec335ce..79aac2f9 100644 --- a/hrp/pkg/uixt/ext.go +++ b/hrp/pkg/uixt/ext.go @@ -13,15 +13,15 @@ import ( ) type DriverExt struct { - Device Device - Driver WebDriver + Device IDevice + Driver IWebDriver ImageService IImageService // used to extract image data // funplugin plugin funplugin.IPlugin } -func newDriverExt(device Device, driver WebDriver, options ...DriverOption) (dExt *DriverExt, err error) { +func newDriverExt(device IDevice, driver IWebDriver, options ...DriverOption) (dExt *DriverExt, err error) { driverOptions := NewDriverOptions() for _, option := range options { option(driverOptions) diff --git a/hrp/pkg/uixt/interface.go b/hrp/pkg/uixt/interface.go index 5214afd3..c9440fca 100644 --- a/hrp/pkg/uixt/interface.go +++ b/hrp/pkg/uixt/interface.go @@ -396,7 +396,7 @@ func (opt SourceOption) WithExcludedAttributes(attributes []string) SourceOption return opt } -type Condition func(wd WebDriver) (bool, error) +type Condition func(wd IWebDriver) (bool, error) type Direction string @@ -475,7 +475,7 @@ func WithDriverPlugin(plugin funplugin.IPlugin) DriverOption { } // current implemeted device: IOSDevice, AndroidDevice -type Device interface { +type IDevice interface { Init() error // init android device UUID() string // ios udid or android serial LogEnabled() bool @@ -497,8 +497,8 @@ type ForegroundApp struct { Activity string } -// WebDriver defines methods supported by WebDriver drivers. -type WebDriver interface { +// IWebDriver defines methods supported by IWebDriver drivers. +type IWebDriver interface { // NewSession starts a new session and returns the SessionInfo. NewSession(capabilities Capabilities) (SessionInfo, error) diff --git a/hrp/pkg/uixt/ios_device.go b/hrp/pkg/uixt/ios_device.go index a8aef4cb..3aa092f8 100644 --- a/hrp/pkg/uixt/ios_device.go +++ b/hrp/pkg/uixt/ios_device.go @@ -320,7 +320,7 @@ func (dev *IOSDevice) NewDriver(options ...DriverOption) (driverExt *DriverExt, capabilities.WithDefaultAlertAction(AlertActionAccept) } - var driver WebDriver + var driver IWebDriver if env.WDA_USB_DRIVER == "" { // default use http driver driver, err = dev.NewHTTPDriver(capabilities) @@ -608,7 +608,7 @@ func (dev *IOSDevice) pcapOpitons() (pcapOptions []gidevice.PcapOption) { } // NewHTTPDriver creates new remote HTTP client, this will also start a new session. -func (dev *IOSDevice) NewHTTPDriver(capabilities Capabilities) (driver WebDriver, err error) { +func (dev *IOSDevice) NewHTTPDriver(capabilities Capabilities) (driver IWebDriver, err error) { var localPort int localPort, err = strconv.Atoi(env.WDA_LOCAL_PORT) if err != nil { @@ -677,7 +677,7 @@ func (dev *IOSDevice) NewHTTPDriver(capabilities Capabilities) (driver WebDriver } // NewUSBDriver creates new client via USB connected device, this will also start a new session. -func (dev *IOSDevice) NewUSBDriver(capabilities Capabilities) (driver WebDriver, err error) { +func (dev *IOSDevice) NewUSBDriver(capabilities Capabilities) (driver IWebDriver, err error) { log.Info().Interface("capabilities", capabilities). Str("udid", dev.UDID).Msg("init WDA USB driver") diff --git a/hrp/pkg/uixt/ios_test.go b/hrp/pkg/uixt/ios_test.go index f26775dc..0ee94d36 100644 --- a/hrp/pkg/uixt/ios_test.go +++ b/hrp/pkg/uixt/ios_test.go @@ -13,7 +13,7 @@ import ( var ( bundleId = "com.apple.Preferences" - driver WebDriver + driver IWebDriver iOSDriverExt *DriverExt ) diff --git a/hrp/step_mobile_ui.go b/hrp/step_mobile_ui.go index 9f3a52a3..41d9b140 100644 --- a/hrp/step_mobile_ui.go +++ b/hrp/step_mobile_ui.go @@ -32,7 +32,7 @@ func initUIClient(serial, osType string) (client *uixt.DriverExt, err error) { } } - var device uixt.Device + var device uixt.IDevice if osType == "ios" { device, err = uixt.NewIOSDevice(uixt.WithUDID(serial)) } else {