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) {
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 {
t.Fatal(err)
}

View File

@@ -24,6 +24,12 @@ import (
"github.com/httprunner/httprunner/v4/hrp/internal/json"
)
const (
defaultWDAPort = 8100
defaultMjpegPort = 9100
defaultResetHomeOnStartup = true
)
const (
// 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,
@@ -39,11 +45,6 @@ const (
dismissAlertButtonSelector = "**/XCUIElementTypeButton[`label IN {'不允许','暂不'}`]"
)
const (
defaultWDAPort = 8100
defaultMjpegPort = 9100
)
type IOSDeviceOption func(*IOSDevice)
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 {
return func(device *IOSDevice) {
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) {
device = &IOSDevice{
Port: defaultWDAPort,
MjpegPort: defaultMjpegPort,
Port: defaultWDAPort,
MjpegPort: defaultMjpegPort,
ResetHomeOnStartup: defaultResetHomeOnStartup,
SnapshotMaxDepth: snapshotMaxDepth,
AcceptAlertButtonSelector: acceptAlertButtonSelector,
DismissAlertButtonSelector: dismissAlertButtonSelector,
}
for _, option := range options {
option(device)
@@ -134,6 +163,12 @@ type IOSDevice struct {
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
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 {
@@ -151,6 +186,21 @@ func (dev *IOSDevice) NewDriver(capabilities Capabilities) (driverExt *DriverExt
if dev.MjpegPort != 0 {
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...)
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")
}
// switch to iOS springboard before init WDA session
// avoid getting stuck when some super app is activate such as douyin or wexin
log.Info().Msg("go back to home screen")
if err = driver.Homescreen(); err != nil {
return nil, errors.Wrap(err, "failed to go back to home screen")
if dev.ResetHomeOnStartup {
log.Info().Msg("go back to home screen")
if err = driver.Homescreen(); err != nil {
return nil, errors.Wrap(err, "failed to go back to home screen")
}
}
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")
}
settings, err := driverExt.Driver.SetAppiumSettings(map[string]interface{}{
"snapshotMaxDepth": snapshotMaxDepth,
"acceptAlertButtonSelector": acceptAlertButtonSelector,
"snapshotMaxDepth": dev.SnapshotMaxDepth,
"acceptAlertButtonSelector": dev.AcceptAlertButtonSelector,
})
if err != nil {
return nil, errors.Wrap(err, "failed to set appium WDA settings")

View File

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