refactor: move ios options to pkg/uixt/options

This commit is contained in:
lilong.129
2025-02-06 14:54:10 +08:00
parent 0c5b60b94a
commit 14c81ea142
25 changed files with 203 additions and 186 deletions

View File

@@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/httprunner/httprunner/v5/pkg/uixt" "github.com/httprunner/httprunner/v5/pkg/uixt"
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
) )
var iosRootCmd = &cobra.Command{ var iosRootCmd = &cobra.Command{
@@ -12,7 +13,7 @@ var iosRootCmd = &cobra.Command{
} }
func getDevice(udid string) (*uixt.IOSDevice, error) { func getDevice(udid string) (*uixt.IOSDevice, error) {
device, err := uixt.NewIOSDevice(uixt.WithUDID(udid)) device, err := uixt.NewIOSDevice(options.WithUDID(udid))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -9,6 +9,7 @@ import (
"github.com/httprunner/httprunner/v5/internal/sdk" "github.com/httprunner/httprunner/v5/internal/sdk"
"github.com/httprunner/httprunner/v5/pkg/uixt" "github.com/httprunner/httprunner/v5/pkg/uixt"
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
) )
var installCmd = &cobra.Command{ var installCmd = &cobra.Command{
@@ -29,7 +30,7 @@ var installCmd = &cobra.Command{
return err return err
} }
device, err := uixt.NewIOSDevice(uixt.WithUDID(udid)) device, err := uixt.NewIOSDevice(options.WithUDID(udid))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return err return err

View File

@@ -9,6 +9,7 @@ import (
"github.com/httprunner/httprunner/v5/internal/sdk" "github.com/httprunner/httprunner/v5/internal/sdk"
"github.com/httprunner/httprunner/v5/pkg/uixt" "github.com/httprunner/httprunner/v5/pkg/uixt"
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
) )
var uninstallCmd = &cobra.Command{ var uninstallCmd = &cobra.Command{
@@ -33,7 +34,7 @@ var uninstallCmd = &cobra.Command{
return err return err
} }
device, err := uixt.NewIOSDevice(uixt.WithUDID(udid)) device, err := uixt.NewIOSDevice(options.WithUDID(udid))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return err return err

View File

@@ -5,6 +5,7 @@ import (
"github.com/httprunner/httprunner/v5/internal/builtin" "github.com/httprunner/httprunner/v5/internal/builtin"
"github.com/httprunner/httprunner/v5/pkg/uixt" "github.com/httprunner/httprunner/v5/pkg/uixt"
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
) )
type IConfig interface { type IConfig interface {
@@ -117,23 +118,23 @@ func (c *TConfig) SetWebSocket(times, interval, timeout, size int64) *TConfig {
return c return c
} }
func (c *TConfig) SetIOS(options ...uixt.IOSDeviceOption) *TConfig { func (c *TConfig) SetIOS(opts ...options.IOSDeviceOption) *TConfig {
wdaOptions := &uixt.IOSDevice{} iosOptions := options.NewIOSDeviceConfig(opts...)
for _, option := range options { device := &uixt.IOSDevice{
option(wdaOptions) IOSDeviceConfig: iosOptions,
} }
// each device can have its own settings // each device can have its own settings
if wdaOptions.UDID != "" { if iosOptions.UDID != "" {
c.IOS = append(c.IOS, wdaOptions) c.IOS = append(c.IOS, device)
return c return c
} }
// device UDID is not specified, settings will be shared // device UDID is not specified, settings will be shared
if len(c.IOS) == 0 { if len(c.IOS) == 0 {
c.IOS = append(c.IOS, wdaOptions) c.IOS = append(c.IOS, device)
} else { } else {
c.IOS[0] = wdaOptions c.IOS[0] = device
} }
return c return c
} }

View File

@@ -7,6 +7,7 @@ import (
"time" "time"
"github.com/httprunner/httprunner/v5/pkg/uixt" "github.com/httprunner/httprunner/v5/pkg/uixt"
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
) )
var ( var (
@@ -28,7 +29,7 @@ func init() {
} }
func launchAppDriver(pkgName string) (driver *uixt.DriverExt, err error) { 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() driver, err = device.NewDriver()
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -7,6 +7,7 @@ import (
hrp "github.com/httprunner/httprunner/v5" hrp "github.com/httprunner/httprunner/v5"
"github.com/httprunner/httprunner/v5/pkg/uixt" "github.com/httprunner/httprunner/v5/pkg/uixt"
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
) )
func TestIOSDouyinFollowLive(t *testing.T) { func TestIOSDouyinFollowLive(t *testing.T) {
@@ -16,9 +17,9 @@ func TestIOSDouyinFollowLive(t *testing.T) {
"app_name": "抖音", "app_name": "抖音",
}). }).
SetIOS( SetIOS(
uixt.WithWDALogOn(true), options.WithWDALogOn(true),
uixt.WithWDAPort(8700), options.WithWDAPort(8700),
uixt.WithWDAMjpegPort(8800), options.WithWDAMjpegPort(8800),
), ),
TestSteps: []hrp.IStep{ TestSteps: []hrp.IStep{
hrp.NewStep("启动抖音"). hrp.NewStep("启动抖音").

View File

@@ -7,6 +7,7 @@ import (
hrp "github.com/httprunner/httprunner/v5" hrp "github.com/httprunner/httprunner/v5"
"github.com/httprunner/httprunner/v5/pkg/uixt" "github.com/httprunner/httprunner/v5/pkg/uixt"
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
) )
func TestIOSDouyinLive(t *testing.T) { func TestIOSDouyinLive(t *testing.T) {
@@ -16,9 +17,9 @@ func TestIOSDouyinLive(t *testing.T) {
"app_name": "抖音", "app_name": "抖音",
}). }).
SetIOS( SetIOS(
uixt.WithWDALogOn(true), options.WithWDALogOn(true),
uixt.WithWDAPort(8700), options.WithWDAPort(8700),
uixt.WithWDAMjpegPort(8800), options.WithWDAMjpegPort(8800),
), ),
TestSteps: []hrp.IStep{ TestSteps: []hrp.IStep{
hrp.NewStep("启动抖音"). hrp.NewStep("启动抖音").

View File

@@ -7,6 +7,7 @@ import (
hrp "github.com/httprunner/httprunner/v5" hrp "github.com/httprunner/httprunner/v5"
"github.com/httprunner/httprunner/v5/pkg/uixt" "github.com/httprunner/httprunner/v5/pkg/uixt"
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
) )
func TestWDALog(t *testing.T) { func TestWDALog(t *testing.T) {
@@ -16,8 +17,9 @@ func TestWDALog(t *testing.T) {
"app_name": "抖音", "app_name": "抖音",
}). }).
SetIOS( SetIOS(
uixt.WithWDALogOn(true), options.WithWDALogOn(true),
uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800), options.WithWDAPort(8700),
options.WithWDAMjpegPort(8800),
), ),
TestSteps: []hrp.IStep{ TestSteps: []hrp.IStep{
hrp.NewStep("启动抖音"). hrp.NewStep("启动抖音").

View File

@@ -5,6 +5,7 @@ import (
hrp "github.com/httprunner/httprunner/v5" hrp "github.com/httprunner/httprunner/v5"
"github.com/httprunner/httprunner/v5/pkg/uixt" "github.com/httprunner/httprunner/v5/pkg/uixt"
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
) )
func TestAndroidExpertTest(t *testing.T) { func TestAndroidExpertTest(t *testing.T) {
@@ -151,10 +152,10 @@ func TestIOSExpertTest(t *testing.T) {
"app_name": "抖音", "app_name": "抖音",
}). }).
SetIOS( SetIOS(
uixt.WithUDID("$device"), options.WithUDID("$device"),
uixt.WithWDALogOn(true), options.WithWDALogOn(true),
uixt.WithWDAPort(8700), options.WithWDAPort(8700),
uixt.WithWDAMjpegPort(8800), options.WithWDAMjpegPort(8800),
), ),
TestSteps: []hrp.IStep{ TestSteps: []hrp.IStep{
// 温启动 // 温启动

View File

@@ -16,6 +16,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v5/pkg/uixt" "github.com/httprunner/httprunner/v5/pkg/uixt"
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
) )
func convertTimeToSeconds(timeStr string) (int, error) { func convertTimeToSeconds(timeStr string) (int, error) {
@@ -38,9 +39,9 @@ func convertTimeToSeconds(timeStr string) (int, error) {
func initIOSDevice(uuid string) uixt.IDevice { func initIOSDevice(uuid string) uixt.IDevice {
device, err := uixt.NewIOSDevice( device, err := uixt.NewIOSDevice(
uixt.WithUDID(uuid), options.WithUDID(uuid),
uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800), options.WithWDAPort(8700), options.WithWDAMjpegPort(8800),
uixt.WithResetHomeOnStartup(false), // not reset home on startup options.WithResetHomeOnStartup(false), // not reset home on startup
) )
if err != nil { if err != nil {

View File

@@ -9,6 +9,7 @@ import (
hrp "github.com/httprunner/httprunner/v5" hrp "github.com/httprunner/httprunner/v5"
"github.com/httprunner/httprunner/v5/pkg/uixt" "github.com/httprunner/httprunner/v5/pkg/uixt"
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
) )
func TestConvertTimeToSeconds(t *testing.T) { func TestConvertTimeToSeconds(t *testing.T) {
@@ -55,10 +56,10 @@ func TestIOSDouyinWorldCupLive(t *testing.T) {
"appBundleID": "com.ss.iphone.ugc.Aweme", "appBundleID": "com.ss.iphone.ugc.Aweme",
}). }).
SetIOS( SetIOS(
uixt.WithUDID(uuid), options.WithUDID(uuid),
uixt.WithWDALogOn(true), options.WithWDALogOn(true),
uixt.WithWDAPort(8700), options.WithWDAPort(8700),
uixt.WithWDAMjpegPort(8800), options.WithWDAMjpegPort(8800),
), ),
TestSteps: []hrp.IStep{ TestSteps: []hrp.IStep{
hrp.NewStep("启动抖音"). hrp.NewStep("启动抖音").

View File

@@ -9,12 +9,14 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v5/pkg/uixt" "github.com/httprunner/httprunner/v5/pkg/uixt"
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
) )
func TestIOSDemo(t *testing.T) { func TestIOSDemo(t *testing.T) {
device, err := uixt.NewIOSDevice( device, err := uixt.NewIOSDevice(
uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800), options.WithWDAPort(8700),
uixt.WithResetHomeOnStartup(false), // not reset home on startup options.WithWDAMjpegPort(8800),
options.WithResetHomeOnStartup(false), // not reset home on startup
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View File

@@ -28,6 +28,7 @@ import (
"github.com/httprunner/httprunner/v5/code" "github.com/httprunner/httprunner/v5/code"
"github.com/httprunner/httprunner/v5/internal/builtin" "github.com/httprunner/httprunner/v5/internal/builtin"
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
) )
const ( const (
@@ -54,62 +55,6 @@ const (
var tunnelManager *tunnel.TunnelManager = nil 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) { func GetIOSDevices(udid ...string) (deviceList []ios.DeviceEntry, err error) {
devices, err := ios.ListDevices() devices, err := ios.ListDevices()
if err != nil { if err != nil {
@@ -145,34 +90,6 @@ func GetIOSDevices(udid ...string) (deviceList []ios.DeviceEntry, err error) {
return deviceList, nil 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) { func StartTunnel(recordsPath string, tunnelInfoPort int, userspaceTUN bool) (err error) {
pm, err := tunnel.NewPairRecordManager(recordsPath) pm, err := tunnel.NewPairRecordManager(recordsPath)
if err != nil { if err != nil {
@@ -208,8 +125,8 @@ func RebootTunnel() (err error) {
return StartTunnel(os.TempDir(), ios.HttpApiPort(), true) return StartTunnel(os.TempDir(), ios.HttpApiPort(), true)
} }
func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) { func NewIOSDevice(opts ...options.IOSDeviceOption) (device *IOSDevice, err error) {
device = &IOSDevice{ deviceOptions := &options.IOSDeviceConfig{
Port: defaultWDAPort, Port: defaultWDAPort,
MjpegPort: defaultMjpegPort, MjpegPort: defaultMjpegPort,
SnapshotMaxDepth: snapshotMaxDepth, SnapshotMaxDepth: snapshotMaxDepth,
@@ -218,32 +135,35 @@ func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) {
// switch to iOS springboard before init WDA session // switch to iOS springboard before init WDA session
// avoid getting stuck when some super app is active such as douyin or wexin // avoid getting stuck when some super app is active such as douyin or wexin
ResetHomeOnStartup: true, ResetHomeOnStartup: true,
listeners: make(map[int]*forward.ConnListener),
} }
for _, option := range options { for _, option := range opts {
option(device) option(deviceOptions)
} }
deviceList, err := GetIOSDevices(device.UDID) deviceList, err := GetIOSDevices(deviceOptions.UDID)
if err != nil { if err != nil {
return nil, errors.Wrap(code.DeviceConnectionError, err.Error()) 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") return nil, errors.Wrap(code.DeviceConnectionError, "more than one device connected, please specify the udid")
} }
dev := deviceList[0] dev := deviceList[0]
udid := dev.Properties.SerialNumber udid := dev.Properties.SerialNumber
if device.UDID == "" { if deviceOptions.UDID == "" {
device.UDID = udid deviceOptions.UDID = udid
log.Warn(). log.Warn().
Str("udid", udid). Str("udid", udid).
Msg("ios UDID is not specified, select the first one") 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") log.Info().Str("udid", device.UDID).Msg("init ios device")
err = device.Init() err = device.Init()
if err != nil { if err != nil {
@@ -254,52 +174,9 @@ func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) {
} }
type IOSDevice struct { type IOSDevice struct {
*options.IOSDeviceConfig
d ios.DeviceEntry d ios.DeviceEntry
listeners map[int]*forward.ConnListener 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 { type DeviceDetail struct {

View File

@@ -7,6 +7,7 @@ import (
"testing" "testing"
"github.com/httprunner/httprunner/v5/internal/builtin" "github.com/httprunner/httprunner/v5/internal/builtin"
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
) )
var ( var (
@@ -16,7 +17,10 @@ var (
func setupiOSStubDriver(t *testing.T) { func setupiOSStubDriver(t *testing.T) {
var err error 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) checkErr(t, err)
iOSStubDriver, err = iOSDevice.NewStubDriver() iOSStubDriver, err = iOSDevice.NewStubDriver()
checkErr(t, err) checkErr(t, err)

View File

@@ -8,6 +8,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
@@ -18,7 +19,10 @@ var (
) )
func setup(t *testing.T) { 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -49,29 +53,36 @@ func TestInstall(t *testing.T) {
} }
func TestNewIOSDevice(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 { if device != nil {
t.Log(device) t.Log(device)
} }
device, _ = NewIOSDevice(WithUDID("xxxx")) device, _ = NewIOSDevice(options.WithUDID("xxxx"))
if device != nil { if device != nil {
t.Log(device) t.Log(device)
} }
device, _ = NewIOSDevice(WithWDAPort(8700), WithWDAMjpegPort(8800)) device, _ = NewIOSDevice(
options.WithWDAPort(8700),
options.WithWDAMjpegPort(8800))
if device != nil { if device != nil {
t.Log(device) 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 { if device != nil {
t.Log(device) t.Log(device)
} }
} }
func TestIOSDevice_GetPackageInfo(t *testing.T) { func TestIOSDevice_GetPackageInfo(t *testing.T) {
device, err := NewIOSDevice(WithWDAPort(8700)) device, err := NewIOSDevice(options.WithWDAPort(8700))
checkErr(t, err) checkErr(t, err)
appInfo, err := device.GetPackageInfo("com.ss.iphone.ugc.Aweme") appInfo, err := device.GetPackageInfo("com.ss.iphone.ugc.Aweme")
checkErr(t, err) checkErr(t, err)

View File

@@ -0,0 +1 @@
package options

112
pkg/uixt/options/ios.go Normal file
View File

@@ -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
}
}

View File

@@ -5,13 +5,13 @@ package hrp
import ( import (
"testing" "testing"
"github.com/httprunner/httprunner/v5/pkg/uixt" "github.com/httprunner/httprunner/v5/pkg/uixt/options"
) )
func TestIOSSettingsAction(t *testing.T) { func TestIOSSettingsAction(t *testing.T) {
testCase := &TestCase{ testCase := &TestCase{
Config: NewConfig("ios ui action on Settings"). Config: NewConfig("ios ui action on Settings").
SetIOS(uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800)), SetIOS(options.WithWDAPort(8700), options.WithWDAMjpegPort(8800)),
TestSteps: []IStep{ TestSteps: []IStep{
NewStep("launch Settings"). NewStep("launch Settings").
IOS().Home().Tap("设置"). IOS().Home().Tap("设置").
@@ -50,7 +50,7 @@ func TestIOSSearchApp(t *testing.T) {
func TestIOSAppLaunch(t *testing.T) { func TestIOSAppLaunch(t *testing.T) {
testCase := &TestCase{ testCase := &TestCase{
Config: NewConfig("启动 & 关闭 App"). Config: NewConfig("启动 & 关闭 App").
SetIOS(uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800)), SetIOS(options.WithWDAPort(8700), options.WithWDAMjpegPort(8800)),
TestSteps: []IStep{ TestSteps: []IStep{
NewStep("终止今日头条"). NewStep("终止今日头条").
IOS().AppTerminate("com.ss.iphone.article.News"), IOS().AppTerminate("com.ss.iphone.article.News"),

View File

@@ -25,6 +25,7 @@ import (
"github.com/httprunner/httprunner/v5/internal/json" "github.com/httprunner/httprunner/v5/internal/json"
"github.com/httprunner/httprunner/v5/pkg/httpstat" "github.com/httprunner/httprunner/v5/pkg/httpstat"
"github.com/httprunner/httprunner/v5/pkg/uixt" "github.com/httprunner/httprunner/v5/pkg/uixt"
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
) )
type HTTPMethod string type HTTPMethod string
@@ -773,11 +774,8 @@ func (s *StepRequest) Android(options ...uixt.AndroidDeviceOption) *StepMobile {
} }
// IOS creates a new ios step session // IOS creates a new ios step session
func (s *StepRequest) IOS(options ...uixt.IOSDeviceOption) *StepMobile { func (s *StepRequest) IOS(opts ...options.IOSDeviceOption) *StepMobile {
iosOptions := &uixt.IOSDevice{} iosOptions := options.NewIOSDeviceConfig(opts...)
for _, option := range options {
option(iosOptions)
}
return &StepMobile{ return &StepMobile{
StepConfig: s.StepConfig, StepConfig: s.StepConfig,
IOS: &MobileUI{ IOS: &MobileUI{