refactor: init WDA HTTP driver

This commit is contained in:
debugtalk
2022-09-30 16:14:58 +08:00
parent 7f929cc72a
commit 30626575dd
9 changed files with 100 additions and 63 deletions

View File

@@ -14,7 +14,7 @@ func TestIOSDouyinLive(t *testing.T) {
WithVariables(map[string]interface{}{
"app_name": "抖音",
}).
SetIOS(hrp.WithLogOn(true), hrp.WithPort(8700), hrp.WithMjpegPort(8800)),
SetIOS(hrp.WithLogOn(true), hrp.WithWDAPort(8700), hrp.WithWDAMjpegPort(8800)),
TestSteps: []hrp.IStep{
hrp.NewStep("启动抖音").
IOS().

View File

@@ -11,7 +11,7 @@ import (
func TestIOSWeixinLive(t *testing.T) {
testCase := &hrp.TestCase{
Config: hrp.NewConfig("通过 feed 卡片进入微信直播间").
SetIOS(hrp.WithLogOn(true), hrp.WithPort(8700), hrp.WithMjpegPort(8800)),
SetIOS(hrp.WithLogOn(true), hrp.WithWDAPort(8700), hrp.WithWDAMjpegPort(8800)),
TestSteps: []hrp.IStep{
hrp.NewStep("启动微信").
IOS().

View File

@@ -1,4 +1,4 @@
//go:build !localtest
//go:build localtest
package uitest
@@ -14,7 +14,7 @@ func TestWDALog(t *testing.T) {
WithVariables(map[string]interface{}{
"app_name": "抖音",
}).
SetIOS(hrp.WithLogOn(true), hrp.WithPort(8700), hrp.WithMjpegPort(8800)),
SetIOS(hrp.WithLogOn(true), hrp.WithWDAPort(8700), hrp.WithWDAMjpegPort(8800)),
TestSteps: []hrp.IStep{
hrp.NewStep("查看时间戳").
IOS().

View File

@@ -43,39 +43,32 @@ const (
)
func InitWDAClient(device *IOSDevice) (*DriverExt, error) {
var deviceOptions []IOSDeviceOption
if device.UDID != "" {
deviceOptions = append(deviceOptions, WithUDID(device.UDID))
}
if device.Port != 0 {
deviceOptions = append(deviceOptions, WithPort(device.Port))
}
if device.MjpegPort != 0 {
deviceOptions = append(deviceOptions, WithMjpegPort(device.MjpegPort))
}
// init wda device
iosDevice, err := NewIOSDevice(deviceOptions...)
iosDevice, err := NewIOSDevice(device.opitons()...)
if err != nil {
return nil, err
}
// switch to iOS springboard before init WDA session
// aviod getting stuck when some super app is activate such as douyin or wexin
log.Info().Msg("switch to iOS springboard")
bundleID := "com.apple.springboard"
_, err = iosDevice.d.AppLaunch(bundleID)
if err != nil {
return nil, errors.Wrap(err, "launch springboard failed")
}
// init WDA driver
capabilities := NewCapabilities()
capabilities.WithDefaultAlertAction(AlertActionAccept)
driver, err := iosDevice.NewUSBDriver(capabilities)
var driver WebDriver
if iosDevice.LocalPort != 0 && iosDevice.LocalMjpegPort != 0 {
driver, err = iosDevice.NewHTTPDriver(capabilities)
} else {
driver, err = iosDevice.NewUSBDriver(capabilities)
}
if err != nil {
return nil, errors.Wrap(err, "failed to init WDA driver")
}
// switch to iOS springboard before init WDA session
// aviod 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")
}
driverExt, err := Extend(driver)
if err != nil {
return nil, errors.Wrap(err, "failed to extend WebDriver")
@@ -108,18 +101,30 @@ func WithUDID(udid string) IOSDeviceOption {
}
}
func WithPort(port int) IOSDeviceOption {
func WithWDAPort(port int) IOSDeviceOption {
return func(device *IOSDevice) {
device.Port = port
}
}
func WithMjpegPort(port int) IOSDeviceOption {
func WithWDAMjpegPort(port int) IOSDeviceOption {
return func(device *IOSDevice) {
device.MjpegPort = port
}
}
func WithWDALocalPort(port int) IOSDeviceOption {
return func(device *IOSDevice) {
device.LocalPort = port
}
}
func WithWDALocalMjpegPort(port int) IOSDeviceOption {
return func(device *IOSDevice) {
device.LocalMjpegPort = port
}
}
func WithLogOn(logOn bool) IOSDeviceOption {
return func(device *IOSDevice) {
device.LogOn = logOn
@@ -161,43 +166,71 @@ func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) {
}
type IOSDevice struct {
d giDevice.Device
UDID string `json:"udid,omitempty" yaml:"udid,omitempty"`
Port int `json:"port,omitempty" yaml:"port,omitempty"`
MjpegPort int `json:"mjpeg_port,omitempty" yaml:"mjpeg_port,omitempty"`
LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"`
d giDevice.Device
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
LocalPort int `json:"local_port,omitempty" yaml:"local_port,omitempty"` // WDA local port
LocalMjpegPort int `json:"local_mjpeg_port,omitempty" yaml:"local_mjpeg_port,omitempty"` // WDA local MJPEG port
LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"`
}
func (dev *IOSDevice) UUID() string {
return dev.UDID
}
func (dev *IOSDevice) opitons() (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 wda_port := os.Getenv("WDA_LOCAL_PORT"); wda_port != "" {
if port, err := strconv.Atoi(wda_port); err == nil {
log.Info().Int("WDA_LOCAL_PORT", port).
Msg("override with environment variable")
dev.LocalPort = port
} else {
log.Error().Err(err).Str("WDA_LOCAL_PORT", wda_port).
Msg("invalid WDA_LOCAL_PORT, ignored")
}
}
if wda_mjpeg_port := os.Getenv("WDA_LOCAL_MJPEG_PORT"); wda_mjpeg_port != "" {
if mjpeg_port, err := strconv.Atoi(wda_mjpeg_port); err == nil {
log.Info().Int("WDA_LOCAL_MJPEG_PORT", mjpeg_port).
Msg("override with environment variable")
dev.LocalMjpegPort = mjpeg_port
} else {
log.Error().Err(err).Str("WDA_LOCAL_MJPEG_PORT", wda_mjpeg_port).
Msg("invalid WDA_LOCAL_MJPEG_PORT, ignored")
}
}
if dev.LocalPort != 0 {
deviceOptions = append(deviceOptions, WithWDALocalPort(dev.LocalPort))
}
if dev.LocalMjpegPort != 0 {
deviceOptions = append(deviceOptions, WithWDALocalMjpegPort(dev.LocalMjpegPort))
}
return
}
// NewHTTPDriver creates new remote HTTP client, this will also start a new session.
// WDA port and mjpeg port must be proxied to local ports:
// iproxy -u UDID WDA_LOCAL_PORT WDA_PORT
// iproxy -u UDID WDA_LOCAL_MJPEG_PORT WDA_MJPEG_PORT
func (dev *IOSDevice) NewHTTPDriver(capabilities Capabilities) (driver WebDriver, err error) {
log.Info().Interface("capabilities", capabilities).Msg("init WDA HTTP driver")
wd := new(wdaDriver)
wd.client = http.DefaultClient
port := dev.Port
mjpeg_port := dev.MjpegPort
if wda_port := os.Getenv("WDA_PROXY_PORT"); wda_port != "" {
if port, err = strconv.Atoi(wda_port); err == nil {
log.Info().Str("WDA_PROXY_PORT", wda_port).
Msg("override with environment variable")
} else {
log.Error().Err(err).Msg("invalid WDA_PROXY_PORT, ignored")
}
}
if wda_mjpeg_port := os.Getenv("WDA_PROXY_MJPEG_PORT"); wda_mjpeg_port != "" {
if mjpeg_port, err = strconv.Atoi(wda_mjpeg_port); err == nil {
log.Info().Str("WDA_PROXY_MJPEG_PORT", wda_mjpeg_port).
Msg("override with environment variable")
} else {
log.Error().Err(err).Msg("invalid WDA_PROXY_MJPEG_PORT, ignored")
}
}
host := "127.0.0.1"
if wd.urlPrefix, err = url.Parse(fmt.Sprintf("http://%s:%d", host, port)); err != nil {
if wd.urlPrefix, err = url.Parse(fmt.Sprintf("http://%s:%d", host, dev.LocalPort)); err != nil {
return nil, err
}
var sessionInfo SessionInfo
@@ -208,7 +241,7 @@ func (dev *IOSDevice) NewHTTPDriver(capabilities Capabilities) (driver WebDriver
if wd.mjpegHTTPConn, err = net.Dial(
"tcp",
fmt.Sprintf("%s:%d", host, mjpeg_port),
fmt.Sprintf("%s:%d", host, dev.LocalMjpegPort),
); err != nil {
return nil, err
}
@@ -219,6 +252,7 @@ func (dev *IOSDevice) NewHTTPDriver(capabilities Capabilities) (driver WebDriver
// NewUSBDriver creates new client via USB connected device, this will also start a new session.
func (dev *IOSDevice) NewUSBDriver(capabilities Capabilities) (driver WebDriver, err error) {
log.Info().Interface("capabilities", capabilities).Msg("init WDA USB driver")
wd := new(wdaDriver)
if wd.defaultConn, err = dev.d.NewConnect(dev.Port, 0); err != nil {

View File

@@ -1,4 +1,5 @@
//go:build localtest
package uixt
import (
@@ -42,12 +43,12 @@ func TestNewIOSDevice(t *testing.T) {
t.Log(device)
}
device, _ = NewIOSDevice(WithPort(8700), WithMjpegPort(8800))
device, _ = NewIOSDevice(WithWDAPort(8700), WithWDAMjpegPort(8800))
if device != nil {
t.Log(device)
}
device, _ = NewIOSDevice(WithUDID("xxxx"), WithPort(8700), WithMjpegPort(8800))
device, _ = NewIOSDevice(WithUDID("xxxx"), WithWDAPort(8700), WithWDAMjpegPort(8800))
if device != nil {
t.Log(device)
}

View File

@@ -1 +1 @@
v4.3.0-beta-09292312
v4.3.0-beta-09301627

View File

@@ -11,10 +11,12 @@ import (
)
var (
WithUDID = uixt.WithUDID
WithPort = uixt.WithPort
WithMjpegPort = uixt.WithMjpegPort
WithLogOn = uixt.WithLogOn
WithUDID = uixt.WithUDID
WithWDAPort = uixt.WithWDAPort
WithWDAMjpegPort = uixt.WithWDAMjpegPort
WithWDALocalPort = uixt.WithWDALocalPort
WithWDALocalMjpegPort = uixt.WithWDALocalMjpegPort
WithLogOn = uixt.WithLogOn
)
type IOSStep struct {

View File

@@ -1,4 +1,4 @@
__version__ = "v4.3.0-beta-09292312"
__version__ = "v4.3.0-beta-09301627"
__description__ = "One-stop solution for HTTP(S) testing."

View File

@@ -1,6 +1,6 @@
[tool.poetry]
name = "httprunner"
version = "v4.3.0-beta-09292312"
version = "v4.3.0-beta-09301627"
description = "One-stop solution for HTTP(S) testing."
license = "Apache-2.0"
readme = "README.md"