mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-21 04:22:30 +08:00
refactor: replace NewDriver args with options
This commit is contained in:
@@ -102,7 +102,7 @@ type WorldCupLive struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewWorldCupLive(device uixt.Device, matchName, bundleID string, duration, interval int) *WorldCupLive {
|
func NewWorldCupLive(device uixt.Device, matchName, bundleID string, duration, interval int) *WorldCupLive {
|
||||||
driverExt, err := device.NewDriver(nil)
|
driverExt, err := device.NewDriver()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("failed to init driver")
|
log.Fatal().Err(err).Msg("failed to init driver")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -154,10 +154,15 @@ func (dev *AndroidDevice) LogEnabled() bool {
|
|||||||
return dev.LogOn
|
return dev.LogOn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dev *AndroidDevice) NewDriver(capabilities Capabilities) (driverExt *DriverExt, err error) {
|
func (dev *AndroidDevice) NewDriver(options ...DriverOption) (driverExt *DriverExt, err error) {
|
||||||
|
driverOptions := &DriverOptions{}
|
||||||
|
for _, option := range options {
|
||||||
|
option(driverOptions)
|
||||||
|
}
|
||||||
|
|
||||||
var driver WebDriver
|
var driver WebDriver
|
||||||
if dev.UIA2 {
|
if dev.UIA2 {
|
||||||
driver, err = dev.NewUSBDriver(capabilities)
|
driver, err = dev.NewUSBDriver(driverOptions.capabilities)
|
||||||
} else {
|
} else {
|
||||||
driver, err = dev.NewAdbDriver()
|
driver, err = dev.NewAdbDriver()
|
||||||
}
|
}
|
||||||
@@ -165,7 +170,7 @@ func (dev *AndroidDevice) NewDriver(capabilities Capabilities) (driverExt *Drive
|
|||||||
return nil, errors.Wrap(err, "failed to init UIA driver")
|
return nil, errors.Wrap(err, "failed to init UIA driver")
|
||||||
}
|
}
|
||||||
|
|
||||||
driverExt, err = NewDriverExt(dev, driver)
|
driverExt, err = newDriverExt(dev, driver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ var (
|
|||||||
func setupAndroid(t *testing.T) {
|
func setupAndroid(t *testing.T) {
|
||||||
device, err := NewAndroidDevice()
|
device, err := NewAndroidDevice()
|
||||||
checkErr(t, err)
|
checkErr(t, err)
|
||||||
driverExt, err = device.NewDriver(nil)
|
driverExt, err = device.NewDriver()
|
||||||
checkErr(t, err)
|
checkErr(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -331,7 +331,7 @@ func TestDeviceList(t *testing.T) {
|
|||||||
|
|
||||||
func TestDriver_AppLaunch(t *testing.T) {
|
func TestDriver_AppLaunch(t *testing.T) {
|
||||||
device, _ := NewAndroidDevice()
|
device, _ := NewAndroidDevice()
|
||||||
driver, err := device.NewDriver(nil)
|
driver, err := device.NewDriver()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -383,7 +383,7 @@ func TestDriver_IsAppInForeground(t *testing.T) {
|
|||||||
|
|
||||||
func TestDriver_KeepAlive(t *testing.T) {
|
func TestDriver_KeepAlive(t *testing.T) {
|
||||||
device, _ := NewAndroidDevice()
|
device, _ := NewAndroidDevice()
|
||||||
driver, err := device.NewDriver(nil)
|
driver, err := device.NewDriver()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -408,7 +408,7 @@ func TestDriver_KeepAlive(t *testing.T) {
|
|||||||
|
|
||||||
func TestDriver_AppTerminate(t *testing.T) {
|
func TestDriver_AppTerminate(t *testing.T) {
|
||||||
device, _ := NewAndroidDevice()
|
device, _ := NewAndroidDevice()
|
||||||
driver, err := device.NewDriver(nil)
|
driver, err := device.NewDriver()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ func TestIOSDemo(t *testing.T) {
|
|||||||
|
|
||||||
capabilities := uixt.NewCapabilities()
|
capabilities := uixt.NewCapabilities()
|
||||||
capabilities.WithDefaultAlertAction(uixt.AlertActionAccept) // or uixt.AlertActionDismiss
|
capabilities.WithDefaultAlertAction(uixt.AlertActionAccept) // or uixt.AlertActionDismiss
|
||||||
driverExt, err := device.NewDriver(capabilities)
|
driverExt, err := device.NewDriver(uixt.WithDriverCapabilities(capabilities))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestDriverExt_Drag(t *testing.T) {
|
func TestDriverExt_Drag(t *testing.T) {
|
||||||
driverExt, err := iosDevice.NewDriver(nil)
|
driverExt, err := iosDevice.NewDriver()
|
||||||
checkErr(t, err)
|
checkErr(t, err)
|
||||||
|
|
||||||
pathSearch := "/Users/hero/Documents/temp/2020-05/opencv/IMG_map.png"
|
pathSearch := "/Users/hero/Documents/temp/2020-05/opencv/IMG_map.png"
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/httprunner/funplugin"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
@@ -91,9 +92,12 @@ type DriverExt struct {
|
|||||||
|
|
||||||
// cache step data
|
// cache step data
|
||||||
cacheStepData cacheStepData
|
cacheStepData cacheStepData
|
||||||
|
|
||||||
|
// funplugin
|
||||||
|
plugin funplugin.IPlugin
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDriverExt(device Device, driver WebDriver) (dExt *DriverExt, err error) {
|
func newDriverExt(device Device, driver WebDriver) (dExt *DriverExt, err error) {
|
||||||
dExt = &DriverExt{
|
dExt = &DriverExt{
|
||||||
Device: device,
|
Device: device,
|
||||||
Driver: driver,
|
Driver: driver,
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/httprunner/funplugin"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -437,11 +439,30 @@ type Rect struct {
|
|||||||
Size
|
Size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DriverOptions struct {
|
||||||
|
capabilities Capabilities
|
||||||
|
plugin funplugin.IPlugin
|
||||||
|
}
|
||||||
|
|
||||||
|
type DriverOption func(*DriverOptions)
|
||||||
|
|
||||||
|
func WithDriverCapabilities(capabilities Capabilities) DriverOption {
|
||||||
|
return func(options *DriverOptions) {
|
||||||
|
options.capabilities = capabilities
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithDriverPlugin(plugin funplugin.IPlugin) DriverOption {
|
||||||
|
return func(options *DriverOptions) {
|
||||||
|
options.plugin = plugin
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// current implemeted device: IOSDevice, AndroidDevice
|
// current implemeted device: IOSDevice, AndroidDevice
|
||||||
type Device interface {
|
type Device interface {
|
||||||
UUID() string // ios udid or android serial
|
UUID() string // ios udid or android serial
|
||||||
LogEnabled() bool
|
LogEnabled() bool
|
||||||
NewDriver(capabilities Capabilities) (driverExt *DriverExt, err error)
|
NewDriver(...DriverOption) (driverExt *DriverExt, err error)
|
||||||
|
|
||||||
StartPerf() error
|
StartPerf() error
|
||||||
StopPerf() string
|
StopPerf() string
|
||||||
|
|||||||
@@ -290,8 +290,14 @@ func (dev *IOSDevice) LogEnabled() bool {
|
|||||||
return dev.LogOn
|
return dev.LogOn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dev *IOSDevice) NewDriver(capabilities Capabilities) (driverExt *DriverExt, err error) {
|
func (dev *IOSDevice) NewDriver(options ...DriverOption) (driverExt *DriverExt, err error) {
|
||||||
|
driverOptions := &DriverOptions{}
|
||||||
|
for _, option := range options {
|
||||||
|
option(driverOptions)
|
||||||
|
}
|
||||||
|
|
||||||
// init WDA driver
|
// init WDA driver
|
||||||
|
capabilities := driverOptions.capabilities
|
||||||
if capabilities == nil {
|
if capabilities == nil {
|
||||||
capabilities = NewCapabilities()
|
capabilities = NewCapabilities()
|
||||||
capabilities.WithDefaultAlertAction(AlertActionAccept)
|
capabilities.WithDefaultAlertAction(AlertActionAccept)
|
||||||
@@ -316,7 +322,7 @@ func (dev *IOSDevice) NewDriver(capabilities Capabilities) (driverExt *DriverExt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
driverExt, err = NewDriverExt(dev, driver)
|
driverExt, err = newDriverExt(dev, driver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ func TestMatchRegex(t *testing.T) {
|
|||||||
func TestTapUIWithScreenshot(t *testing.T) {
|
func TestTapUIWithScreenshot(t *testing.T) {
|
||||||
serialNumber := os.Getenv("SERIAL_NUMBER")
|
serialNumber := os.Getenv("SERIAL_NUMBER")
|
||||||
device, _ := NewAndroidDevice(WithSerialNumber(serialNumber))
|
device, _ := NewAndroidDevice(WithSerialNumber(serialNumber))
|
||||||
driver, err := device.NewDriver(nil)
|
driver, err := device.NewDriver()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -88,7 +88,7 @@ func TestTapUIWithScreenshot(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDriverExtOCR(t *testing.T) {
|
func TestDriverExtOCR(t *testing.T) {
|
||||||
driverExt, err := iosDevice.NewDriver(nil)
|
driverExt, err := iosDevice.NewDriver()
|
||||||
checkErr(t, err)
|
checkErr(t, err)
|
||||||
|
|
||||||
point, err := driverExt.FindScreenText("抖音")
|
point, err := driverExt.FindScreenText("抖音")
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDriverExt_TapXY(t *testing.T) {
|
func TestDriverExt_TapXY(t *testing.T) {
|
||||||
driverExt, err := iosDevice.NewDriver(nil)
|
driverExt, err := iosDevice.NewDriver()
|
||||||
checkErr(t, err)
|
checkErr(t, err)
|
||||||
|
|
||||||
err = driverExt.TapXY(0.4, 0.5)
|
err = driverExt.TapXY(0.4, 0.5)
|
||||||
@@ -21,7 +21,7 @@ func TestDriverExt_TapXY(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDriverExt_TapAbsXY(t *testing.T) {
|
func TestDriverExt_TapAbsXY(t *testing.T) {
|
||||||
driverExt, err := iosDevice.NewDriver(nil)
|
driverExt, err := iosDevice.NewDriver()
|
||||||
checkErr(t, err)
|
checkErr(t, err)
|
||||||
|
|
||||||
err = driverExt.TapAbsXY(100, 300)
|
err = driverExt.TapAbsXY(100, 300)
|
||||||
@@ -29,7 +29,7 @@ func TestDriverExt_TapAbsXY(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDriverExt_TapWithOCR(t *testing.T) {
|
func TestDriverExt_TapWithOCR(t *testing.T) {
|
||||||
driverExt, err := iosDevice.NewDriver(nil)
|
driverExt, err := iosDevice.NewDriver()
|
||||||
checkErr(t, err)
|
checkErr(t, err)
|
||||||
|
|
||||||
// 需要点击文字上方的图标
|
// 需要点击文字上方的图标
|
||||||
|
|||||||
@@ -434,7 +434,7 @@ func (r *CaseRunner) parseConfig() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "init iOS device failed")
|
return errors.Wrap(err, "init iOS device failed")
|
||||||
}
|
}
|
||||||
client, err := device.NewDriver(nil)
|
client, err := device.NewDriver()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "init iOS WDA client failed")
|
return errors.Wrap(err, "init iOS WDA client failed")
|
||||||
}
|
}
|
||||||
@@ -453,7 +453,7 @@ func (r *CaseRunner) parseConfig() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "init Android device failed")
|
return errors.Wrap(err, "init Android device failed")
|
||||||
}
|
}
|
||||||
client, err := device.NewDriver(nil)
|
client, err := device.NewDriver()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "init Android client failed")
|
return errors.Wrap(err, "init Android client failed")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -538,7 +538,7 @@ func (r *HRPRunner) initUIClient(uuid string, osType string) (client *uixt.Drive
|
|||||||
return nil, errors.Wrapf(err, "init %s device failed", osType)
|
return nil, errors.Wrapf(err, "init %s device failed", osType)
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err = device.NewDriver(nil)
|
client, err = device.NewDriver()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user