mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-07 16:40:50 +08:00
refactor: move harmony options to pkg/uixt/options
This commit is contained in:
@@ -139,23 +139,23 @@ func (c *TConfig) SetIOS(opts ...options.IOSDeviceOption) *TConfig {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *TConfig) SetHarmony(options ...uixt.HarmonyDeviceOption) *TConfig {
|
func (c *TConfig) SetHarmony(opts ...options.HarmonyDeviceOption) *TConfig {
|
||||||
harmonyOptions := &uixt.HarmonyDevice{}
|
harmonyOptions := options.NewHarmonyDeviceConfig(opts...)
|
||||||
for _, option := range options {
|
device := &uixt.HarmonyDevice{
|
||||||
option(harmonyOptions)
|
HarmonyDeviceConfig: harmonyOptions,
|
||||||
}
|
}
|
||||||
|
|
||||||
// each device can have its own settings
|
// each device can have its own settings
|
||||||
if harmonyOptions.ConnectKey != "" {
|
if harmonyOptions.ConnectKey != "" {
|
||||||
c.Harmony = append(c.Harmony, harmonyOptions)
|
c.Harmony = append(c.Harmony, device)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// device UDID is not specified, settings will be shared
|
// device UDID is not specified, settings will be shared
|
||||||
if len(c.Harmony) == 0 {
|
if len(c.Harmony) == 0 {
|
||||||
c.Harmony = append(c.Harmony, harmonyOptions)
|
c.Harmony = append(c.Harmony, device)
|
||||||
} else {
|
} else {
|
||||||
c.Harmony[0] = harmonyOptions
|
c.Harmony[0] = device
|
||||||
}
|
}
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
hrp "github.com/httprunner/httprunner/v5"
|
hrp "github.com/httprunner/httprunner/v5"
|
||||||
"github.com/httprunner/httprunner/v5/pkg/uixt"
|
"github.com/httprunner/httprunner/v5/pkg/uixt"
|
||||||
|
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHarmonyDouyinE2E(t *testing.T) {
|
func TestHarmonyDouyinE2E(t *testing.T) {
|
||||||
@@ -14,7 +15,9 @@ func TestHarmonyDouyinE2E(t *testing.T) {
|
|||||||
"device": "${ENV(SerialNumber)}",
|
"device": "${ENV(SerialNumber)}",
|
||||||
"ups": "${ENV(LIVEUPLIST)}",
|
"ups": "${ENV(LIVEUPLIST)}",
|
||||||
}).
|
}).
|
||||||
SetHarmony(uixt.WithConnectKey("$device"), uixt.WithLogOn(true)),
|
SetHarmony(
|
||||||
|
options.WithConnectKey("$device"),
|
||||||
|
options.WithLogOn(true)),
|
||||||
TestSteps: []hrp.IStep{
|
TestSteps: []hrp.IStep{
|
||||||
hrp.NewStep("启动抖音").
|
hrp.NewStep("启动抖音").
|
||||||
Harmony().
|
Harmony().
|
||||||
|
|||||||
+14
-39
@@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
"github.com/httprunner/httprunner/v5/code"
|
"github.com/httprunner/httprunner/v5/code"
|
||||||
|
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -16,61 +17,35 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type HarmonyDevice struct {
|
type HarmonyDevice struct {
|
||||||
d *ghdc.Device
|
*options.HarmonyDeviceConfig
|
||||||
ConnectKey string `json:"connect_key,omitempty" yaml:"connect_key,omitempty"`
|
d *ghdc.Device
|
||||||
LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dev *HarmonyDevice) Options() (deviceOptions []HarmonyDeviceOption) {
|
func NewHarmonyDevice(opts ...options.HarmonyDeviceOption) (device *HarmonyDevice, err error) {
|
||||||
if dev.ConnectKey != "" {
|
deviceConfig := options.NewHarmonyDeviceConfig(opts...)
|
||||||
deviceOptions = append(deviceOptions, WithConnectKey(dev.ConnectKey))
|
|
||||||
}
|
|
||||||
if dev.LogOn {
|
|
||||||
deviceOptions = append(deviceOptions, WithLogOn(true))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
type HarmonyDeviceOption func(*HarmonyDevice)
|
deviceList, err := GetHarmonyDevices(deviceConfig.ConnectKey)
|
||||||
|
|
||||||
func WithConnectKey(connectKey string) HarmonyDeviceOption {
|
|
||||||
return func(device *HarmonyDevice) {
|
|
||||||
device.ConnectKey = connectKey
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func WithLogOn(logOn bool) HarmonyDeviceOption {
|
|
||||||
return func(device *HarmonyDevice) {
|
|
||||||
device.LogOn = logOn
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewHarmonyDevice(options ...HarmonyDeviceOption) (device *HarmonyDevice, err error) {
|
|
||||||
device = &HarmonyDevice{}
|
|
||||||
for _, option := range options {
|
|
||||||
option(device)
|
|
||||||
}
|
|
||||||
|
|
||||||
deviceList, err := GetHarmonyDevices(device.ConnectKey)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(code.DeviceConnectionError, err.Error())
|
return nil, errors.Wrap(code.DeviceConnectionError, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if device.ConnectKey == "" && len(deviceList) > 1 {
|
if deviceConfig.ConnectKey == "" && len(deviceList) > 1 {
|
||||||
return nil, errors.Wrap(code.DeviceConnectionError, "more than one device connected, please specify the serial")
|
return nil, errors.Wrap(code.DeviceConnectionError, "more than one device connected, please specify the serial")
|
||||||
}
|
}
|
||||||
|
|
||||||
dev := deviceList[0]
|
dev := deviceList[0]
|
||||||
|
if deviceConfig.ConnectKey == "" {
|
||||||
if device.ConnectKey == "" {
|
|
||||||
selectSerial := dev.Serial()
|
selectSerial := dev.Serial()
|
||||||
device.ConnectKey = selectSerial
|
deviceConfig.ConnectKey = selectSerial
|
||||||
log.Warn().
|
log.Warn().
|
||||||
Str("connectKey", device.ConnectKey).
|
Str("connectKey", deviceConfig.ConnectKey).
|
||||||
Msg("harmony ConnectKey is not specified, select the first one")
|
Msg("harmony ConnectKey is not specified, select the first one")
|
||||||
}
|
}
|
||||||
|
|
||||||
device.d = dev
|
device = &HarmonyDevice{
|
||||||
|
HarmonyDeviceConfig: deviceConfig,
|
||||||
|
d: dev,
|
||||||
|
}
|
||||||
log.Info().Str("connectKey", device.ConnectKey).Msg("init harmony device")
|
log.Info().Str("connectKey", device.ConnectKey).Msg("init harmony device")
|
||||||
return device, nil
|
return device, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package options
|
||||||
|
|
||||||
|
type HarmonyDeviceConfig 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) {
|
||||||
|
if dev.ConnectKey != "" {
|
||||||
|
deviceOptions = append(deviceOptions, WithConnectKey(dev.ConnectKey))
|
||||||
|
}
|
||||||
|
if dev.LogOn {
|
||||||
|
deviceOptions = append(deviceOptions, WithLogOn(true))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHarmonyDeviceConfig(options ...HarmonyDeviceOption) (device *HarmonyDeviceConfig) {
|
||||||
|
device = &HarmonyDeviceConfig{}
|
||||||
|
for _, option := range options {
|
||||||
|
option(device)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type HarmonyDeviceOption func(*HarmonyDeviceConfig)
|
||||||
|
|
||||||
|
func WithConnectKey(connectKey string) HarmonyDeviceOption {
|
||||||
|
return func(device *HarmonyDeviceConfig) {
|
||||||
|
device.ConnectKey = connectKey
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithLogOn(logOn bool) HarmonyDeviceOption {
|
||||||
|
return func(device *HarmonyDeviceConfig) {
|
||||||
|
device.LogOn = logOn
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-6
@@ -24,7 +24,6 @@ import (
|
|||||||
"github.com/httprunner/httprunner/v5/internal/builtin"
|
"github.com/httprunner/httprunner/v5/internal/builtin"
|
||||||
"github.com/httprunner/httprunner/v5/internal/json"
|
"github.com/httprunner/httprunner/v5/internal/json"
|
||||||
"github.com/httprunner/httprunner/v5/pkg/httpstat"
|
"github.com/httprunner/httprunner/v5/pkg/httpstat"
|
||||||
"github.com/httprunner/httprunner/v5/pkg/uixt"
|
|
||||||
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
|
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -782,11 +781,8 @@ func (s *StepRequest) IOS(opts ...options.IOSDeviceOption) *StepMobile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Harmony creates a new harmony step session
|
// Harmony creates a new harmony step session
|
||||||
func (s *StepRequest) Harmony(options ...uixt.HarmonyDeviceOption) *StepMobile {
|
func (s *StepRequest) Harmony(opts ...options.HarmonyDeviceOption) *StepMobile {
|
||||||
harmonyOptions := &uixt.HarmonyDevice{}
|
harmonyOptions := options.NewHarmonyDeviceConfig(opts...)
|
||||||
for _, option := range options {
|
|
||||||
option(harmonyOptions)
|
|
||||||
}
|
|
||||||
return &StepMobile{
|
return &StepMobile{
|
||||||
StepConfig: s.StepConfig,
|
StepConfig: s.StepConfig,
|
||||||
Harmony: &MobileUI{
|
Harmony: &MobileUI{
|
||||||
|
|||||||
Reference in New Issue
Block a user