From 14c81ea14293cb5cbe614d0f3ab3b73545c4e471 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Thu, 6 Feb 2025 14:54:10 +0800 Subject: [PATCH] refactor: move ios options to pkg/uixt/options --- cmd/ios/init.go | 3 +- cmd/ios/install.go | 3 +- cmd/ios/uninstall.go | 3 +- config.go | 17 +- examples/uitest/bili/ios/cli.go | 3 +- .../uitest/demo_douyin_follow_live_test.go | 7 +- examples/uitest/demo_ios_live_swipe_test.go | 7 +- examples/uitest/demo_ios_wda_log_test.go | 6 +- examples/uitest/expert_test.go | 9 +- examples/worldcup/main.go | 7 +- examples/worldcup/main_test.go | 9 +- pkg/uixt/demo/main_test.go | 6 +- pkg/uixt/ios_device.go | 153 ++---------------- pkg/uixt/ios_driver_stub_test.go | 6 +- pkg/uixt/ios_test.go | 23 ++- pkg/uixt/options/android.go | 1 + pkg/uixt/options/ios.go | 112 +++++++++++++ pkg/uixt/{drag.go => ui_driver_drag.go} | 0 pkg/uixt/{input.go => ui_driver_input.go} | 0 pkg/uixt/{swipe.go => ui_swipe.go} | 0 pkg/uixt/{swipe_test.go => ui_swipe_test.go} | 0 pkg/uixt/{tap.go => ui_tap.go} | 0 pkg/uixt/{tap_test.go => ui_tap_test.go} | 0 step_mobile_ui_test.go | 6 +- step_request.go | 8 +- 25 files changed, 203 insertions(+), 186 deletions(-) create mode 100644 pkg/uixt/options/android.go create mode 100644 pkg/uixt/options/ios.go rename pkg/uixt/{drag.go => ui_driver_drag.go} (100%) rename pkg/uixt/{input.go => ui_driver_input.go} (100%) rename pkg/uixt/{swipe.go => ui_swipe.go} (100%) rename pkg/uixt/{swipe_test.go => ui_swipe_test.go} (100%) rename pkg/uixt/{tap.go => ui_tap.go} (100%) rename pkg/uixt/{tap_test.go => ui_tap_test.go} (100%) diff --git a/cmd/ios/init.go b/cmd/ios/init.go index afc603fc..509e9347 100644 --- a/cmd/ios/init.go +++ b/cmd/ios/init.go @@ -4,6 +4,7 @@ import ( "github.com/spf13/cobra" "github.com/httprunner/httprunner/v5/pkg/uixt" + "github.com/httprunner/httprunner/v5/pkg/uixt/options" ) var iosRootCmd = &cobra.Command{ @@ -12,7 +13,7 @@ var iosRootCmd = &cobra.Command{ } func getDevice(udid string) (*uixt.IOSDevice, error) { - device, err := uixt.NewIOSDevice(uixt.WithUDID(udid)) + device, err := uixt.NewIOSDevice(options.WithUDID(udid)) if err != nil { return nil, err } diff --git a/cmd/ios/install.go b/cmd/ios/install.go index 7a18131a..2f23a59a 100644 --- a/cmd/ios/install.go +++ b/cmd/ios/install.go @@ -9,6 +9,7 @@ import ( "github.com/httprunner/httprunner/v5/internal/sdk" "github.com/httprunner/httprunner/v5/pkg/uixt" + "github.com/httprunner/httprunner/v5/pkg/uixt/options" ) var installCmd = &cobra.Command{ @@ -29,7 +30,7 @@ var installCmd = &cobra.Command{ return err } - device, err := uixt.NewIOSDevice(uixt.WithUDID(udid)) + device, err := uixt.NewIOSDevice(options.WithUDID(udid)) if err != nil { fmt.Println(err) return err diff --git a/cmd/ios/uninstall.go b/cmd/ios/uninstall.go index 06d6511e..1cf6b282 100644 --- a/cmd/ios/uninstall.go +++ b/cmd/ios/uninstall.go @@ -9,6 +9,7 @@ import ( "github.com/httprunner/httprunner/v5/internal/sdk" "github.com/httprunner/httprunner/v5/pkg/uixt" + "github.com/httprunner/httprunner/v5/pkg/uixt/options" ) var uninstallCmd = &cobra.Command{ @@ -33,7 +34,7 @@ var uninstallCmd = &cobra.Command{ return err } - device, err := uixt.NewIOSDevice(uixt.WithUDID(udid)) + device, err := uixt.NewIOSDevice(options.WithUDID(udid)) if err != nil { fmt.Println(err) return err diff --git a/config.go b/config.go index 3e8319d1..a1e3b567 100644 --- a/config.go +++ b/config.go @@ -5,6 +5,7 @@ import ( "github.com/httprunner/httprunner/v5/internal/builtin" "github.com/httprunner/httprunner/v5/pkg/uixt" + "github.com/httprunner/httprunner/v5/pkg/uixt/options" ) type IConfig interface { @@ -117,23 +118,23 @@ func (c *TConfig) SetWebSocket(times, interval, timeout, size int64) *TConfig { return c } -func (c *TConfig) SetIOS(options ...uixt.IOSDeviceOption) *TConfig { - wdaOptions := &uixt.IOSDevice{} - for _, option := range options { - option(wdaOptions) +func (c *TConfig) SetIOS(opts ...options.IOSDeviceOption) *TConfig { + iosOptions := options.NewIOSDeviceConfig(opts...) + device := &uixt.IOSDevice{ + IOSDeviceConfig: iosOptions, } // each device can have its own settings - if wdaOptions.UDID != "" { - c.IOS = append(c.IOS, wdaOptions) + if iosOptions.UDID != "" { + c.IOS = append(c.IOS, device) return c } // device UDID is not specified, settings will be shared if len(c.IOS) == 0 { - c.IOS = append(c.IOS, wdaOptions) + c.IOS = append(c.IOS, device) } else { - c.IOS[0] = wdaOptions + c.IOS[0] = device } return c } diff --git a/examples/uitest/bili/ios/cli.go b/examples/uitest/bili/ios/cli.go index 2916e1e4..1f8db695 100644 --- a/examples/uitest/bili/ios/cli.go +++ b/examples/uitest/bili/ios/cli.go @@ -7,6 +7,7 @@ import ( "time" "github.com/httprunner/httprunner/v5/pkg/uixt" + "github.com/httprunner/httprunner/v5/pkg/uixt/options" ) var ( @@ -28,7 +29,7 @@ func init() { } func launchAppDriver(pkgName string) (driver *uixt.DriverExt, err error) { - device, _ := uixt.NewIOSDevice(uixt.WithUDID(serial)) + device, _ := uixt.NewIOSDevice(options.WithUDID(serial)) driver, err = device.NewDriver() if err != nil { return nil, err diff --git a/examples/uitest/demo_douyin_follow_live_test.go b/examples/uitest/demo_douyin_follow_live_test.go index 12c99960..0bcf97c9 100644 --- a/examples/uitest/demo_douyin_follow_live_test.go +++ b/examples/uitest/demo_douyin_follow_live_test.go @@ -7,6 +7,7 @@ import ( hrp "github.com/httprunner/httprunner/v5" "github.com/httprunner/httprunner/v5/pkg/uixt" + "github.com/httprunner/httprunner/v5/pkg/uixt/options" ) func TestIOSDouyinFollowLive(t *testing.T) { @@ -16,9 +17,9 @@ func TestIOSDouyinFollowLive(t *testing.T) { "app_name": "抖音", }). SetIOS( - uixt.WithWDALogOn(true), - uixt.WithWDAPort(8700), - uixt.WithWDAMjpegPort(8800), + options.WithWDALogOn(true), + options.WithWDAPort(8700), + options.WithWDAMjpegPort(8800), ), TestSteps: []hrp.IStep{ hrp.NewStep("启动抖音"). diff --git a/examples/uitest/demo_ios_live_swipe_test.go b/examples/uitest/demo_ios_live_swipe_test.go index c08b659e..817b0004 100644 --- a/examples/uitest/demo_ios_live_swipe_test.go +++ b/examples/uitest/demo_ios_live_swipe_test.go @@ -7,6 +7,7 @@ import ( hrp "github.com/httprunner/httprunner/v5" "github.com/httprunner/httprunner/v5/pkg/uixt" + "github.com/httprunner/httprunner/v5/pkg/uixt/options" ) func TestIOSDouyinLive(t *testing.T) { @@ -16,9 +17,9 @@ func TestIOSDouyinLive(t *testing.T) { "app_name": "抖音", }). SetIOS( - uixt.WithWDALogOn(true), - uixt.WithWDAPort(8700), - uixt.WithWDAMjpegPort(8800), + options.WithWDALogOn(true), + options.WithWDAPort(8700), + options.WithWDAMjpegPort(8800), ), TestSteps: []hrp.IStep{ hrp.NewStep("启动抖音"). diff --git a/examples/uitest/demo_ios_wda_log_test.go b/examples/uitest/demo_ios_wda_log_test.go index 15c6c8d0..041e1f99 100644 --- a/examples/uitest/demo_ios_wda_log_test.go +++ b/examples/uitest/demo_ios_wda_log_test.go @@ -7,6 +7,7 @@ import ( hrp "github.com/httprunner/httprunner/v5" "github.com/httprunner/httprunner/v5/pkg/uixt" + "github.com/httprunner/httprunner/v5/pkg/uixt/options" ) func TestWDALog(t *testing.T) { @@ -16,8 +17,9 @@ func TestWDALog(t *testing.T) { "app_name": "抖音", }). SetIOS( - uixt.WithWDALogOn(true), - uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800), + options.WithWDALogOn(true), + options.WithWDAPort(8700), + options.WithWDAMjpegPort(8800), ), TestSteps: []hrp.IStep{ hrp.NewStep("启动抖音"). diff --git a/examples/uitest/expert_test.go b/examples/uitest/expert_test.go index cdb37aee..41e12bb1 100644 --- a/examples/uitest/expert_test.go +++ b/examples/uitest/expert_test.go @@ -5,6 +5,7 @@ import ( hrp "github.com/httprunner/httprunner/v5" "github.com/httprunner/httprunner/v5/pkg/uixt" + "github.com/httprunner/httprunner/v5/pkg/uixt/options" ) func TestAndroidExpertTest(t *testing.T) { @@ -151,10 +152,10 @@ func TestIOSExpertTest(t *testing.T) { "app_name": "抖音", }). SetIOS( - uixt.WithUDID("$device"), - uixt.WithWDALogOn(true), - uixt.WithWDAPort(8700), - uixt.WithWDAMjpegPort(8800), + options.WithUDID("$device"), + options.WithWDALogOn(true), + options.WithWDAPort(8700), + options.WithWDAMjpegPort(8800), ), TestSteps: []hrp.IStep{ // 温启动 diff --git a/examples/worldcup/main.go b/examples/worldcup/main.go index 1ebd2225..c346bb43 100644 --- a/examples/worldcup/main.go +++ b/examples/worldcup/main.go @@ -16,6 +16,7 @@ import ( "github.com/rs/zerolog/log" "github.com/httprunner/httprunner/v5/pkg/uixt" + "github.com/httprunner/httprunner/v5/pkg/uixt/options" ) func convertTimeToSeconds(timeStr string) (int, error) { @@ -38,9 +39,9 @@ func convertTimeToSeconds(timeStr string) (int, error) { func initIOSDevice(uuid string) uixt.IDevice { device, err := uixt.NewIOSDevice( - uixt.WithUDID(uuid), - uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800), - uixt.WithResetHomeOnStartup(false), // not reset home on startup + options.WithUDID(uuid), + options.WithWDAPort(8700), options.WithWDAMjpegPort(8800), + options.WithResetHomeOnStartup(false), // not reset home on startup ) if err != nil { diff --git a/examples/worldcup/main_test.go b/examples/worldcup/main_test.go index f5ab4642..52044d20 100644 --- a/examples/worldcup/main_test.go +++ b/examples/worldcup/main_test.go @@ -9,6 +9,7 @@ import ( hrp "github.com/httprunner/httprunner/v5" "github.com/httprunner/httprunner/v5/pkg/uixt" + "github.com/httprunner/httprunner/v5/pkg/uixt/options" ) func TestConvertTimeToSeconds(t *testing.T) { @@ -55,10 +56,10 @@ func TestIOSDouyinWorldCupLive(t *testing.T) { "appBundleID": "com.ss.iphone.ugc.Aweme", }). SetIOS( - uixt.WithUDID(uuid), - uixt.WithWDALogOn(true), - uixt.WithWDAPort(8700), - uixt.WithWDAMjpegPort(8800), + options.WithUDID(uuid), + options.WithWDALogOn(true), + options.WithWDAPort(8700), + options.WithWDAMjpegPort(8800), ), TestSteps: []hrp.IStep{ hrp.NewStep("启动抖音"). diff --git a/pkg/uixt/demo/main_test.go b/pkg/uixt/demo/main_test.go index 31a75a1a..d197b43e 100644 --- a/pkg/uixt/demo/main_test.go +++ b/pkg/uixt/demo/main_test.go @@ -9,12 +9,14 @@ import ( "github.com/rs/zerolog/log" "github.com/httprunner/httprunner/v5/pkg/uixt" + "github.com/httprunner/httprunner/v5/pkg/uixt/options" ) func TestIOSDemo(t *testing.T) { device, err := uixt.NewIOSDevice( - uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800), - uixt.WithResetHomeOnStartup(false), // not reset home on startup + options.WithWDAPort(8700), + options.WithWDAMjpegPort(8800), + options.WithResetHomeOnStartup(false), // not reset home on startup ) if err != nil { t.Fatal(err) diff --git a/pkg/uixt/ios_device.go b/pkg/uixt/ios_device.go index b4946304..505e68fa 100644 --- a/pkg/uixt/ios_device.go +++ b/pkg/uixt/ios_device.go @@ -28,6 +28,7 @@ import ( "github.com/httprunner/httprunner/v5/code" "github.com/httprunner/httprunner/v5/internal/builtin" + "github.com/httprunner/httprunner/v5/pkg/uixt/options" ) const ( @@ -54,62 +55,6 @@ const ( var tunnelManager *tunnel.TunnelManager = nil -type IOSDeviceOption func(*IOSDevice) - -func WithUDID(udid string) IOSDeviceOption { - return func(device *IOSDevice) { - device.UDID = udid - } -} - -func WithWDAPort(port int) IOSDeviceOption { - return func(device *IOSDevice) { - device.Port = port - } -} - -func WithWDAMjpegPort(port int) IOSDeviceOption { - return func(device *IOSDevice) { - device.MjpegPort = port - } -} - -func WithWDALogOn(logOn bool) IOSDeviceOption { - return func(device *IOSDevice) { - device.LogOn = logOn - } -} - -func WithIOSStub(stub bool) IOSDeviceOption { - return func(device *IOSDevice) { - device.STUB = stub - } -} - -func WithResetHomeOnStartup(reset bool) IOSDeviceOption { - return func(device *IOSDevice) { - device.ResetHomeOnStartup = reset - } -} - -func WithSnapshotMaxDepth(depth int) IOSDeviceOption { - return func(device *IOSDevice) { - device.SnapshotMaxDepth = depth - } -} - -func WithAcceptAlertButtonSelector(selector string) IOSDeviceOption { - return func(device *IOSDevice) { - device.AcceptAlertButtonSelector = selector - } -} - -func WithDismissAlertButtonSelector(selector string) IOSDeviceOption { - return func(device *IOSDevice) { - device.DismissAlertButtonSelector = selector - } -} - func GetIOSDevices(udid ...string) (deviceList []ios.DeviceEntry, err error) { devices, err := ios.ListDevices() if err != nil { @@ -145,34 +90,6 @@ func GetIOSDevices(udid ...string) (deviceList []ios.DeviceEntry, err error) { return deviceList, nil } -func GetIOSDeviceOptions(dev *IOSDevice) (deviceOptions []IOSDeviceOption) { - if dev.UDID != "" { - deviceOptions = append(deviceOptions, WithUDID(dev.UDID)) - } - if dev.Port != 0 { - deviceOptions = append(deviceOptions, WithWDAPort(dev.Port)) - } - if dev.MjpegPort != 0 { - deviceOptions = append(deviceOptions, WithWDAMjpegPort(dev.MjpegPort)) - } - if dev.LogOn { - deviceOptions = append(deviceOptions, WithWDALogOn(true)) - } - if dev.ResetHomeOnStartup { - deviceOptions = append(deviceOptions, WithResetHomeOnStartup(true)) - } - if dev.SnapshotMaxDepth != 0 { - deviceOptions = append(deviceOptions, WithSnapshotMaxDepth(dev.SnapshotMaxDepth)) - } - if dev.AcceptAlertButtonSelector != "" { - deviceOptions = append(deviceOptions, WithAcceptAlertButtonSelector(dev.AcceptAlertButtonSelector)) - } - if dev.DismissAlertButtonSelector != "" { - deviceOptions = append(deviceOptions, WithDismissAlertButtonSelector(dev.DismissAlertButtonSelector)) - } - return -} - func StartTunnel(recordsPath string, tunnelInfoPort int, userspaceTUN bool) (err error) { pm, err := tunnel.NewPairRecordManager(recordsPath) if err != nil { @@ -208,8 +125,8 @@ func RebootTunnel() (err error) { return StartTunnel(os.TempDir(), ios.HttpApiPort(), true) } -func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) { - device = &IOSDevice{ +func NewIOSDevice(opts ...options.IOSDeviceOption) (device *IOSDevice, err error) { + deviceOptions := &options.IOSDeviceConfig{ Port: defaultWDAPort, MjpegPort: defaultMjpegPort, SnapshotMaxDepth: snapshotMaxDepth, @@ -218,32 +135,35 @@ func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) { // switch to iOS springboard before init WDA session // avoid getting stuck when some super app is active such as douyin or wexin ResetHomeOnStartup: true, - listeners: make(map[int]*forward.ConnListener), } - for _, option := range options { - option(device) + for _, option := range opts { + option(deviceOptions) } - deviceList, err := GetIOSDevices(device.UDID) + deviceList, err := GetIOSDevices(deviceOptions.UDID) if err != nil { return nil, errors.Wrap(code.DeviceConnectionError, err.Error()) } - if device.UDID == "" && len(deviceList) > 1 { + if deviceOptions.UDID == "" && len(deviceList) > 1 { return nil, errors.Wrap(code.DeviceConnectionError, "more than one device connected, please specify the udid") } dev := deviceList[0] udid := dev.Properties.SerialNumber - if device.UDID == "" { - device.UDID = udid + if deviceOptions.UDID == "" { + deviceOptions.UDID = udid log.Warn(). Str("udid", udid). Msg("ios UDID is not specified, select the first one") } - device.d = dev + device = &IOSDevice{ + IOSDeviceConfig: deviceOptions, + listeners: make(map[int]*forward.ConnListener), + d: dev, + } log.Info().Str("udid", device.UDID).Msg("init ios device") err = device.Init() if err != nil { @@ -254,52 +174,9 @@ func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) { } type IOSDevice struct { + *options.IOSDeviceConfig d ios.DeviceEntry listeners map[int]*forward.ConnListener - UDID string `json:"udid,omitempty" yaml:"udid,omitempty"` - Port int `json:"port,omitempty" yaml:"port,omitempty"` // WDA remote port - MjpegPort int `json:"mjpeg_port,omitempty" yaml:"mjpeg_port,omitempty"` // WDA remote MJPEG port - STUB bool `json:"stub,omitempty" yaml:"stub,omitempty"` // use stub - LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"` - - // switch to iOS springboard before init WDA session - ResetHomeOnStartup bool `json:"reset_home_on_startup,omitempty" yaml:"reset_home_on_startup,omitempty"` - - // config appium settings - SnapshotMaxDepth int `json:"snapshot_max_depth,omitempty" yaml:"snapshot_max_depth,omitempty"` - AcceptAlertButtonSelector string `json:"accept_alert_button_selector,omitempty" yaml:"accept_alert_button_selector,omitempty"` - DismissAlertButtonSelector string `json:"dismiss_alert_button_selector,omitempty" yaml:"dismiss_alert_button_selector,omitempty"` -} - -func (dev *IOSDevice) Options() (deviceOptions []IOSDeviceOption) { - if dev.UDID != "" { - deviceOptions = append(deviceOptions, WithUDID(dev.UDID)) - } - if dev.Port != 0 { - deviceOptions = append(deviceOptions, WithWDAPort(dev.Port)) - } - if dev.MjpegPort != 0 { - deviceOptions = append(deviceOptions, WithWDAMjpegPort(dev.MjpegPort)) - } - if dev.STUB { - deviceOptions = append(deviceOptions, WithIOSStub(true)) - } - if dev.LogOn { - deviceOptions = append(deviceOptions, WithWDALogOn(true)) - } - if dev.ResetHomeOnStartup { - deviceOptions = append(deviceOptions, WithResetHomeOnStartup(true)) - } - if dev.SnapshotMaxDepth != 0 { - deviceOptions = append(deviceOptions, WithSnapshotMaxDepth(dev.SnapshotMaxDepth)) - } - if dev.AcceptAlertButtonSelector != "" { - deviceOptions = append(deviceOptions, WithAcceptAlertButtonSelector(dev.AcceptAlertButtonSelector)) - } - if dev.DismissAlertButtonSelector != "" { - deviceOptions = append(deviceOptions, WithDismissAlertButtonSelector(dev.DismissAlertButtonSelector)) - } - return } type DeviceDetail struct { diff --git a/pkg/uixt/ios_driver_stub_test.go b/pkg/uixt/ios_driver_stub_test.go index 0a9f3a8e..95665a2a 100644 --- a/pkg/uixt/ios_driver_stub_test.go +++ b/pkg/uixt/ios_driver_stub_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/httprunner/httprunner/v5/internal/builtin" + "github.com/httprunner/httprunner/v5/pkg/uixt/options" ) var ( @@ -16,7 +17,10 @@ var ( func setupiOSStubDriver(t *testing.T) { var err error - iOSDevice, err = NewIOSDevice(WithWDAPort(8700), WithWDAMjpegPort(8800), WithIOSStub(false)) + iOSDevice, err = NewIOSDevice( + options.WithWDAPort(8700), + options.WithWDAMjpegPort(8800), + options.WithIOSStub(false)) checkErr(t, err) iOSStubDriver, err = iOSDevice.NewStubDriver() checkErr(t, err) diff --git a/pkg/uixt/ios_test.go b/pkg/uixt/ios_test.go index 1e5d0632..a2674069 100644 --- a/pkg/uixt/ios_test.go +++ b/pkg/uixt/ios_test.go @@ -8,6 +8,7 @@ import ( "testing" "time" + "github.com/httprunner/httprunner/v5/pkg/uixt/options" "github.com/rs/zerolog/log" ) @@ -18,7 +19,10 @@ var ( ) func setup(t *testing.T) { - device, err := NewIOSDevice(WithWDAPort(8700), WithWDAMjpegPort(8800), WithWDALogOn(true)) + device, err := NewIOSDevice( + options.WithWDAPort(8700), + options.WithWDAMjpegPort(8800), + options.WithWDALogOn(true)) if err != nil { t.Fatal(err) } @@ -49,29 +53,36 @@ func TestInstall(t *testing.T) { } func TestNewIOSDevice(t *testing.T) { - device, _ := NewIOSDevice(WithWDAPort(8700), WithWDAMjpegPort(8800)) + device, _ := NewIOSDevice( + options.WithWDAPort(8700), + options.WithWDAMjpegPort(8800)) if device != nil { t.Log(device) } - device, _ = NewIOSDevice(WithUDID("xxxx")) + device, _ = NewIOSDevice(options.WithUDID("xxxx")) if device != nil { t.Log(device) } - device, _ = NewIOSDevice(WithWDAPort(8700), WithWDAMjpegPort(8800)) + device, _ = NewIOSDevice( + options.WithWDAPort(8700), + options.WithWDAMjpegPort(8800)) if device != nil { t.Log(device) } - device, _ = NewIOSDevice(WithUDID("xxxx"), WithWDAPort(8700), WithWDAMjpegPort(8800)) + device, _ = NewIOSDevice( + options.WithUDID("xxxx"), + options.WithWDAPort(8700), + options.WithWDAMjpegPort(8800)) if device != nil { t.Log(device) } } func TestIOSDevice_GetPackageInfo(t *testing.T) { - device, err := NewIOSDevice(WithWDAPort(8700)) + device, err := NewIOSDevice(options.WithWDAPort(8700)) checkErr(t, err) appInfo, err := device.GetPackageInfo("com.ss.iphone.ugc.Aweme") checkErr(t, err) diff --git a/pkg/uixt/options/android.go b/pkg/uixt/options/android.go new file mode 100644 index 00000000..0684d059 --- /dev/null +++ b/pkg/uixt/options/android.go @@ -0,0 +1 @@ +package options diff --git a/pkg/uixt/options/ios.go b/pkg/uixt/options/ios.go new file mode 100644 index 00000000..141b9808 --- /dev/null +++ b/pkg/uixt/options/ios.go @@ -0,0 +1,112 @@ +package options + +type IOSDeviceConfig struct { + UDID string `json:"udid,omitempty" yaml:"udid,omitempty"` + Port int `json:"port,omitempty" yaml:"port,omitempty"` // WDA remote port + MjpegPort int `json:"mjpeg_port,omitempty" yaml:"mjpeg_port,omitempty"` // WDA remote MJPEG port + STUB bool `json:"stub,omitempty" yaml:"stub,omitempty"` // use stub + LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"` + + // switch to iOS springboard before init WDA session + ResetHomeOnStartup bool `json:"reset_home_on_startup,omitempty" yaml:"reset_home_on_startup,omitempty"` + + // config appium settings + SnapshotMaxDepth int `json:"snapshot_max_depth,omitempty" yaml:"snapshot_max_depth,omitempty"` + AcceptAlertButtonSelector string `json:"accept_alert_button_selector,omitempty" yaml:"accept_alert_button_selector,omitempty"` + DismissAlertButtonSelector string `json:"dismiss_alert_button_selector,omitempty" yaml:"dismiss_alert_button_selector,omitempty"` +} + +func (dev *IOSDeviceConfig) Options() (deviceOptions []IOSDeviceOption) { + if dev.UDID != "" { + deviceOptions = append(deviceOptions, WithUDID(dev.UDID)) + } + if dev.Port != 0 { + deviceOptions = append(deviceOptions, WithWDAPort(dev.Port)) + } + if dev.MjpegPort != 0 { + deviceOptions = append(deviceOptions, WithWDAMjpegPort(dev.MjpegPort)) + } + if dev.STUB { + deviceOptions = append(deviceOptions, WithIOSStub(true)) + } + if dev.LogOn { + deviceOptions = append(deviceOptions, WithWDALogOn(true)) + } + if dev.ResetHomeOnStartup { + deviceOptions = append(deviceOptions, WithResetHomeOnStartup(true)) + } + if dev.SnapshotMaxDepth != 0 { + deviceOptions = append(deviceOptions, WithSnapshotMaxDepth(dev.SnapshotMaxDepth)) + } + if dev.AcceptAlertButtonSelector != "" { + deviceOptions = append(deviceOptions, WithAcceptAlertButtonSelector(dev.AcceptAlertButtonSelector)) + } + if dev.DismissAlertButtonSelector != "" { + deviceOptions = append(deviceOptions, WithDismissAlertButtonSelector(dev.DismissAlertButtonSelector)) + } + return +} + +func NewIOSDeviceConfig(opts ...IOSDeviceOption) *IOSDeviceConfig { + config := &IOSDeviceConfig{} + for _, opt := range opts { + opt(config) + } + return config +} + +type IOSDeviceOption func(*IOSDeviceConfig) + +func WithUDID(udid string) IOSDeviceOption { + return func(device *IOSDeviceConfig) { + device.UDID = udid + } +} + +func WithWDAPort(port int) IOSDeviceOption { + return func(device *IOSDeviceConfig) { + device.Port = port + } +} + +func WithWDAMjpegPort(port int) IOSDeviceOption { + return func(device *IOSDeviceConfig) { + device.MjpegPort = port + } +} + +func WithWDALogOn(logOn bool) IOSDeviceOption { + return func(device *IOSDeviceConfig) { + device.LogOn = logOn + } +} + +func WithIOSStub(stub bool) IOSDeviceOption { + return func(device *IOSDeviceConfig) { + device.STUB = stub + } +} + +func WithResetHomeOnStartup(reset bool) IOSDeviceOption { + return func(device *IOSDeviceConfig) { + device.ResetHomeOnStartup = reset + } +} + +func WithSnapshotMaxDepth(depth int) IOSDeviceOption { + return func(device *IOSDeviceConfig) { + device.SnapshotMaxDepth = depth + } +} + +func WithAcceptAlertButtonSelector(selector string) IOSDeviceOption { + return func(device *IOSDeviceConfig) { + device.AcceptAlertButtonSelector = selector + } +} + +func WithDismissAlertButtonSelector(selector string) IOSDeviceOption { + return func(device *IOSDeviceConfig) { + device.DismissAlertButtonSelector = selector + } +} diff --git a/pkg/uixt/drag.go b/pkg/uixt/ui_driver_drag.go similarity index 100% rename from pkg/uixt/drag.go rename to pkg/uixt/ui_driver_drag.go diff --git a/pkg/uixt/input.go b/pkg/uixt/ui_driver_input.go similarity index 100% rename from pkg/uixt/input.go rename to pkg/uixt/ui_driver_input.go diff --git a/pkg/uixt/swipe.go b/pkg/uixt/ui_swipe.go similarity index 100% rename from pkg/uixt/swipe.go rename to pkg/uixt/ui_swipe.go diff --git a/pkg/uixt/swipe_test.go b/pkg/uixt/ui_swipe_test.go similarity index 100% rename from pkg/uixt/swipe_test.go rename to pkg/uixt/ui_swipe_test.go diff --git a/pkg/uixt/tap.go b/pkg/uixt/ui_tap.go similarity index 100% rename from pkg/uixt/tap.go rename to pkg/uixt/ui_tap.go diff --git a/pkg/uixt/tap_test.go b/pkg/uixt/ui_tap_test.go similarity index 100% rename from pkg/uixt/tap_test.go rename to pkg/uixt/ui_tap_test.go diff --git a/step_mobile_ui_test.go b/step_mobile_ui_test.go index ab8bc677..aae4c093 100644 --- a/step_mobile_ui_test.go +++ b/step_mobile_ui_test.go @@ -5,13 +5,13 @@ package hrp import ( "testing" - "github.com/httprunner/httprunner/v5/pkg/uixt" + "github.com/httprunner/httprunner/v5/pkg/uixt/options" ) func TestIOSSettingsAction(t *testing.T) { testCase := &TestCase{ Config: NewConfig("ios ui action on Settings"). - SetIOS(uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800)), + SetIOS(options.WithWDAPort(8700), options.WithWDAMjpegPort(8800)), TestSteps: []IStep{ NewStep("launch Settings"). IOS().Home().Tap("设置"). @@ -50,7 +50,7 @@ func TestIOSSearchApp(t *testing.T) { func TestIOSAppLaunch(t *testing.T) { testCase := &TestCase{ Config: NewConfig("启动 & 关闭 App"). - SetIOS(uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800)), + SetIOS(options.WithWDAPort(8700), options.WithWDAMjpegPort(8800)), TestSteps: []IStep{ NewStep("终止今日头条"). IOS().AppTerminate("com.ss.iphone.article.News"), diff --git a/step_request.go b/step_request.go index 79320fb3..3d40629b 100644 --- a/step_request.go +++ b/step_request.go @@ -25,6 +25,7 @@ import ( "github.com/httprunner/httprunner/v5/internal/json" "github.com/httprunner/httprunner/v5/pkg/httpstat" "github.com/httprunner/httprunner/v5/pkg/uixt" + "github.com/httprunner/httprunner/v5/pkg/uixt/options" ) type HTTPMethod string @@ -773,11 +774,8 @@ func (s *StepRequest) Android(options ...uixt.AndroidDeviceOption) *StepMobile { } // IOS creates a new ios step session -func (s *StepRequest) IOS(options ...uixt.IOSDeviceOption) *StepMobile { - iosOptions := &uixt.IOSDevice{} - for _, option := range options { - option(iosOptions) - } +func (s *StepRequest) IOS(opts ...options.IOSDeviceOption) *StepMobile { + iosOptions := options.NewIOSDeviceConfig(opts...) return &StepMobile{ StepConfig: s.StepConfig, IOS: &MobileUI{