From f5e55ef49d6e6bc83b6aca8d2079b05a051be061 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Wed, 25 Sep 2024 22:41:04 +0800 Subject: [PATCH] fix: miss device options --- hrp/internal/version/VERSION | 2 +- hrp/pkg/uixt/harmony_device.go | 13 ++++++++ hrp/runner.go | 58 ++++++++++++++++++++++++++++++---- hrp/step_mobile_ui.go | 20 ++++-------- 4 files changed, 71 insertions(+), 22 deletions(-) diff --git a/hrp/internal/version/VERSION b/hrp/internal/version/VERSION index e71a498d..73bc323e 100644 --- a/hrp/internal/version/VERSION +++ b/hrp/internal/version/VERSION @@ -1 +1 @@ -v5.0.0-beta-2409251646 \ No newline at end of file +v5.0.0-beta-2409252241 \ No newline at end of file diff --git a/hrp/pkg/uixt/harmony_device.go b/hrp/pkg/uixt/harmony_device.go index 8820c962..6a3853d7 100644 --- a/hrp/pkg/uixt/harmony_device.go +++ b/hrp/pkg/uixt/harmony_device.go @@ -42,6 +42,19 @@ 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) { device = &HarmonyDevice{} for _, option := range options { diff --git a/hrp/runner.go b/hrp/runner.go index d3a5f4d6..e106e542 100644 --- a/hrp/runner.go +++ b/hrp/runner.go @@ -26,6 +26,7 @@ import ( "github.com/httprunner/httprunner/v4/hrp/code" "github.com/httprunner/httprunner/v4/hrp/internal/sdk" "github.com/httprunner/httprunner/v4/hrp/internal/version" + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) // Run starts to run testcase with default configs. @@ -405,28 +406,71 @@ func (r *CaseRunner) parseConfig() (parsedConfig *TConfig, err error) { } r.parametersIterator = parametersIterator - // parse android/ios/harmony config - for _, device := range parsedConfig.Android { - err := r.parseDeviceConfig(device, parsedConfig.Variables) + // init android devices + for _, androidDevice := range parsedConfig.Android { + err := r.parseDeviceConfig(androidDevice, parsedConfig.Variables) if err != nil { return nil, errors.Wrap(code.InvalidCaseError, 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 } - for _, device := range parsedConfig.IOS { - err := r.parseDeviceConfig(device, parsedConfig.Variables) + // init iOS devices + for _, iosDevice := range parsedConfig.IOS { + err := r.parseDeviceConfig(iosDevice, parsedConfig.Variables) if err != nil { return nil, errors.Wrap(code.InvalidCaseError, 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 } - for _, device := range parsedConfig.Harmony { - err := r.parseDeviceConfig(device, parsedConfig.Variables) + // init harmony devices + for _, harmonyDevice := range parsedConfig.Harmony { + err := r.parseDeviceConfig(harmonyDevice, parsedConfig.Variables) if err != nil { return nil, errors.Wrap(code.InvalidCaseError, 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 } diff --git a/hrp/step_mobile_ui.go b/hrp/step_mobile_ui.go index 40051572..ea1dd828 100644 --- a/hrp/step_mobile_ui.go +++ b/hrp/step_mobile_ui.go @@ -11,14 +11,14 @@ import ( "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) -var uiClients 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) { if uiClients == nil { uiClients = make(map[string]*uixt.DriverExt) } - // avoid duplicate init + // get the first device if serial == "" && len(uiClients) > 0 { for _, v := range uiClients { return v, nil @@ -44,6 +44,10 @@ func initUIClient(serial, osType string) (client *uixt.DriverExt, err error) { return nil, errors.Wrapf(err, "init %s device failed", osType) } + if err := device.Init(); err != nil { + return nil, err + } + client, err = device.NewDriver() if err != nil { return nil, err @@ -595,26 +599,14 @@ func runStepMobileUI(s *SessionRunner, step *TStep) (stepResult *StepResult, err // ios step osType = "ios" mobileStep = step.IOS - iosDevices := s.caseRunner.Config.IOS - if mobileStep.Serial == "" && len(iosDevices) > 0 { - mobileStep.Serial = iosDevices[0].UDID - } } else if step.Harmony != nil { // harmony step osType = "harmony" mobileStep = step.Harmony - harmonyDevices := s.caseRunner.Config.Harmony - if mobileStep.Serial == "" && len(harmonyDevices) > 0 { - mobileStep.Serial = harmonyDevices[0].ConnectKey - } } else { // android step osType = "android" mobileStep = step.Android - androidDevices := s.caseRunner.Config.Android - if mobileStep.Serial == "" && len(androidDevices) > 0 { - mobileStep.Serial = androidDevices[0].SerialNumber - } } // report GA event