refactor: api

This commit is contained in:
lilong.129
2025-02-07 15:16:13 +08:00
parent 6cbab3c066
commit ae5a137353
23 changed files with 635 additions and 267 deletions

View File

@@ -1,18 +1,31 @@
package option
type AndroidDeviceConfig struct {
import "github.com/httprunner/httprunner/v5/pkg/gadb"
type AndroidDeviceOptions struct {
SerialNumber string `json:"serial,omitempty" yaml:"serial,omitempty"`
STUB bool `json:"stub,omitempty" yaml:"stub,omitempty"` // use stub
UIA2 bool `json:"uia2,omitempty" yaml:"uia2,omitempty"` // use uiautomator2
UIA2IP string `json:"uia2_ip,omitempty" yaml:"uia2_ip,omitempty"` // uiautomator2 server ip
UIA2Port int `json:"uia2_port,omitempty" yaml:"uia2_port,omitempty"` // uiautomator2 server port
STUB bool `json:"stub,omitempty" yaml:"stub,omitempty"` // use stub
LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"`
// adb
AdbServerHost string `json:"adb_server_host,omitempty" yaml:"adb_server_host,omitempty"`
AdbServerPort int `json:"adb_server_port,omitempty" yaml:"adb_server_port,omitempty"`
// uiautomator2
UIA2 bool `json:"uia2,omitempty" yaml:"uia2,omitempty"` // use uiautomator2
UIA2IP string `json:"uia2_ip,omitempty" yaml:"uia2_ip,omitempty"` // uiautomator2 server ip
UIA2Port int `json:"uia2_port,omitempty" yaml:"uia2_port,omitempty"` // uiautomator2 server port
UIA2ServerPackageName string `json:"uia2_server_package_name,omitempty" yaml:"uia2_server_package_name,omitempty"`
UIA2ServerTestPackageName string `json:"uia2_server_test_package_name,omitempty" yaml:"uia2_server_test_package_name,omitempty"`
}
func (dev *AndroidDeviceConfig) Options() (deviceOptions []AndroidDeviceOption) {
func (dev *AndroidDeviceOptions) Options() (deviceOptions []AndroidDeviceOption) {
if dev.SerialNumber != "" {
deviceOptions = append(deviceOptions, WithSerialNumber(dev.SerialNumber))
}
if dev.STUB {
deviceOptions = append(deviceOptions, WithStub(true))
}
if dev.UIA2 {
deviceOptions = append(deviceOptions, WithUIA2(true))
}
@@ -28,54 +41,87 @@ func (dev *AndroidDeviceConfig) Options() (deviceOptions []AndroidDeviceOption)
return
}
func NewAndroidDeviceConfig(opts ...AndroidDeviceOption) *AndroidDeviceConfig {
config := &AndroidDeviceConfig{}
const (
// adb server
defaultAdbServerHost = "localhost"
defaultAdbServerPort = gadb.AdbServerPort // 5037
// uiautomator2 server
defaultUIA2ServerHost = "localhost"
defaultUIA2ServerPort = 6790
defaultUIA2ServerPackageName = "io.appium.uiautomator2.server"
defaultUIA2ServerTestPackageName = "io.appium.uiautomator2.server.test"
)
func NewAndroidDeviceOptions(opts ...AndroidDeviceOption) *AndroidDeviceOptions {
config := &AndroidDeviceOptions{}
for _, opt := range opts {
opt(config)
}
// set default
if config.AdbServerHost == "" {
config.AdbServerHost = defaultAdbServerHost
}
if config.AdbServerPort == 0 {
config.AdbServerPort = defaultAdbServerPort
}
if config.UIA2 {
if config.UIA2IP == "" && config.UIA2Port == 0 {
config.UIA2IP = defaultUIA2ServerHost
config.UIA2Port = defaultUIA2ServerPort
}
if config.UIA2ServerPackageName == "" {
config.UIA2ServerPackageName = defaultUIA2ServerPackageName
}
if config.UIA2ServerTestPackageName == "" {
config.UIA2ServerTestPackageName = defaultUIA2ServerTestPackageName
}
}
return config
}
type AndroidDeviceOption func(*AndroidDeviceConfig)
type AndroidDeviceOption func(*AndroidDeviceOptions)
func WithDriverTypeADB() AndroidDeviceOption {
return func(device *AndroidDeviceConfig) {
return func(device *AndroidDeviceOptions) {
device.STUB = false
}
}
func WithSerialNumber(serial string) AndroidDeviceOption {
return func(device *AndroidDeviceConfig) {
return func(device *AndroidDeviceOptions) {
device.SerialNumber = serial
}
}
func WithUIA2(uia2On bool) AndroidDeviceOption {
return func(device *AndroidDeviceConfig) {
return func(device *AndroidDeviceOptions) {
device.UIA2 = uia2On
}
}
func WithStub(stubOn bool) AndroidDeviceOption {
return func(device *AndroidDeviceConfig) {
return func(device *AndroidDeviceOptions) {
device.STUB = stubOn
}
}
func WithUIA2IP(ip string) AndroidDeviceOption {
return func(device *AndroidDeviceConfig) {
return func(device *AndroidDeviceOptions) {
device.UIA2IP = ip
}
}
func WithUIA2Port(port int) AndroidDeviceOption {
return func(device *AndroidDeviceConfig) {
return func(device *AndroidDeviceOptions) {
device.UIA2Port = port
}
}
func WithAdbLogOn(logOn bool) AndroidDeviceOption {
return func(device *AndroidDeviceConfig) {
return func(device *AndroidDeviceOptions) {
device.LogOn = logOn
}
}

View File

@@ -1,11 +1,11 @@
package option
type HarmonyDeviceConfig struct {
type HarmonyDeviceOptions struct {
ConnectKey string `json:"connect_key,omitempty" yaml:"connect_key,omitempty"`
LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"`
}
func (dev *HarmonyDeviceConfig) Options() (deviceOptions []HarmonyDeviceOption) {
func (dev *HarmonyDeviceOptions) Options() (deviceOptions []HarmonyDeviceOption) {
if dev.ConnectKey != "" {
deviceOptions = append(deviceOptions, WithConnectKey(dev.ConnectKey))
}
@@ -15,24 +15,24 @@ func (dev *HarmonyDeviceConfig) Options() (deviceOptions []HarmonyDeviceOption)
return
}
func NewHarmonyDeviceConfig(opts ...HarmonyDeviceOption) (device *HarmonyDeviceConfig) {
device = &HarmonyDeviceConfig{}
func NewHarmonyDeviceOptions(opts ...HarmonyDeviceOption) (device *HarmonyDeviceOptions) {
device = &HarmonyDeviceOptions{}
for _, option := range opts {
option(device)
}
return
}
type HarmonyDeviceOption func(*HarmonyDeviceConfig)
type HarmonyDeviceOption func(*HarmonyDeviceOptions)
func WithConnectKey(connectKey string) HarmonyDeviceOption {
return func(device *HarmonyDeviceConfig) {
return func(device *HarmonyDeviceOptions) {
device.ConnectKey = connectKey
}
}
func WithLogOn(logOn bool) HarmonyDeviceOption {
return func(device *HarmonyDeviceConfig) {
return func(device *HarmonyDeviceOptions) {
device.LogOn = logOn
}
}

View File

@@ -1,11 +1,11 @@
package option
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"`
type IOSDeviceOptions struct {
UDID string `json:"udid,omitempty" yaml:"udid,omitempty"`
WDAPort int `json:"port,omitempty" yaml:"port,omitempty"` // WDA remote port
WDAMjpegPort 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"`
@@ -16,15 +16,15 @@ type IOSDeviceConfig struct {
DismissAlertButtonSelector string `json:"dismiss_alert_button_selector,omitempty" yaml:"dismiss_alert_button_selector,omitempty"`
}
func (dev *IOSDeviceConfig) Options() (deviceOptions []IOSDeviceOption) {
func (dev *IOSDeviceOptions) Options() (deviceOptions []IOSDeviceOption) {
if dev.UDID != "" {
deviceOptions = append(deviceOptions, WithUDID(dev.UDID))
}
if dev.Port != 0 {
deviceOptions = append(deviceOptions, WithWDAPort(dev.Port))
if dev.WDAPort != 0 {
deviceOptions = append(deviceOptions, WithWDAPort(dev.WDAPort))
}
if dev.MjpegPort != 0 {
deviceOptions = append(deviceOptions, WithWDAMjpegPort(dev.MjpegPort))
if dev.WDAMjpegPort != 0 {
deviceOptions = append(deviceOptions, WithWDAMjpegPort(dev.WDAMjpegPort))
}
if dev.STUB {
deviceOptions = append(deviceOptions, WithIOSStub(true))
@@ -47,66 +47,108 @@ func (dev *IOSDeviceConfig) Options() (deviceOptions []IOSDeviceOption) {
return
}
func NewIOSDeviceConfig(opts ...IOSDeviceOption) *IOSDeviceConfig {
config := &IOSDeviceConfig{}
const (
defaultWDAPort = 8100
defaultMjpegPort = 9100
)
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,
// but it might restrict the depth of source tree.
// A part of elements source tree might be lost if the value was too small. Defaults to 50
defaultSnapshotMaxDepth = 10
// Allows to customize accept/dismiss alert button selector.
// It helps you to handle an arbitrary element as accept button in accept alert command.
// The selector should be a valid class chain expression, where the search root is the alert element itself.
// The default button location algorithm is used if the provided selector is wrong or does not match any element.
// e.g. **/XCUIElementTypeButton[`label CONTAINS[c] accept`]
acceptAlertButtonSelector = "**/XCUIElementTypeButton[`label IN {'允许','好','仅在使用应用期间','稍后再说'}`]"
dismissAlertButtonSelector = "**/XCUIElementTypeButton[`label IN {'不允许','暂不'}`]"
)
func NewIOSDeviceOptions(opts ...IOSDeviceOption) *IOSDeviceOptions {
config := &IOSDeviceOptions{}
for _, opt := range opts {
opt(config)
}
if config.WDAPort == 0 {
config.WDAPort = defaultWDAPort
}
if config.WDAMjpegPort == 0 {
config.WDAMjpegPort = defaultMjpegPort
}
if config.SnapshotMaxDepth == 0 {
config.SnapshotMaxDepth = defaultSnapshotMaxDepth
}
if config.AcceptAlertButtonSelector == "" {
config.AcceptAlertButtonSelector = acceptAlertButtonSelector
}
if config.DismissAlertButtonSelector == "" {
config.DismissAlertButtonSelector = dismissAlertButtonSelector
}
// switch to iOS springboard before init WDA session
// avoid getting stuck when some super app is active such as douyin or wexin
config.ResetHomeOnStartup = true
return config
}
type IOSDeviceOption func(*IOSDeviceConfig)
type IOSDeviceOption func(*IOSDeviceOptions)
func WithUDID(udid string) IOSDeviceOption {
return func(device *IOSDeviceConfig) {
return func(device *IOSDeviceOptions) {
device.UDID = udid
}
}
func WithWDAPort(port int) IOSDeviceOption {
return func(device *IOSDeviceConfig) {
device.Port = port
return func(device *IOSDeviceOptions) {
device.WDAPort = port
}
}
func WithWDAMjpegPort(port int) IOSDeviceOption {
return func(device *IOSDeviceConfig) {
device.MjpegPort = port
return func(device *IOSDeviceOptions) {
device.WDAMjpegPort = port
}
}
func WithWDALogOn(logOn bool) IOSDeviceOption {
return func(device *IOSDeviceConfig) {
return func(device *IOSDeviceOptions) {
device.LogOn = logOn
}
}
func WithIOSStub(stub bool) IOSDeviceOption {
return func(device *IOSDeviceConfig) {
return func(device *IOSDeviceOptions) {
device.STUB = stub
}
}
func WithResetHomeOnStartup(reset bool) IOSDeviceOption {
return func(device *IOSDeviceConfig) {
return func(device *IOSDeviceOptions) {
device.ResetHomeOnStartup = reset
}
}
func WithSnapshotMaxDepth(depth int) IOSDeviceOption {
return func(device *IOSDeviceConfig) {
return func(device *IOSDeviceOptions) {
device.SnapshotMaxDepth = depth
}
}
func WithAcceptAlertButtonSelector(selector string) IOSDeviceOption {
return func(device *IOSDeviceConfig) {
return func(device *IOSDeviceOptions) {
device.AcceptAlertButtonSelector = selector
}
}
func WithDismissAlertButtonSelector(selector string) IOSDeviceOption {
return func(device *IOSDeviceConfig) {
return func(device *IOSDeviceOptions) {
device.DismissAlertButtonSelector = selector
}
}