refactor: initUIClient

This commit is contained in:
lilong.129
2024-11-10 00:12:44 +08:00
parent 7cac7742d2
commit 52b45a55d2
6 changed files with 138 additions and 154 deletions

View File

@@ -1 +1 @@
v5.0.0+2411092321 v5.0.0+2411100026

View File

@@ -86,25 +86,6 @@ func WithAdbLogOn(logOn bool) AndroidDeviceOption {
} }
} }
func GetAndroidDeviceOptions(dev *AndroidDevice) (deviceOptions []AndroidDeviceOption) {
if dev.SerialNumber != "" {
deviceOptions = append(deviceOptions, WithSerialNumber(dev.SerialNumber))
}
if dev.UIA2 {
deviceOptions = append(deviceOptions, WithUIA2(true))
}
if dev.UIA2IP != "" {
deviceOptions = append(deviceOptions, WithUIA2IP(dev.UIA2IP))
}
if dev.UIA2Port != 0 {
deviceOptions = append(deviceOptions, WithUIA2Port(dev.UIA2Port))
}
if dev.LogOn {
deviceOptions = append(deviceOptions, WithAdbLogOn(true))
}
return
}
func NewAndroidDevice(options ...AndroidDeviceOption) (device *AndroidDevice, err error) { func NewAndroidDevice(options ...AndroidDeviceOption) (device *AndroidDevice, err error) {
device = &AndroidDevice{ device = &AndroidDevice{
UIA2IP: UIA2ServerHost, UIA2IP: UIA2ServerHost,
@@ -191,6 +172,25 @@ type AndroidDevice struct {
IgnorePopup bool `json:"ignore_popup,omitempty" yaml:"ignore_popup,omitempty"` IgnorePopup bool `json:"ignore_popup,omitempty" yaml:"ignore_popup,omitempty"`
} }
func (dev *AndroidDevice) Options() (deviceOptions []AndroidDeviceOption) {
if dev.SerialNumber != "" {
deviceOptions = append(deviceOptions, WithSerialNumber(dev.SerialNumber))
}
if dev.UIA2 {
deviceOptions = append(deviceOptions, WithUIA2(true))
}
if dev.UIA2IP != "" {
deviceOptions = append(deviceOptions, WithUIA2IP(dev.UIA2IP))
}
if dev.UIA2Port != 0 {
deviceOptions = append(deviceOptions, WithUIA2Port(dev.UIA2Port))
}
if dev.LogOn {
deviceOptions = append(deviceOptions, WithAdbLogOn(true))
}
return
}
func (dev *AndroidDevice) Init() error { func (dev *AndroidDevice) Init() error {
dev.d.RunShellCommand("ime", "enable", "io.appium.settings/.UnicodeIME") dev.d.RunShellCommand("ime", "enable", "io.appium.settings/.UnicodeIME")
dev.d.RunShellCommand("rm", "-r", config.DeviceActionLogFilePath) dev.d.RunShellCommand("rm", "-r", config.DeviceActionLogFilePath)

View File

@@ -22,6 +22,19 @@ type HarmonyDevice struct {
LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"` LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"`
} }
func (dev *HarmonyDevice) Options() (deviceOptions []HarmonyDeviceOption) {
if dev.ConnectKey != "" {
deviceOptions = append(deviceOptions, WithConnectKey(dev.ConnectKey))
}
if dev.IgnorePopup {
deviceOptions = append(deviceOptions, WithIgnorePopup(true))
}
if dev.LogOn {
deviceOptions = append(deviceOptions, WithLogOn(true))
}
return
}
type HarmonyDeviceOption func(*HarmonyDevice) type HarmonyDeviceOption func(*HarmonyDevice)
func WithConnectKey(connectKey string) HarmonyDeviceOption { func WithConnectKey(connectKey string) HarmonyDeviceOption {
@@ -42,19 +55,6 @@ func WithLogOn(logOn bool) HarmonyDeviceOption {
} }
} }
func GetHarmonyDeviceOptions(dev *HarmonyDevice) (deviceOptions []HarmonyDeviceOption) {
if dev.ConnectKey != "" {
deviceOptions = append(deviceOptions, WithConnectKey(dev.ConnectKey))
}
if dev.IgnorePopup {
deviceOptions = append(deviceOptions, WithIgnorePopup(true))
}
if dev.LogOn {
deviceOptions = append(deviceOptions, WithLogOn(true))
}
return
}
func NewHarmonyDevice(options ...HarmonyDeviceOption) (device *HarmonyDevice, err error) { func NewHarmonyDevice(options ...HarmonyDeviceOption) (device *HarmonyDevice, err error) {
device = &HarmonyDevice{} device = &HarmonyDevice{}
for _, option := range options { for _, option := range options {

View File

@@ -182,43 +182,6 @@ func GetIOSDevices(udid ...string) (devices []gidevice.Device, 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.PerfOptions != nil {
deviceOptions = append(deviceOptions, WithIOSPerfOptions(dev.perfOpitons()...))
}
if dev.PcapOptions != nil {
deviceOptions = append(deviceOptions, WithIOSPcapOptions(dev.pcapOpitons()...))
}
if dev.XCTestBundleID != "" {
deviceOptions = append(deviceOptions, WithXCTest(dev.XCTestBundleID))
}
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 NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) { func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) {
device = &IOSDevice{ device = &IOSDevice{
Port: defaultWDAPort, Port: defaultWDAPort,
@@ -296,6 +259,43 @@ type IOSDevice struct {
pcapFile string // saved pcap file path pcapFile string // saved pcap file path
} }
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.LogOn {
deviceOptions = append(deviceOptions, WithWDALogOn(true))
}
if dev.PerfOptions != nil {
deviceOptions = append(deviceOptions, WithIOSPerfOptions(dev.perfOpitons()...))
}
if dev.PcapOptions != nil {
deviceOptions = append(deviceOptions, WithIOSPcapOptions(dev.pcapOpitons()...))
}
if dev.XCTestBundleID != "" {
deviceOptions = append(deviceOptions, WithXCTest(dev.XCTestBundleID))
}
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 (dev *IOSDevice) Init() error { func (dev *IOSDevice) Init() error {
return nil return nil
} }

View File

@@ -26,7 +26,6 @@ import (
"github.com/httprunner/httprunner/v4/hrp/code" "github.com/httprunner/httprunner/v4/hrp/code"
"github.com/httprunner/httprunner/v4/hrp/internal/sdk" "github.com/httprunner/httprunner/v4/hrp/internal/sdk"
"github.com/httprunner/httprunner/v4/hrp/internal/version" "github.com/httprunner/httprunner/v4/hrp/internal/version"
"github.com/httprunner/httprunner/v4/hrp/pkg/uixt"
) )
// Run starts to run testcase with default configs. // Run starts to run testcase with default configs.
@@ -407,69 +406,29 @@ func (r *CaseRunner) parseConfig() (parsedConfig *TConfig, err error) {
} }
r.parametersIterator = parametersIterator r.parametersIterator = parametersIterator
// init android devices // parse android devices config
for _, androidDevice := range parsedConfig.Android { for _, androidDevice := range parsedConfig.Android {
err := r.parseDeviceConfig(androidDevice, parsedConfig.Variables) err := r.parseDeviceConfig(androidDevice, parsedConfig.Variables)
if err != nil { if err != nil {
return nil, errors.Wrap(code.InvalidCaseError, return nil, errors.Wrap(code.InvalidCaseError,
fmt.Sprintf("parse android config failed: %v", err)) fmt.Sprintf("parse android config failed: %v", err))
} }
device, err := uixt.NewAndroidDevice(
uixt.GetAndroidDeviceOptions(androidDevice)...)
if err != nil {
return nil, errors.Wrap(err, "init android device failed")
}
if err := device.Init(); err != nil {
return nil, err
}
client, err := device.NewDriver(uixt.WithDriverPlugin(r.parser.plugin))
if err != nil {
return nil, errors.Wrap(err, "init android driver failed")
}
uiClients[device.SerialNumber] = client
} }
// init iOS devices // parse iOS devices config
for _, iosDevice := range parsedConfig.IOS { for _, iosDevice := range parsedConfig.IOS {
err := r.parseDeviceConfig(iosDevice, parsedConfig.Variables) err := r.parseDeviceConfig(iosDevice, parsedConfig.Variables)
if err != nil { if err != nil {
return nil, errors.Wrap(code.InvalidCaseError, return nil, errors.Wrap(code.InvalidCaseError,
fmt.Sprintf("parse ios config failed: %v", err)) fmt.Sprintf("parse ios config failed: %v", err))
} }
device, err := uixt.NewIOSDevice(
uixt.GetIOSDeviceOptions(iosDevice)...)
if err != nil {
return nil, errors.Wrap(err, "init ios device failed")
}
if err := device.Init(); err != nil {
return nil, err
}
client, err := device.NewDriver(uixt.WithDriverPlugin(r.parser.plugin))
if err != nil {
return nil, errors.Wrap(err, "init ios driver failed")
}
uiClients[device.UDID] = client
} }
// init harmony devices // parse harmony devices config
for _, harmonyDevice := range parsedConfig.Harmony { for _, harmonyDevice := range parsedConfig.Harmony {
err := r.parseDeviceConfig(harmonyDevice, parsedConfig.Variables) err := r.parseDeviceConfig(harmonyDevice, parsedConfig.Variables)
if err != nil { if err != nil {
return nil, errors.Wrap(code.InvalidCaseError, return nil, errors.Wrap(code.InvalidCaseError,
fmt.Sprintf("parse harmony config failed: %v", err)) fmt.Sprintf("parse harmony config failed: %v", err))
} }
device, err := uixt.NewHarmonyDevice(
uixt.GetHarmonyDeviceOptions(harmonyDevice)...)
if err != nil {
return nil, errors.Wrap(err, "init harmony device failed")
}
if err := device.Init(); err != nil {
return nil, err
}
client, err := device.NewDriver(uixt.WithDriverPlugin(r.parser.plugin))
if err != nil {
return nil, errors.Wrap(err, "init harmony driver failed")
}
uiClients[device.ConnectKey] = client
} }
return parsedConfig, nil return parsedConfig, nil

View File

@@ -14,32 +14,54 @@ import (
var uiClients = make(map[string]*uixt.DriverExt) // UI automation clients for iOS and Android, key is udid/serial var uiClients = make(map[string]*uixt.DriverExt) // UI automation clients for iOS and Android, key is udid/serial
func initUIClient(serial, osType string) (client *uixt.DriverExt, err error) { func initUIClient(serial, osType string, caseConfig *TConfig) (client *uixt.DriverExt, err error) {
if uiClients == nil { // get cached driver
uiClients = make(map[string]*uixt.DriverExt) for key, driver := range uiClients {
} // if serial is empty, return the first driver
if serial == "" {
// get the first device return driver, nil
if serial == "" && len(uiClients) > 0 { }
for _, v := range uiClients { // or return the driver with the same serial
return v, nil if key == serial {
} return driver, nil
}
// avoid duplicate init
if serial != "" {
if client, ok := uiClients[serial]; ok {
return client, nil
} }
} }
// init new driver
var device uixt.IDevice var device uixt.IDevice
if osType == "ios" { switch strings.ToLower(osType) {
device, err = uixt.NewIOSDevice(uixt.WithUDID(serial)) case "ios":
} else if osType == "harmony" { for _, ios := range caseConfig.IOS {
device, err = uixt.NewHarmonyDevice(uixt.WithConnectKey(serial)) if serial == "" || ios.UDID == serial {
} else { device, err = uixt.NewIOSDevice(ios.Options()...)
device, err = uixt.NewAndroidDevice(uixt.WithSerialNumber(serial)) break
}
}
if device == nil {
device, err = uixt.NewIOSDevice(uixt.WithUDID(serial))
}
case "harmony":
for _, harmony := range caseConfig.Harmony {
if serial == "" || harmony.ConnectKey == serial {
device, err = uixt.NewHarmonyDevice(harmony.Options()...)
break
}
}
if device == nil {
device, err = uixt.NewHarmonyDevice(uixt.WithConnectKey(serial))
}
case "android":
for _, android := range caseConfig.Android {
if serial == "" || android.SerialNumber == serial {
device, err = uixt.NewAndroidDevice(android.Options()...)
break
}
}
if device == nil {
device, err = uixt.NewAndroidDevice(uixt.WithSerialNumber(serial))
}
default:
return nil, errors.Errorf("unsupported os type: %s", osType)
} }
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "init %s device failed", osType) return nil, errors.Wrapf(err, "init %s device failed", osType)
@@ -55,10 +77,7 @@ func initUIClient(serial, osType string) (client *uixt.DriverExt, err error) {
} }
// cache wda client // cache wda client
if uiClients == nil { uiClients[serial] = client
uiClients = make(map[string]*uixt.DriverExt)
}
uiClients[client.Device.UUID()] = client
return client, nil return client, nil
} }
@@ -73,29 +92,34 @@ type MobileUI struct {
// StepMobile implements IStep interface. // StepMobile implements IStep interface.
type StepMobile struct { type StepMobile struct {
StepConfig StepConfig
stepObj *MobileUI
Android *MobileUI `json:"android,omitempty" yaml:"android,omitempty"` Android *MobileUI `json:"android,omitempty" yaml:"android,omitempty"`
Harmony *MobileUI `json:"harmony,omitempty" yaml:"harmony,omitempty"` Harmony *MobileUI `json:"harmony,omitempty" yaml:"harmony,omitempty"`
IOS *MobileUI `json:"ios,omitempty" yaml:"ios,omitempty"` IOS *MobileUI `json:"ios,omitempty" yaml:"ios,omitempty"`
cache *MobileUI // used for caching
} }
// uniform interface for all types of mobile systems // uniform interface for all types of mobile systems
func (s *StepMobile) obj() *MobileUI { func (s *StepMobile) obj() *MobileUI {
if s.stepObj != nil { if s.cache != nil {
return s.stepObj return s.cache
} }
if s.IOS != nil { if s.IOS != nil {
s.stepObj = s.IOS s.cache = s.IOS
s.stepObj.OSType = string(stepTypeIOS) s.cache.OSType = string(stepTypeIOS)
return s.stepObj return s.cache
} else if s.Harmony != nil { } else if s.Harmony != nil {
s.stepObj = s.Harmony s.cache = s.Harmony
s.stepObj.OSType = string(stepTypeHarmony) s.cache.OSType = string(stepTypeHarmony)
return s.stepObj return s.cache
} else if s.Android != nil {
s.cache = s.Android
s.cache.OSType = string(stepTypeAndroid)
return s.cache
} }
s.stepObj = s.Android
s.stepObj.OSType = string(stepTypeAndroid) panic("no mobile device config")
return s.stepObj
} }
func (s *StepMobile) Serial(serial string) *StepMobile { func (s *StepMobile) Serial(serial string) *StepMobile {
@@ -651,8 +675,9 @@ func runStepMobileUI(s *SessionRunner, step IStep) (stepResult *StepResult, err
ContentSize: 0, ContentSize: 0,
} }
// init wda/uia driver // init wda/uia/hdc driver
uiDriver, err := initUIClient(mobileStep.Serial, mobileStep.OSType) caseConfig := s.caseRunner.TestCase.Config.Get()
uiDriver, err := initUIClient(mobileStep.Serial, mobileStep.OSType, caseConfig)
if err != nil { if err != nil {
return return
} }