mirror of
https://github.com/httprunner/httprunner.git
synced 2026-08-28 11:36:56 +08:00
refactor: init WDA HTTP driver
This commit is contained in:
@@ -14,7 +14,7 @@ func TestIOSDouyinLive(t *testing.T) {
|
|||||||
WithVariables(map[string]interface{}{
|
WithVariables(map[string]interface{}{
|
||||||
"app_name": "抖音",
|
"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{
|
TestSteps: []hrp.IStep{
|
||||||
hrp.NewStep("启动抖音").
|
hrp.NewStep("启动抖音").
|
||||||
IOS().
|
IOS().
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
func TestIOSWeixinLive(t *testing.T) {
|
func TestIOSWeixinLive(t *testing.T) {
|
||||||
testCase := &hrp.TestCase{
|
testCase := &hrp.TestCase{
|
||||||
Config: hrp.NewConfig("通过 feed 卡片进入微信直播间").
|
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{
|
TestSteps: []hrp.IStep{
|
||||||
hrp.NewStep("启动微信").
|
hrp.NewStep("启动微信").
|
||||||
IOS().
|
IOS().
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
//go:build !localtest
|
//go:build localtest
|
||||||
|
|
||||||
package uitest
|
package uitest
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ func TestWDALog(t *testing.T) {
|
|||||||
WithVariables(map[string]interface{}{
|
WithVariables(map[string]interface{}{
|
||||||
"app_name": "抖音",
|
"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{
|
TestSteps: []hrp.IStep{
|
||||||
hrp.NewStep("查看时间戳").
|
hrp.NewStep("查看时间戳").
|
||||||
IOS().
|
IOS().
|
||||||
|
|||||||
@@ -43,39 +43,32 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func InitWDAClient(device *IOSDevice) (*DriverExt, error) {
|
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
|
// init wda device
|
||||||
iosDevice, err := NewIOSDevice(deviceOptions...)
|
iosDevice, err := NewIOSDevice(device.opitons()...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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
|
// init WDA driver
|
||||||
capabilities := NewCapabilities()
|
capabilities := NewCapabilities()
|
||||||
capabilities.WithDefaultAlertAction(AlertActionAccept)
|
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 {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to init WDA driver")
|
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)
|
driverExt, err := Extend(driver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to extend WebDriver")
|
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) {
|
return func(device *IOSDevice) {
|
||||||
device.Port = port
|
device.Port = port
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithMjpegPort(port int) IOSDeviceOption {
|
func WithWDAMjpegPort(port int) IOSDeviceOption {
|
||||||
return func(device *IOSDevice) {
|
return func(device *IOSDevice) {
|
||||||
device.MjpegPort = port
|
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 {
|
func WithLogOn(logOn bool) IOSDeviceOption {
|
||||||
return func(device *IOSDevice) {
|
return func(device *IOSDevice) {
|
||||||
device.LogOn = logOn
|
device.LogOn = logOn
|
||||||
@@ -161,43 +166,71 @@ func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type IOSDevice struct {
|
type IOSDevice struct {
|
||||||
d giDevice.Device
|
d giDevice.Device
|
||||||
UDID string `json:"udid,omitempty" yaml:"udid,omitempty"`
|
UDID string `json:"udid,omitempty" yaml:"udid,omitempty"`
|
||||||
Port int `json:"port,omitempty" yaml:"port,omitempty"`
|
Port int `json:"port,omitempty" yaml:"port,omitempty"` // WDA remote port
|
||||||
MjpegPort int `json:"mjpeg_port,omitempty" yaml:"mjpeg_port,omitempty"`
|
MjpegPort int `json:"mjpeg_port,omitempty" yaml:"mjpeg_port,omitempty"` // WDA remote MJPEG port
|
||||||
LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"`
|
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 {
|
func (dev *IOSDevice) UUID() string {
|
||||||
return dev.UDID
|
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.
|
// 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) {
|
func (dev *IOSDevice) NewHTTPDriver(capabilities Capabilities) (driver WebDriver, err error) {
|
||||||
|
log.Info().Interface("capabilities", capabilities).Msg("init WDA HTTP driver")
|
||||||
wd := new(wdaDriver)
|
wd := new(wdaDriver)
|
||||||
wd.client = http.DefaultClient
|
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"
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
var sessionInfo SessionInfo
|
var sessionInfo SessionInfo
|
||||||
@@ -208,7 +241,7 @@ func (dev *IOSDevice) NewHTTPDriver(capabilities Capabilities) (driver WebDriver
|
|||||||
|
|
||||||
if wd.mjpegHTTPConn, err = net.Dial(
|
if wd.mjpegHTTPConn, err = net.Dial(
|
||||||
"tcp",
|
"tcp",
|
||||||
fmt.Sprintf("%s:%d", host, mjpeg_port),
|
fmt.Sprintf("%s:%d", host, dev.LocalMjpegPort),
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
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.
|
// 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) {
|
func (dev *IOSDevice) NewUSBDriver(capabilities Capabilities) (driver WebDriver, err error) {
|
||||||
|
log.Info().Interface("capabilities", capabilities).Msg("init WDA USB driver")
|
||||||
wd := new(wdaDriver)
|
wd := new(wdaDriver)
|
||||||
|
|
||||||
if wd.defaultConn, err = dev.d.NewConnect(dev.Port, 0); err != nil {
|
if wd.defaultConn, err = dev.d.NewConnect(dev.Port, 0); err != nil {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
//go:build localtest
|
//go:build localtest
|
||||||
|
|
||||||
package uixt
|
package uixt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -42,12 +43,12 @@ func TestNewIOSDevice(t *testing.T) {
|
|||||||
t.Log(device)
|
t.Log(device)
|
||||||
}
|
}
|
||||||
|
|
||||||
device, _ = NewIOSDevice(WithPort(8700), WithMjpegPort(8800))
|
device, _ = NewIOSDevice(WithWDAPort(8700), WithWDAMjpegPort(8800))
|
||||||
if device != nil {
|
if device != nil {
|
||||||
t.Log(device)
|
t.Log(device)
|
||||||
}
|
}
|
||||||
|
|
||||||
device, _ = NewIOSDevice(WithUDID("xxxx"), WithPort(8700), WithMjpegPort(8800))
|
device, _ = NewIOSDevice(WithUDID("xxxx"), WithWDAPort(8700), WithWDAMjpegPort(8800))
|
||||||
if device != nil {
|
if device != nil {
|
||||||
t.Log(device)
|
t.Log(device)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
v4.3.0-beta-09292312
|
v4.3.0-beta-09301627
|
||||||
+6
-4
@@ -11,10 +11,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
WithUDID = uixt.WithUDID
|
WithUDID = uixt.WithUDID
|
||||||
WithPort = uixt.WithPort
|
WithWDAPort = uixt.WithWDAPort
|
||||||
WithMjpegPort = uixt.WithMjpegPort
|
WithWDAMjpegPort = uixt.WithWDAMjpegPort
|
||||||
WithLogOn = uixt.WithLogOn
|
WithWDALocalPort = uixt.WithWDALocalPort
|
||||||
|
WithWDALocalMjpegPort = uixt.WithWDALocalMjpegPort
|
||||||
|
WithLogOn = uixt.WithLogOn
|
||||||
)
|
)
|
||||||
|
|
||||||
type IOSStep struct {
|
type IOSStep struct {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
__version__ = "v4.3.0-beta-09292312"
|
__version__ = "v4.3.0-beta-09301627"
|
||||||
__description__ = "One-stop solution for HTTP(S) testing."
|
__description__ = "One-stop solution for HTTP(S) testing."
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = "httprunner"
|
name = "httprunner"
|
||||||
version = "v4.3.0-beta-09292312"
|
version = "v4.3.0-beta-09301627"
|
||||||
description = "One-stop solution for HTTP(S) testing."
|
description = "One-stop solution for HTTP(S) testing."
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
|||||||
Reference in New Issue
Block a user