mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-30 21:09:36 +08:00
refactor: NewStubDriver
This commit is contained in:
@@ -173,15 +173,13 @@ func (dev *AndroidDevice) LogEnabled() bool {
|
||||
}
|
||||
|
||||
func (dev *AndroidDevice) NewDriver(opts ...option.DriverOption) (driverExt *DriverExt, err error) {
|
||||
options := option.NewDriverOptions(opts...)
|
||||
|
||||
var driver IWebDriver
|
||||
if dev.UIA2 || dev.LogOn {
|
||||
driver, err = NewUIA2Driver(dev, opts...)
|
||||
} else if dev.STUB {
|
||||
driver, err = dev.NewStubDriver(options.Capabilities)
|
||||
driver, err = NewStubDriver(dev, opts...)
|
||||
} else {
|
||||
driver, err = NewADBDriver(dev)
|
||||
driver, err = NewADBDriver(dev, opts...)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to init UIA driver")
|
||||
@@ -202,36 +200,6 @@ func (dev *AndroidDevice) NewDriver(opts ...option.DriverOption) (driverExt *Dri
|
||||
return driverExt, nil
|
||||
}
|
||||
|
||||
func (dev *AndroidDevice) NewStubDriver(capabilities option.Capabilities) (driver *StubAndroidDriver, err error) {
|
||||
socketLocalPort, err := dev.d.Forward(StubSocketName)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(code.DeviceConnectionError,
|
||||
fmt.Sprintf("forward port %d->%s failed: %v",
|
||||
socketLocalPort, StubSocketName, err))
|
||||
}
|
||||
|
||||
serverLocalPort, err := dev.d.Forward(DouyinServerPort)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(code.DeviceConnectionError,
|
||||
fmt.Sprintf("forward port %d->%d failed: %v",
|
||||
serverLocalPort, DouyinServerPort, err))
|
||||
}
|
||||
|
||||
rawURL := fmt.Sprintf("http://forward-to-%d:%d",
|
||||
serverLocalPort, DouyinServerPort)
|
||||
|
||||
stubDriver, err := newStubAndroidDriver(fmt.Sprintf("127.0.0.1:%d", socketLocalPort), rawURL)
|
||||
if err != nil {
|
||||
_ = dev.d.ForwardKill(socketLocalPort)
|
||||
_ = dev.d.ForwardKill(serverLocalPort)
|
||||
return nil, errors.Wrap(code.DeviceConnectionError, err.Error())
|
||||
}
|
||||
stubDriver.adbClient = dev.d
|
||||
stubDriver.logcat = dev.logcat
|
||||
|
||||
return stubDriver, nil
|
||||
}
|
||||
|
||||
func (dev *AndroidDevice) StartPerf() error {
|
||||
// TODO
|
||||
return nil
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package uixt
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -10,12 +9,55 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/code"
|
||||
"github.com/httprunner/httprunner/v5/internal/json"
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/option"
|
||||
)
|
||||
|
||||
const StubSocketName = "com.bytest.device"
|
||||
|
||||
func NewStubDriver(device *AndroidDevice, opts ...option.DriverOption) (driver *StubAndroidDriver, err error) {
|
||||
socketLocalPort, err := device.d.Forward(StubSocketName)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(code.DeviceConnectionError,
|
||||
fmt.Sprintf("forward port %d->%s failed: %v",
|
||||
socketLocalPort, StubSocketName, err))
|
||||
}
|
||||
|
||||
serverLocalPort, err := device.d.Forward(DouyinServerPort)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(code.DeviceConnectionError,
|
||||
fmt.Sprintf("forward port %d->%d failed: %v",
|
||||
serverLocalPort, DouyinServerPort, err))
|
||||
}
|
||||
|
||||
address := fmt.Sprintf("127.0.0.1:%d", socketLocalPort)
|
||||
conn, err := net.Dial("tcp", address)
|
||||
if err != nil {
|
||||
log.Err(err).Msg(fmt.Sprintf("failed to connect %s", address))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
driver = &StubAndroidDriver{
|
||||
socket: conn,
|
||||
timeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
rawURL := fmt.Sprintf("http://forward-to-%d:%d",
|
||||
serverLocalPort, DouyinServerPort)
|
||||
if driver.urlPrefix, err = url.Parse(rawURL); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
driver.NewSession(nil)
|
||||
driver.adbClient = device.d
|
||||
driver.logcat = device.logcat
|
||||
return driver, nil
|
||||
}
|
||||
|
||||
type StubAndroidDriver struct {
|
||||
socket net.Conn
|
||||
seq int
|
||||
@@ -23,41 +65,12 @@ type StubAndroidDriver struct {
|
||||
ADBDriver
|
||||
}
|
||||
|
||||
const StubSocketName = "com.bytest.device"
|
||||
|
||||
type AppLoginInfo struct {
|
||||
Did string `json:"did,omitempty" yaml:"did,omitempty"`
|
||||
Uid string `json:"uid,omitempty" yaml:"uid,omitempty"`
|
||||
IsLogin bool `json:"is_login,omitempty" yaml:"is_login,omitempty"`
|
||||
}
|
||||
|
||||
// newStubAndroidDriver
|
||||
// 创建stub Driver address为forward后的端口格式127.0.0.1:${port}
|
||||
func newStubAndroidDriver(address string, urlPrefix string, readTimeout ...time.Duration) (*StubAndroidDriver, error) {
|
||||
timeout := 10 * time.Second
|
||||
if len(readTimeout) > 0 {
|
||||
timeout = readTimeout[0]
|
||||
}
|
||||
|
||||
conn, err := net.Dial("tcp", address)
|
||||
if err != nil {
|
||||
log.Err(err).Msg(fmt.Sprintf("failed to connect %s", address))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
driver := &StubAndroidDriver{
|
||||
socket: conn,
|
||||
timeout: timeout,
|
||||
}
|
||||
|
||||
if driver.urlPrefix, err = url.Parse(urlPrefix); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
driver.NewSession(nil)
|
||||
return driver, nil
|
||||
}
|
||||
|
||||
func (sad *StubAndroidDriver) httpGET(pathElem ...string) (rawResp rawResponse, err error) {
|
||||
var localPort int
|
||||
{
|
||||
|
||||
@@ -14,7 +14,7 @@ func setupStubDriver(t *testing.T) {
|
||||
device, err := NewAndroidDevice()
|
||||
checkErr(t, err)
|
||||
device.STUB = true
|
||||
androidStubDriver, err = device.NewStubDriver(option.Capabilities{})
|
||||
androidStubDriver, err = NewStubDriver(device)
|
||||
checkErr(t, err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user