feat: support config ios ResetHomeOnStartup, SnapshotMaxDepth, AcceptAlertButtonSelector, DismissAlertButtonSelector

This commit is contained in:
debugtalk
2022-10-11 20:44:24 +08:00
parent 7a0ce219e3
commit fa7e2754af
3 changed files with 77 additions and 20 deletions

View File

@@ -8,7 +8,10 @@ import (
) )
func TestIOSDemo(t *testing.T) { func TestIOSDemo(t *testing.T) {
device, err := uixt.NewIOSDevice(uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800)) device, err := uixt.NewIOSDevice(
uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800),
uixt.WithResetHomeOnStartup(false), // not reset home on startup
)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@@ -24,6 +24,12 @@ import (
"github.com/httprunner/httprunner/v4/hrp/internal/json" "github.com/httprunner/httprunner/v4/hrp/internal/json"
) )
const (
defaultWDAPort = 8100
defaultMjpegPort = 9100
defaultResetHomeOnStartup = true
)
const ( const (
// Changes the value of maximum depth for traversing elements source tree. // Changes the value of maximum depth for traversing elements source tree.
// It may help to prevent out of memory or timeout errors while getting the elements source tree, // It may help to prevent out of memory or timeout errors while getting the elements source tree,
@@ -39,11 +45,6 @@ const (
dismissAlertButtonSelector = "**/XCUIElementTypeButton[`label IN {'不允许','暂不'}`]" dismissAlertButtonSelector = "**/XCUIElementTypeButton[`label IN {'不允许','暂不'}`]"
) )
const (
defaultWDAPort = 8100
defaultMjpegPort = 9100
)
type IOSDeviceOption func(*IOSDevice) type IOSDeviceOption func(*IOSDevice)
func WithUDID(udid string) IOSDeviceOption { func WithUDID(udid string) IOSDeviceOption {
@@ -70,6 +71,30 @@ func WithLogOn(logOn bool) IOSDeviceOption {
} }
} }
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 WithPerfOptions(options ...giDevice.PerfOption) IOSDeviceOption { func WithPerfOptions(options ...giDevice.PerfOption) IOSDeviceOption {
return func(device *IOSDevice) { return func(device *IOSDevice) {
device.PerfOptions = &giDevice.PerfOptions{} device.PerfOptions = &giDevice.PerfOptions{}
@@ -105,8 +130,12 @@ func IOSDevices(udid ...string) (devices []giDevice.Device, err error) {
func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) { func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) {
device = &IOSDevice{ device = &IOSDevice{
Port: defaultWDAPort, Port: defaultWDAPort,
MjpegPort: defaultMjpegPort, MjpegPort: defaultMjpegPort,
ResetHomeOnStartup: defaultResetHomeOnStartup,
SnapshotMaxDepth: snapshotMaxDepth,
AcceptAlertButtonSelector: acceptAlertButtonSelector,
DismissAlertButtonSelector: dismissAlertButtonSelector,
} }
for _, option := range options { for _, option := range options {
option(device) option(device)
@@ -134,6 +163,12 @@ type IOSDevice struct {
Port int `json:"port,omitempty" yaml:"port,omitempty"` // WDA remote port 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 MjpegPort int `json:"mjpeg_port,omitempty" yaml:"mjpeg_port,omitempty"` // WDA remote MJPEG port
LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"` LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"`
// switch to iOS springboard before init WDA session
// avoid getting stuck when some super app is activate such as douyin or wexin
ResetHomeOnStartup bool `json:"reset_home_on_startup,omitempty" yaml:"reset_home_on_startup,omitempty"`
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) UUID() string { func (dev *IOSDevice) UUID() string {
@@ -151,6 +186,21 @@ func (dev *IOSDevice) NewDriver(capabilities Capabilities) (driverExt *DriverExt
if dev.MjpegPort != 0 { if dev.MjpegPort != 0 {
deviceOptions = append(deviceOptions, WithWDAMjpegPort(dev.MjpegPort)) deviceOptions = append(deviceOptions, WithWDAMjpegPort(dev.MjpegPort))
} }
if dev.LogOn {
deviceOptions = append(deviceOptions, WithLogOn(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, WithAcceptAlertButtonSelector(dev.DismissAlertButtonSelector))
}
iosDevice, err := NewIOSDevice(deviceOptions...) iosDevice, err := NewIOSDevice(deviceOptions...)
if err != nil { if err != nil {
@@ -177,11 +227,11 @@ func (dev *IOSDevice) initWDAClient(capabilities Capabilities) (driverExt *Drive
return nil, errors.Wrap(err, "failed to init WDA driver") return nil, errors.Wrap(err, "failed to init WDA driver")
} }
// switch to iOS springboard before init WDA session if dev.ResetHomeOnStartup {
// avoid getting stuck when some super app is activate such as douyin or wexin log.Info().Msg("go back to home screen")
log.Info().Msg("go back to home screen") if err = driver.Homescreen(); err != nil {
if err = driver.Homescreen(); err != nil { return nil, errors.Wrap(err, "failed to go back to home screen")
return nil, errors.Wrap(err, "failed to go back to home screen") }
} }
driverExt, err = Extend(driver) driverExt, err = Extend(driver)
@@ -189,8 +239,8 @@ func (dev *IOSDevice) initWDAClient(capabilities Capabilities) (driverExt *Drive
return nil, errors.Wrap(err, "failed to extend WebDriver") return nil, errors.Wrap(err, "failed to extend WebDriver")
} }
settings, err := driverExt.Driver.SetAppiumSettings(map[string]interface{}{ settings, err := driverExt.Driver.SetAppiumSettings(map[string]interface{}{
"snapshotMaxDepth": snapshotMaxDepth, "snapshotMaxDepth": dev.SnapshotMaxDepth,
"acceptAlertButtonSelector": acceptAlertButtonSelector, "acceptAlertButtonSelector": dev.AcceptAlertButtonSelector,
}) })
if err != nil { if err != nil {
return nil, errors.Wrap(err, "failed to set appium WDA settings") return nil, errors.Wrap(err, "failed to set appium WDA settings")

View File

@@ -11,11 +11,15 @@ import (
) )
var ( var (
WithUDID = uixt.WithUDID WithUDID = uixt.WithUDID
WithWDAPort = uixt.WithWDAPort WithWDAPort = uixt.WithWDAPort
WithWDAMjpegPort = uixt.WithWDAMjpegPort WithWDAMjpegPort = uixt.WithWDAMjpegPort
WithLogOn = uixt.WithLogOn WithLogOn = uixt.WithLogOn
WithPerfOptions = uixt.WithPerfOptions WithResetHomeOnStartup = uixt.WithResetHomeOnStartup
WithSnapshotMaxDepth = uixt.WithSnapshotMaxDepth
WithAcceptAlertButtonSelector = uixt.WithAcceptAlertButtonSelector
WithDismissAlertButtonSelector = uixt.WithDismissAlertButtonSelector
WithPerfOptions = uixt.WithPerfOptions
) )
type IOSStep struct { type IOSStep struct {