mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-30 12:59:39 +08:00
refactor: move driver/capatibilities options to pkg/uixt/options
This commit is contained in:
@@ -174,17 +174,14 @@ func (dev *AndroidDevice) LogEnabled() bool {
|
||||
return dev.LogOn
|
||||
}
|
||||
|
||||
func (dev *AndroidDevice) NewDriver(options ...DriverOption) (driverExt *DriverExt, err error) {
|
||||
driverOptions := NewDriverOptions()
|
||||
for _, option := range options {
|
||||
option(driverOptions)
|
||||
}
|
||||
func (dev *AndroidDevice) NewDriver(opts ...options.DriverOption) (driverExt *DriverExt, err error) {
|
||||
driverOptions := options.NewDriverOptions(opts...)
|
||||
|
||||
var driver IWebDriver
|
||||
if dev.UIA2 || dev.LogOn {
|
||||
driver, err = dev.NewUSBDriver(driverOptions.capabilities)
|
||||
driver, err = dev.NewUSBDriver(driverOptions.Capabilities)
|
||||
} else if dev.STUB {
|
||||
driver, err = dev.NewStubDriver(driverOptions.capabilities)
|
||||
driver, err = dev.NewStubDriver(driverOptions.Capabilities)
|
||||
} else {
|
||||
driver, err = dev.NewAdbDriver()
|
||||
}
|
||||
@@ -192,7 +189,7 @@ func (dev *AndroidDevice) NewDriver(options ...DriverOption) (driverExt *DriverE
|
||||
return nil, errors.Wrap(err, "failed to init UIA driver")
|
||||
}
|
||||
|
||||
driverExt, err = newDriverExt(dev, driver, options...)
|
||||
driverExt, err = newDriverExt(dev, driver, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -208,7 +205,7 @@ func (dev *AndroidDevice) NewDriver(options ...DriverOption) (driverExt *DriverE
|
||||
}
|
||||
|
||||
// NewUSBDriver creates new client via USB connected device, this will also start a new session.
|
||||
func (dev *AndroidDevice) NewUSBDriver(capabilities Capabilities) (driver IWebDriver, err error) {
|
||||
func (dev *AndroidDevice) NewUSBDriver(capabilities options.Capabilities) (driver IWebDriver, err error) {
|
||||
localPort, err := dev.d.Forward(dev.UIA2Port)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(code.DeviceConnectionError,
|
||||
@@ -229,7 +226,7 @@ func (dev *AndroidDevice) NewUSBDriver(capabilities Capabilities) (driver IWebDr
|
||||
return uiaDriver, nil
|
||||
}
|
||||
|
||||
func (dev *AndroidDevice) NewStubDriver(capabilities Capabilities) (driver *stubAndroidDriver, err error) {
|
||||
func (dev *AndroidDevice) NewStubDriver(capabilities options.Capabilities) (driver *stubAndroidDriver, err error) {
|
||||
socketLocalPort, err := dev.d.Forward(StubSocketName)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(code.DeviceConnectionError,
|
||||
@@ -260,7 +257,7 @@ func (dev *AndroidDevice) NewStubDriver(capabilities Capabilities) (driver *stub
|
||||
}
|
||||
|
||||
// NewHTTPDriver creates new remote HTTP client, this will also start a new session.
|
||||
func (dev *AndroidDevice) NewHTTPDriver(capabilities Capabilities) (driver IWebDriver, err error) {
|
||||
func (dev *AndroidDevice) NewHTTPDriver(capabilities options.Capabilities) (driver IWebDriver, err error) {
|
||||
rawURL := fmt.Sprintf("http://%s:%d/wd/hub", dev.UIA2IP, dev.UIA2Port)
|
||||
uiaDriver, err := NewUIADriver(capabilities, rawURL)
|
||||
if err != nil {
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/httprunner/httprunner/v5/internal/config"
|
||||
"github.com/httprunner/httprunner/v5/internal/utf7"
|
||||
"github.com/httprunner/httprunner/v5/pkg/gadb"
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -78,7 +79,7 @@ func (ad *adbDriver) runShellCommand(cmd string, args ...string) (output string,
|
||||
return output, err
|
||||
}
|
||||
|
||||
func (ad *adbDriver) NewSession(capabilities Capabilities) (sessionInfo SessionInfo, err error) {
|
||||
func (ad *adbDriver) NewSession(capabilities options.Capabilities) (sessionInfo SessionInfo, err error) {
|
||||
ad.Driver.session.Reset()
|
||||
err = errDriverNotImplemented
|
||||
return
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/internal/json"
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
|
||||
)
|
||||
|
||||
type stubAndroidDriver struct {
|
||||
@@ -100,7 +101,7 @@ func (sad *stubAndroidDriver) httpPOST(data interface{}, pathElem ...string) (ra
|
||||
return sad.Request(http.MethodPost, sad.concatURL(nil, pathElem...), bsJSON)
|
||||
}
|
||||
|
||||
func (sad *stubAndroidDriver) NewSession(capabilities Capabilities) (SessionInfo, error) {
|
||||
func (sad *stubAndroidDriver) NewSession(capabilities options.Capabilities) (SessionInfo, error) {
|
||||
sad.Driver.session.Reset()
|
||||
return SessionInfo{}, errDriverNotImplemented
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
|
||||
)
|
||||
|
||||
var androidStubDriver *stubAndroidDriver
|
||||
@@ -12,7 +14,7 @@ func setupStubDriver(t *testing.T) {
|
||||
device, err := NewAndroidDevice()
|
||||
checkErr(t, err)
|
||||
device.STUB = true
|
||||
androidStubDriver, err = device.NewStubDriver(Capabilities{})
|
||||
androidStubDriver, err = device.NewStubDriver(options.Capabilities{})
|
||||
checkErr(t, err)
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/internal/utf7"
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
|
||||
)
|
||||
|
||||
var errDriverNotImplemented = errors.New("driver method not implemented")
|
||||
@@ -25,10 +26,10 @@ type uiaDriver struct {
|
||||
adbDriver
|
||||
}
|
||||
|
||||
func NewUIADriver(capabilities Capabilities, urlPrefix string) (driver *uiaDriver, err error) {
|
||||
func NewUIADriver(capabilities options.Capabilities, urlPrefix string) (driver *uiaDriver, err error) {
|
||||
log.Info().Msg("init uiautomator2 driver")
|
||||
if capabilities == nil {
|
||||
capabilities = NewCapabilities()
|
||||
capabilities = options.NewCapabilities()
|
||||
capabilities.WithWaitForIdleTimeout(0)
|
||||
}
|
||||
driver = new(uiaDriver)
|
||||
@@ -85,7 +86,7 @@ func (bs BatteryStatus) String() string {
|
||||
}
|
||||
|
||||
func (ud *uiaDriver) resetDriver() error {
|
||||
newUIADriver, err := NewUIADriver(NewCapabilities(), ud.urlPrefix.String())
|
||||
newUIADriver, err := NewUIADriver(options.NewCapabilities(), ud.urlPrefix.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -133,7 +134,7 @@ func (ud *uiaDriver) httpDELETE(pathElem ...string) (rawResp rawResponse, err er
|
||||
return ud.httpRequest(http.MethodDelete, ud.concatURL(nil, pathElem...), nil)
|
||||
}
|
||||
|
||||
func (ud *uiaDriver) NewSession(capabilities Capabilities) (sessionInfo SessionInfo, err error) {
|
||||
func (ud *uiaDriver) NewSession(capabilities options.Capabilities) (sessionInfo SessionInfo, err error) {
|
||||
// register(postHandler, new NewSession("/wd/hub/session"))
|
||||
var rawResp rawResponse
|
||||
data := make(map[string]interface{})
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/internal/builtin"
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -59,7 +60,7 @@ func TestDriver_NewSession(t *testing.T) {
|
||||
firstMatchEntry := make(map[string]interface{})
|
||||
firstMatchEntry["package"] = "com.android.settings"
|
||||
firstMatchEntry["activity"] = "com.android.settings/.Settings"
|
||||
caps := Capabilities{
|
||||
caps := options.Capabilities{
|
||||
"firstMatch": []interface{}{firstMatchEntry},
|
||||
"alwaysMatch": struct{}{},
|
||||
}
|
||||
|
||||
@@ -22,9 +22,9 @@ func TestIOSDemo(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
capabilities := uixt.NewCapabilities()
|
||||
capabilities.WithDefaultAlertAction(uixt.AlertActionAccept) // or uixt.AlertActionDismiss
|
||||
driverExt, err := device.NewDriver(uixt.WithDriverCapabilities(capabilities))
|
||||
capabilities := options.NewCapabilities()
|
||||
capabilities.WithDefaultAlertAction(options.AlertActionAccept) // or uixt.AlertActionDismiss
|
||||
driverExt, err := device.NewDriver(options.WithDriverCapabilities(capabilities))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/httprunner/httprunner/v5/internal/builtin"
|
||||
"github.com/httprunner/httprunner/v5/internal/config"
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
|
||||
)
|
||||
|
||||
type DriverExt struct {
|
||||
@@ -24,24 +25,21 @@ type DriverExt struct {
|
||||
plugin funplugin.IPlugin
|
||||
}
|
||||
|
||||
func newDriverExt(device IDevice, driver IWebDriver, options ...DriverOption) (dExt *DriverExt, err error) {
|
||||
driverOptions := NewDriverOptions()
|
||||
for _, option := range options {
|
||||
option(driverOptions)
|
||||
}
|
||||
func newDriverExt(device IDevice, driver IWebDriver, opts ...options.DriverOption) (dExt *DriverExt, err error) {
|
||||
driverOptions := options.NewDriverOptions(opts...)
|
||||
|
||||
dExt = &DriverExt{
|
||||
Device: device,
|
||||
Driver: driver,
|
||||
plugin: driverOptions.plugin,
|
||||
plugin: driverOptions.Plugin,
|
||||
}
|
||||
|
||||
if driverOptions.withImageService {
|
||||
if driverOptions.WithImageService {
|
||||
if dExt.ImageService, err = newVEDEMImageService(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if driverOptions.withResultFolder {
|
||||
if driverOptions.WithResultFolder {
|
||||
// create results directory
|
||||
if err = builtin.EnsureFolderExists(config.ResultsPath); err != nil {
|
||||
return nil, errors.Wrap(err, "create results directory failed")
|
||||
|
||||
@@ -95,14 +95,14 @@ func (dev *HarmonyDevice) LogEnabled() bool {
|
||||
return dev.LogOn
|
||||
}
|
||||
|
||||
func (dev *HarmonyDevice) NewDriver(options ...DriverOption) (driverExt *DriverExt, err error) {
|
||||
func (dev *HarmonyDevice) NewDriver(opts ...options.DriverOption) (driverExt *DriverExt, err error) {
|
||||
driver, err := newHarmonyDriver(dev.d)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to new harmony driver")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
driverExt, err = newDriverExt(dev, driver, options...)
|
||||
driverExt, err = newDriverExt(dev, driver, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -110,7 +110,7 @@ func (dev *HarmonyDevice) NewDriver(options ...DriverOption) (driverExt *DriverE
|
||||
return driverExt, nil
|
||||
}
|
||||
|
||||
func (dev *HarmonyDevice) NewUSBDriver(options ...DriverOption) (driver IWebDriver, err error) {
|
||||
func (dev *HarmonyDevice) NewUSBDriver(opts ...options.DriverOption) (driver IWebDriver, err error) {
|
||||
harmonyDriver, err := newHarmonyDriver(dev.d)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to new harmony driver")
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"code.byted.org/iesqa/ghdc"
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
@@ -39,7 +40,7 @@ func newHarmonyDriver(device *ghdc.Device) (driver *hdcDriver, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (hd *hdcDriver) NewSession(capabilities Capabilities) (SessionInfo, error) {
|
||||
func (hd *hdcDriver) NewSession(capabilities options.Capabilities) (SessionInfo, error) {
|
||||
hd.Driver.session.Reset()
|
||||
hd.Unlock()
|
||||
return SessionInfo{}, errDriverNotImplemented
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/httprunner/funplugin"
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -14,101 +14,6 @@ var (
|
||||
DefaultWaitInterval = 400 * time.Millisecond
|
||||
)
|
||||
|
||||
type AlertAction string
|
||||
|
||||
const (
|
||||
AlertActionAccept AlertAction = "accept"
|
||||
AlertActionDismiss AlertAction = "dismiss"
|
||||
)
|
||||
|
||||
type Capabilities map[string]interface{}
|
||||
|
||||
func NewCapabilities() Capabilities {
|
||||
return make(Capabilities)
|
||||
}
|
||||
|
||||
// WithDefaultAlertAction
|
||||
func (caps Capabilities) WithDefaultAlertAction(alertAction AlertAction) Capabilities {
|
||||
caps["defaultAlertAction"] = alertAction
|
||||
return caps
|
||||
}
|
||||
|
||||
// WithMaxTypingFrequency
|
||||
//
|
||||
// Defaults to `60`.
|
||||
func (caps Capabilities) WithMaxTypingFrequency(n int) Capabilities {
|
||||
if n <= 0 {
|
||||
n = 60
|
||||
}
|
||||
caps["maxTypingFrequency"] = n
|
||||
return caps
|
||||
}
|
||||
|
||||
// WithWaitForIdleTimeout
|
||||
//
|
||||
// Defaults to `10`
|
||||
func (caps Capabilities) WithWaitForIdleTimeout(second float64) Capabilities {
|
||||
caps["waitForIdleTimeout"] = second
|
||||
return caps
|
||||
}
|
||||
|
||||
// WithShouldUseTestManagerForVisibilityDetection If set to YES will ask TestManagerDaemon for element visibility
|
||||
//
|
||||
// Defaults to `false`
|
||||
func (caps Capabilities) WithShouldUseTestManagerForVisibilityDetection(b bool) Capabilities {
|
||||
caps["shouldUseTestManagerForVisibilityDetection"] = b
|
||||
return caps
|
||||
}
|
||||
|
||||
// WithShouldUseCompactResponses If set to YES will use compact (standards-compliant) & faster responses
|
||||
//
|
||||
// Defaults to `true`
|
||||
func (caps Capabilities) WithShouldUseCompactResponses(b bool) Capabilities {
|
||||
caps["shouldUseCompactResponses"] = b
|
||||
return caps
|
||||
}
|
||||
|
||||
// WithElementResponseAttributes If shouldUseCompactResponses == NO,
|
||||
// is the comma-separated list of fields to return with each element.
|
||||
//
|
||||
// Defaults to `type,label`.
|
||||
func (caps Capabilities) WithElementResponseAttributes(s string) Capabilities {
|
||||
caps["elementResponseAttributes"] = s
|
||||
return caps
|
||||
}
|
||||
|
||||
// WithShouldUseSingletonTestManager
|
||||
//
|
||||
// Defaults to `true`
|
||||
func (caps Capabilities) WithShouldUseSingletonTestManager(b bool) Capabilities {
|
||||
caps["shouldUseSingletonTestManager"] = b
|
||||
return caps
|
||||
}
|
||||
|
||||
// WithDisableAutomaticScreenshots
|
||||
//
|
||||
// Defaults to `true`
|
||||
func (caps Capabilities) WithDisableAutomaticScreenshots(b bool) Capabilities {
|
||||
caps["disableAutomaticScreenshots"] = b
|
||||
return caps
|
||||
}
|
||||
|
||||
// WithShouldTerminateApp
|
||||
//
|
||||
// Defaults to `true`
|
||||
func (caps Capabilities) WithShouldTerminateApp(b bool) Capabilities {
|
||||
caps["shouldTerminateApp"] = b
|
||||
return caps
|
||||
}
|
||||
|
||||
// WithEventloopIdleDelaySec
|
||||
// Delays the invocation of '-[XCUIApplicationProcess setEventLoopHasIdled:]' by the timer interval passed.
|
||||
// which is skipped on setting it to zero.
|
||||
func (caps Capabilities) WithEventloopIdleDelaySec(second float64) Capabilities {
|
||||
caps["eventloopIdleDelaySec"] = second
|
||||
return caps
|
||||
}
|
||||
|
||||
type SessionInfo struct {
|
||||
SessionId string `json:"sessionId"`
|
||||
Capabilities struct {
|
||||
@@ -445,54 +350,6 @@ type Rect struct {
|
||||
Size
|
||||
}
|
||||
|
||||
type DriverOptions struct {
|
||||
capabilities Capabilities
|
||||
plugin funplugin.IPlugin
|
||||
withImageService bool
|
||||
withResultFolder bool
|
||||
withUIAction bool
|
||||
}
|
||||
|
||||
func NewDriverOptions() *DriverOptions {
|
||||
return &DriverOptions{
|
||||
withImageService: true,
|
||||
withResultFolder: true,
|
||||
withUIAction: true,
|
||||
}
|
||||
}
|
||||
|
||||
type DriverOption func(*DriverOptions)
|
||||
|
||||
func WithDriverCapabilities(capabilities Capabilities) DriverOption {
|
||||
return func(options *DriverOptions) {
|
||||
options.capabilities = capabilities
|
||||
}
|
||||
}
|
||||
|
||||
func WithDriverImageService(withImageService bool) DriverOption {
|
||||
return func(options *DriverOptions) {
|
||||
options.withImageService = withImageService
|
||||
}
|
||||
}
|
||||
|
||||
func WithDriverResultFolder(withResultFolder bool) DriverOption {
|
||||
return func(options *DriverOptions) {
|
||||
options.withResultFolder = withResultFolder
|
||||
}
|
||||
}
|
||||
|
||||
func WithUIAction(withUIAction bool) DriverOption {
|
||||
return func(options *DriverOptions) {
|
||||
options.withUIAction = withUIAction
|
||||
}
|
||||
}
|
||||
|
||||
func WithDriverPlugin(plugin funplugin.IPlugin) DriverOption {
|
||||
return func(options *DriverOptions) {
|
||||
options.plugin = plugin
|
||||
}
|
||||
}
|
||||
|
||||
// current implemeted device: IOSDevice, AndroidDevice, HarmonyDevice
|
||||
type IDevice interface {
|
||||
Init() error // init android device
|
||||
@@ -500,7 +357,7 @@ type IDevice interface {
|
||||
LogEnabled() bool
|
||||
|
||||
// TODO: add ctx to NewDriver
|
||||
NewDriver(...DriverOption) (driverExt *DriverExt, err error)
|
||||
NewDriver(...options.DriverOption) (driverExt *DriverExt, err error)
|
||||
|
||||
Install(appPath string, options ...InstallOption) error
|
||||
Uninstall(packageName string) error
|
||||
@@ -519,7 +376,7 @@ type ForegroundApp struct {
|
||||
// IWebDriver defines methods supported by IWebDriver drivers.
|
||||
type IWebDriver interface {
|
||||
// NewSession starts a new session and returns the SessionInfo.
|
||||
NewSession(capabilities Capabilities) (SessionInfo, error)
|
||||
NewSession(capabilities options.Capabilities) (SessionInfo, error)
|
||||
|
||||
// DeleteSession Kills application associated with that session and removes session
|
||||
// 1) alertsMonitor disable
|
||||
|
||||
@@ -260,17 +260,14 @@ func (dev *IOSDevice) getAppInfo(packageName string) (appInfo AppInfo, err error
|
||||
return AppInfo{}, fmt.Errorf("not found App by bundle id: %s", packageName)
|
||||
}
|
||||
|
||||
func (dev *IOSDevice) NewDriver(options ...DriverOption) (driverExt *DriverExt, err error) {
|
||||
driverOptions := NewDriverOptions()
|
||||
for _, option := range options {
|
||||
option(driverOptions)
|
||||
}
|
||||
func (dev *IOSDevice) NewDriver(opts ...options.DriverOption) (driverExt *DriverExt, err error) {
|
||||
driverOptions := options.NewDriverOptions()
|
||||
|
||||
// init WDA driver
|
||||
capabilities := driverOptions.capabilities
|
||||
capabilities := driverOptions.Capabilities
|
||||
if capabilities == nil {
|
||||
capabilities = NewCapabilities()
|
||||
capabilities.WithDefaultAlertAction(AlertActionAccept)
|
||||
capabilities = options.NewCapabilities()
|
||||
capabilities.WithDefaultAlertAction(options.AlertActionAccept)
|
||||
}
|
||||
|
||||
var driver IWebDriver
|
||||
@@ -302,7 +299,7 @@ func (dev *IOSDevice) NewDriver(options ...DriverOption) (driverExt *DriverExt,
|
||||
}
|
||||
}
|
||||
|
||||
driverExt, err = newDriverExt(dev, driver, options...)
|
||||
driverExt, err = newDriverExt(dev, driver, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -564,7 +561,7 @@ func (dev *IOSDevice) Reboot() error {
|
||||
}
|
||||
|
||||
// NewHTTPDriver creates new remote HTTP client, this will also start a new session.
|
||||
func (dev *IOSDevice) NewHTTPDriver(capabilities Capabilities) (driver IWebDriver, err error) {
|
||||
func (dev *IOSDevice) NewHTTPDriver(capabilities options.Capabilities) (driver IWebDriver, err error) {
|
||||
var localPort int
|
||||
localPort, err = strconv.Atoi(os.Getenv("WDA_LOCAL_PORT"))
|
||||
if err != nil {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
@@ -37,8 +38,8 @@ func newStubIOSDriver(bightInsightAddr, serverAddr string, dev *IOSDevice, readT
|
||||
|
||||
func (s *stubIOSDriver) setUpWda() (err error) {
|
||||
if s.wdaDriver == nil {
|
||||
capabilities := NewCapabilities()
|
||||
capabilities.WithDefaultAlertAction(AlertActionAccept)
|
||||
capabilities := options.NewCapabilities()
|
||||
capabilities.WithDefaultAlertAction(options.AlertActionAccept)
|
||||
driver, err := s.device.NewHTTPDriver(capabilities)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("stub driver failed to init wda driver")
|
||||
@@ -50,7 +51,7 @@ func (s *stubIOSDriver) setUpWda() (err error) {
|
||||
}
|
||||
|
||||
// NewSession starts a new session and returns the SessionInfo.
|
||||
func (s *stubIOSDriver) NewSession(capabilities Capabilities) (SessionInfo, error) {
|
||||
func (s *stubIOSDriver) NewSession(capabilities options.Capabilities) (SessionInfo, error) {
|
||||
err := s.setUpWda()
|
||||
if err != nil {
|
||||
return SessionInfo{}, err
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/httprunner/httprunner/v5/code"
|
||||
"github.com/httprunner/httprunner/v5/internal/builtin"
|
||||
"github.com/httprunner/httprunner/v5/internal/json"
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
|
||||
)
|
||||
|
||||
type wdaDriver struct {
|
||||
@@ -36,8 +37,8 @@ type wdaDriver struct {
|
||||
}
|
||||
|
||||
func (wd *wdaDriver) resetSession() error {
|
||||
capabilities := NewCapabilities()
|
||||
capabilities.WithDefaultAlertAction(AlertActionAccept)
|
||||
capabilities := options.NewCapabilities()
|
||||
capabilities.WithDefaultAlertAction(options.AlertActionAccept)
|
||||
|
||||
data := map[string]interface{}{
|
||||
"capabilities": map[string]interface{}{
|
||||
@@ -115,7 +116,7 @@ func (wd *wdaDriver) GetMjpegClient() *http.Client {
|
||||
return wd.mjpegClient
|
||||
}
|
||||
|
||||
func (wd *wdaDriver) NewSession(capabilities Capabilities) (sessionInfo SessionInfo, err error) {
|
||||
func (wd *wdaDriver) NewSession(capabilities options.Capabilities) (sessionInfo SessionInfo, err error) {
|
||||
// [[FBRoute POST:@"/session"].withoutSession respondWithTarget:self action:@selector(handleCreateSession:)]
|
||||
data := make(map[string]interface{})
|
||||
if len(capabilities) == 0 {
|
||||
|
||||
@@ -26,8 +26,8 @@ func setup(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
capabilities := NewCapabilities()
|
||||
capabilities.WithDefaultAlertAction(AlertActionAccept)
|
||||
capabilities := options.NewCapabilities()
|
||||
capabilities.WithDefaultAlertAction(options.AlertActionAccept)
|
||||
driver, err = device.NewHTTPDriver(capabilities)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
96
pkg/uixt/options/capabilities.go
Normal file
96
pkg/uixt/options/capabilities.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package options
|
||||
|
||||
type AlertAction string
|
||||
|
||||
const (
|
||||
AlertActionAccept AlertAction = "accept"
|
||||
AlertActionDismiss AlertAction = "dismiss"
|
||||
)
|
||||
|
||||
type Capabilities map[string]interface{}
|
||||
|
||||
func NewCapabilities() Capabilities {
|
||||
return make(Capabilities)
|
||||
}
|
||||
|
||||
// WithDefaultAlertAction
|
||||
func (caps Capabilities) WithDefaultAlertAction(alertAction AlertAction) Capabilities {
|
||||
caps["defaultAlertAction"] = alertAction
|
||||
return caps
|
||||
}
|
||||
|
||||
// WithMaxTypingFrequency
|
||||
//
|
||||
// Defaults to `60`.
|
||||
func (caps Capabilities) WithMaxTypingFrequency(n int) Capabilities {
|
||||
if n <= 0 {
|
||||
n = 60
|
||||
}
|
||||
caps["maxTypingFrequency"] = n
|
||||
return caps
|
||||
}
|
||||
|
||||
// WithWaitForIdleTimeout
|
||||
//
|
||||
// Defaults to `10`
|
||||
func (caps Capabilities) WithWaitForIdleTimeout(second float64) Capabilities {
|
||||
caps["waitForIdleTimeout"] = second
|
||||
return caps
|
||||
}
|
||||
|
||||
// WithShouldUseTestManagerForVisibilityDetection If set to YES will ask TestManagerDaemon for element visibility
|
||||
//
|
||||
// Defaults to `false`
|
||||
func (caps Capabilities) WithShouldUseTestManagerForVisibilityDetection(b bool) Capabilities {
|
||||
caps["shouldUseTestManagerForVisibilityDetection"] = b
|
||||
return caps
|
||||
}
|
||||
|
||||
// WithShouldUseCompactResponses If set to YES will use compact (standards-compliant) & faster responses
|
||||
//
|
||||
// Defaults to `true`
|
||||
func (caps Capabilities) WithShouldUseCompactResponses(b bool) Capabilities {
|
||||
caps["shouldUseCompactResponses"] = b
|
||||
return caps
|
||||
}
|
||||
|
||||
// WithElementResponseAttributes If shouldUseCompactResponses == NO,
|
||||
// is the comma-separated list of fields to return with each element.
|
||||
//
|
||||
// Defaults to `type,label`.
|
||||
func (caps Capabilities) WithElementResponseAttributes(s string) Capabilities {
|
||||
caps["elementResponseAttributes"] = s
|
||||
return caps
|
||||
}
|
||||
|
||||
// WithShouldUseSingletonTestManager
|
||||
//
|
||||
// Defaults to `true`
|
||||
func (caps Capabilities) WithShouldUseSingletonTestManager(b bool) Capabilities {
|
||||
caps["shouldUseSingletonTestManager"] = b
|
||||
return caps
|
||||
}
|
||||
|
||||
// WithDisableAutomaticScreenshots
|
||||
//
|
||||
// Defaults to `true`
|
||||
func (caps Capabilities) WithDisableAutomaticScreenshots(b bool) Capabilities {
|
||||
caps["disableAutomaticScreenshots"] = b
|
||||
return caps
|
||||
}
|
||||
|
||||
// WithShouldTerminateApp
|
||||
//
|
||||
// Defaults to `true`
|
||||
func (caps Capabilities) WithShouldTerminateApp(b bool) Capabilities {
|
||||
caps["shouldTerminateApp"] = b
|
||||
return caps
|
||||
}
|
||||
|
||||
// WithEventloopIdleDelaySec
|
||||
// Delays the invocation of '-[XCUIApplicationProcess setEventLoopHasIdled:]' by the timer interval passed.
|
||||
// which is skipped on setting it to zero.
|
||||
func (caps Capabilities) WithEventloopIdleDelaySec(second float64) Capabilities {
|
||||
caps["eventloopIdleDelaySec"] = second
|
||||
return caps
|
||||
}
|
||||
55
pkg/uixt/options/driver.go
Normal file
55
pkg/uixt/options/driver.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package options
|
||||
|
||||
import "github.com/httprunner/funplugin"
|
||||
|
||||
type DriverOptions struct {
|
||||
Capabilities Capabilities
|
||||
Plugin funplugin.IPlugin
|
||||
WithImageService bool
|
||||
WithResultFolder bool
|
||||
WithUIAction bool
|
||||
}
|
||||
|
||||
func NewDriverOptions(opts ...DriverOption) *DriverOptions {
|
||||
driverOptions := &DriverOptions{
|
||||
WithImageService: true,
|
||||
WithResultFolder: true,
|
||||
WithUIAction: true,
|
||||
}
|
||||
for _, option := range opts {
|
||||
option(driverOptions)
|
||||
}
|
||||
return driverOptions
|
||||
}
|
||||
|
||||
type DriverOption func(*DriverOptions)
|
||||
|
||||
func WithDriverCapabilities(capabilities Capabilities) DriverOption {
|
||||
return func(options *DriverOptions) {
|
||||
options.Capabilities = capabilities
|
||||
}
|
||||
}
|
||||
|
||||
func WithDriverImageService(withImageService bool) DriverOption {
|
||||
return func(options *DriverOptions) {
|
||||
options.WithImageService = withImageService
|
||||
}
|
||||
}
|
||||
|
||||
func WithDriverResultFolder(withResultFolder bool) DriverOption {
|
||||
return func(options *DriverOptions) {
|
||||
options.WithResultFolder = withResultFolder
|
||||
}
|
||||
}
|
||||
|
||||
func WithUIAction(withUIAction bool) DriverOption {
|
||||
return func(options *DriverOptions) {
|
||||
options.WithUIAction = withUIAction
|
||||
}
|
||||
}
|
||||
|
||||
func WithDriverPlugin(plugin funplugin.IPlugin) DriverOption {
|
||||
return func(options *DriverOptions) {
|
||||
options.Plugin = plugin
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user