mirror of
https://github.com/httprunner/httprunner.git
synced 2026-08-28 03:30:01 +08:00
refactor: restructure
This commit is contained in:
+101
-361
@@ -1,22 +1,14 @@
|
||||
package hrp
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/uixt"
|
||||
)
|
||||
|
||||
type (
|
||||
WDAOptions = uixt.WDAOptions
|
||||
WDAOption = uixt.WDAOption
|
||||
)
|
||||
|
||||
var (
|
||||
WithUDID = uixt.WithUDID
|
||||
WithPort = uixt.WithPort
|
||||
@@ -25,9 +17,9 @@ var (
|
||||
)
|
||||
|
||||
type IOSStep struct {
|
||||
WDAOptions `yaml:",inline"` // inline refers to https://pkg.go.dev/gopkg.in/yaml.v3#Marshal
|
||||
MobileAction `yaml:",inline"`
|
||||
Actions []MobileAction `json:"actions,omitempty" yaml:"actions,omitempty"`
|
||||
uixt.WDAOptions `yaml:",inline"` // inline refers to https://pkg.go.dev/gopkg.in/yaml.v3#Marshal
|
||||
uixt.MobileAction `yaml:",inline"`
|
||||
Actions []uixt.MobileAction `json:"actions,omitempty" yaml:"actions,omitempty"`
|
||||
}
|
||||
|
||||
// StepIOS implements IStep interface.
|
||||
@@ -41,49 +33,49 @@ func (s *StepIOS) UDID(udid string) *StepIOS {
|
||||
}
|
||||
|
||||
func (s *StepIOS) InstallApp(path string) *StepIOS {
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, MobileAction{
|
||||
Method: appInstall,
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, uixt.MobileAction{
|
||||
Method: uixt.AppInstall,
|
||||
Params: path,
|
||||
})
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *StepIOS) AppLaunch(bundleId string) *StepIOS {
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, MobileAction{
|
||||
Method: appLaunch,
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, uixt.MobileAction{
|
||||
Method: uixt.AppLaunch,
|
||||
Params: bundleId,
|
||||
})
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *StepIOS) AppLaunchUnattached(bundleId string) *StepIOS {
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, MobileAction{
|
||||
Method: appLaunchUnattached,
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, uixt.MobileAction{
|
||||
Method: uixt.AppLaunchUnattached,
|
||||
Params: bundleId,
|
||||
})
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *StepIOS) AppTerminate(bundleId string) *StepIOS {
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, MobileAction{
|
||||
Method: appTerminate,
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, uixt.MobileAction{
|
||||
Method: uixt.AppTerminate,
|
||||
Params: bundleId,
|
||||
})
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *StepIOS) Home() *StepIOS {
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, MobileAction{
|
||||
Method: uiHome,
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, uixt.MobileAction{
|
||||
Method: uixt.ACTION_Home,
|
||||
Params: nil,
|
||||
})
|
||||
return &StepIOS{step: s.step}
|
||||
}
|
||||
|
||||
// TapXY taps the point {X,Y}, X & Y is percentage of coordinates
|
||||
func (s *StepIOS) TapXY(x, y float64, options ...ActionOption) *StepIOS {
|
||||
action := MobileAction{
|
||||
Method: uiTapXY,
|
||||
func (s *StepIOS) TapXY(x, y float64, options ...uixt.ActionOption) *StepIOS {
|
||||
action := uixt.MobileAction{
|
||||
Method: uixt.ACTION_TapXY,
|
||||
Params: []float64{x, y},
|
||||
}
|
||||
for _, option := range options {
|
||||
@@ -94,9 +86,9 @@ func (s *StepIOS) TapXY(x, y float64, options ...ActionOption) *StepIOS {
|
||||
}
|
||||
|
||||
// Tap taps on the target element
|
||||
func (s *StepIOS) Tap(params string, options ...ActionOption) *StepIOS {
|
||||
action := MobileAction{
|
||||
Method: uiTap,
|
||||
func (s *StepIOS) Tap(params string, options ...uixt.ActionOption) *StepIOS {
|
||||
action := uixt.MobileAction{
|
||||
Method: uixt.ACTION_Tap,
|
||||
Params: params,
|
||||
}
|
||||
for _, option := range options {
|
||||
@@ -107,9 +99,9 @@ func (s *StepIOS) Tap(params string, options ...ActionOption) *StepIOS {
|
||||
}
|
||||
|
||||
// Tap taps on the target element by OCR recognition
|
||||
func (s *StepIOS) TapByOCR(ocrText string, options ...ActionOption) *StepIOS {
|
||||
action := MobileAction{
|
||||
Method: uiTapByOCR,
|
||||
func (s *StepIOS) TapByOCR(ocrText string, options ...uixt.ActionOption) *StepIOS {
|
||||
action := uixt.MobileAction{
|
||||
Method: uixt.ACTION_TapByOCR,
|
||||
Params: ocrText,
|
||||
}
|
||||
for _, option := range options {
|
||||
@@ -120,9 +112,9 @@ func (s *StepIOS) TapByOCR(ocrText string, options ...ActionOption) *StepIOS {
|
||||
}
|
||||
|
||||
// Tap taps on the target element by CV recognition
|
||||
func (s *StepIOS) TapByCV(imagePath string, options ...ActionOption) *StepIOS {
|
||||
action := MobileAction{
|
||||
Method: uiTapByCV,
|
||||
func (s *StepIOS) TapByCV(imagePath string, options ...uixt.ActionOption) *StepIOS {
|
||||
action := uixt.MobileAction{
|
||||
Method: uixt.ACTION_TapByCV,
|
||||
Params: imagePath,
|
||||
}
|
||||
for _, option := range options {
|
||||
@@ -134,16 +126,16 @@ func (s *StepIOS) TapByCV(imagePath string, options ...ActionOption) *StepIOS {
|
||||
|
||||
// DoubleTapXY double taps the point {X,Y}, X & Y is percentage of coordinates
|
||||
func (s *StepIOS) DoubleTapXY(x, y float64) *StepIOS {
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, MobileAction{
|
||||
Method: uiDoubleTapXY,
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, uixt.MobileAction{
|
||||
Method: uixt.ACTION_DoubleTapXY,
|
||||
Params: []float64{x, y},
|
||||
})
|
||||
return &StepIOS{step: s.step}
|
||||
}
|
||||
|
||||
func (s *StepIOS) DoubleTap(params string, options ...ActionOption) *StepIOS {
|
||||
action := MobileAction{
|
||||
Method: uiDoubleTap,
|
||||
func (s *StepIOS) DoubleTap(params string, options ...uixt.ActionOption) *StepIOS {
|
||||
action := uixt.MobileAction{
|
||||
Method: uixt.ACTION_DoubleTap,
|
||||
Params: params,
|
||||
}
|
||||
for _, option := range options {
|
||||
@@ -153,9 +145,9 @@ func (s *StepIOS) DoubleTap(params string, options ...ActionOption) *StepIOS {
|
||||
return &StepIOS{step: s.step}
|
||||
}
|
||||
|
||||
func (s *StepIOS) Swipe(sx, sy, ex, ey int, options ...ActionOption) *StepIOS {
|
||||
action := MobileAction{
|
||||
Method: uiSwipe,
|
||||
func (s *StepIOS) Swipe(sx, sy, ex, ey int, options ...uixt.ActionOption) *StepIOS {
|
||||
action := uixt.MobileAction{
|
||||
Method: uixt.ACTION_Swipe,
|
||||
Params: []int{sx, sy, ex, ey},
|
||||
}
|
||||
for _, option := range options {
|
||||
@@ -165,9 +157,9 @@ func (s *StepIOS) Swipe(sx, sy, ex, ey int, options ...ActionOption) *StepIOS {
|
||||
return &StepIOS{step: s.step}
|
||||
}
|
||||
|
||||
func (s *StepIOS) SwipeUp(options ...ActionOption) *StepIOS {
|
||||
action := MobileAction{
|
||||
Method: uiSwipe,
|
||||
func (s *StepIOS) SwipeUp(options ...uixt.ActionOption) *StepIOS {
|
||||
action := uixt.MobileAction{
|
||||
Method: uixt.ACTION_Swipe,
|
||||
Params: "up",
|
||||
}
|
||||
for _, option := range options {
|
||||
@@ -177,9 +169,9 @@ func (s *StepIOS) SwipeUp(options ...ActionOption) *StepIOS {
|
||||
return &StepIOS{step: s.step}
|
||||
}
|
||||
|
||||
func (s *StepIOS) SwipeDown(options ...ActionOption) *StepIOS {
|
||||
action := MobileAction{
|
||||
Method: uiSwipe,
|
||||
func (s *StepIOS) SwipeDown(options ...uixt.ActionOption) *StepIOS {
|
||||
action := uixt.MobileAction{
|
||||
Method: uixt.ACTION_Swipe,
|
||||
Params: "down",
|
||||
}
|
||||
for _, option := range options {
|
||||
@@ -189,9 +181,9 @@ func (s *StepIOS) SwipeDown(options ...ActionOption) *StepIOS {
|
||||
return &StepIOS{step: s.step}
|
||||
}
|
||||
|
||||
func (s *StepIOS) SwipeLeft(options ...ActionOption) *StepIOS {
|
||||
action := MobileAction{
|
||||
Method: uiSwipe,
|
||||
func (s *StepIOS) SwipeLeft(options ...uixt.ActionOption) *StepIOS {
|
||||
action := uixt.MobileAction{
|
||||
Method: uixt.ACTION_Swipe,
|
||||
Params: "left",
|
||||
}
|
||||
for _, option := range options {
|
||||
@@ -201,9 +193,9 @@ func (s *StepIOS) SwipeLeft(options ...ActionOption) *StepIOS {
|
||||
return &StepIOS{step: s.step}
|
||||
}
|
||||
|
||||
func (s *StepIOS) SwipeRight(options ...ActionOption) *StepIOS {
|
||||
action := MobileAction{
|
||||
Method: uiSwipe,
|
||||
func (s *StepIOS) SwipeRight(options ...uixt.ActionOption) *StepIOS {
|
||||
action := uixt.MobileAction{
|
||||
Method: uixt.ACTION_Swipe,
|
||||
Params: "right",
|
||||
}
|
||||
for _, option := range options {
|
||||
@@ -213,9 +205,9 @@ func (s *StepIOS) SwipeRight(options ...ActionOption) *StepIOS {
|
||||
return &StepIOS{step: s.step}
|
||||
}
|
||||
|
||||
func (s *StepIOS) SwipeToTapApp(appName string, options ...ActionOption) *StepIOS {
|
||||
action := MobileAction{
|
||||
Method: swipeToTapApp,
|
||||
func (s *StepIOS) SwipeToTapApp(appName string, options ...uixt.ActionOption) *StepIOS {
|
||||
action := uixt.MobileAction{
|
||||
Method: uixt.ACTION_SwipeToTapApp,
|
||||
Params: appName,
|
||||
}
|
||||
for _, option := range options {
|
||||
@@ -225,9 +217,9 @@ func (s *StepIOS) SwipeToTapApp(appName string, options ...ActionOption) *StepIO
|
||||
return &StepIOS{step: s.step}
|
||||
}
|
||||
|
||||
func (s *StepIOS) SwipeToTapText(text string, options ...ActionOption) *StepIOS {
|
||||
action := MobileAction{
|
||||
Method: swipeToTapText,
|
||||
func (s *StepIOS) SwipeToTapText(text string, options ...uixt.ActionOption) *StepIOS {
|
||||
action := uixt.MobileAction{
|
||||
Method: uixt.ACTION_SwipeToTapText,
|
||||
Params: text,
|
||||
}
|
||||
for _, option := range options {
|
||||
@@ -238,8 +230,8 @@ func (s *StepIOS) SwipeToTapText(text string, options ...ActionOption) *StepIOS
|
||||
}
|
||||
|
||||
func (s *StepIOS) Input(text string) *StepIOS {
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, MobileAction{
|
||||
Method: uiInput,
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, uixt.MobileAction{
|
||||
Method: uixt.ACTION_Input,
|
||||
Params: text,
|
||||
})
|
||||
return &StepIOS{step: s.step}
|
||||
@@ -268,32 +260,32 @@ func (s *StepIOS) Times(n int) *StepIOS {
|
||||
|
||||
// Sleep specify sleep seconds after last action
|
||||
func (s *StepIOS) Sleep(n float64) *StepIOS {
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, MobileAction{
|
||||
Method: ctlSleep,
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, uixt.MobileAction{
|
||||
Method: uixt.CtlSleep,
|
||||
Params: n,
|
||||
})
|
||||
return &StepIOS{step: s.step}
|
||||
}
|
||||
|
||||
func (s *StepIOS) ScreenShot() *StepIOS {
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, MobileAction{
|
||||
Method: ctlScreenShot,
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, uixt.MobileAction{
|
||||
Method: uixt.CtlScreenShot,
|
||||
Params: nil,
|
||||
})
|
||||
return &StepIOS{step: s.step}
|
||||
}
|
||||
|
||||
func (s *StepIOS) StartCamera() *StepIOS {
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, MobileAction{
|
||||
Method: ctlStartCamera,
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, uixt.MobileAction{
|
||||
Method: uixt.CtlStartCamera,
|
||||
Params: nil,
|
||||
})
|
||||
return &StepIOS{step: s.step}
|
||||
}
|
||||
|
||||
func (s *StepIOS) StopCamera() *StepIOS {
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, MobileAction{
|
||||
Method: ctlStopCamera,
|
||||
s.step.IOS.Actions = append(s.step.IOS.Actions, uixt.MobileAction{
|
||||
Method: uixt.CtlStopCamera,
|
||||
Params: nil,
|
||||
})
|
||||
return &StepIOS{step: s.step}
|
||||
@@ -329,8 +321,8 @@ type StepIOSValidation struct {
|
||||
|
||||
func (s *StepIOSValidation) AssertNameExists(expectedName string, msg ...string) *StepIOSValidation {
|
||||
v := Validator{
|
||||
Check: uiSelectorName,
|
||||
Assert: assertionExists,
|
||||
Check: uixt.SelectorName,
|
||||
Assert: uixt.AssertionExists,
|
||||
Expect: expectedName,
|
||||
}
|
||||
if len(msg) > 0 {
|
||||
@@ -344,8 +336,8 @@ func (s *StepIOSValidation) AssertNameExists(expectedName string, msg ...string)
|
||||
|
||||
func (s *StepIOSValidation) AssertNameNotExists(expectedName string, msg ...string) *StepIOSValidation {
|
||||
v := Validator{
|
||||
Check: uiSelectorName,
|
||||
Assert: assertionNotExists,
|
||||
Check: uixt.SelectorName,
|
||||
Assert: uixt.AssertionNotExists,
|
||||
Expect: expectedName,
|
||||
}
|
||||
if len(msg) > 0 {
|
||||
@@ -359,8 +351,8 @@ func (s *StepIOSValidation) AssertNameNotExists(expectedName string, msg ...stri
|
||||
|
||||
func (s *StepIOSValidation) AssertLabelExists(expectedLabel string, msg ...string) *StepIOSValidation {
|
||||
v := Validator{
|
||||
Check: uiSelectorLabel,
|
||||
Assert: assertionExists,
|
||||
Check: uixt.SelectorLabel,
|
||||
Assert: uixt.AssertionExists,
|
||||
Expect: expectedLabel,
|
||||
}
|
||||
if len(msg) > 0 {
|
||||
@@ -374,8 +366,8 @@ func (s *StepIOSValidation) AssertLabelExists(expectedLabel string, msg ...strin
|
||||
|
||||
func (s *StepIOSValidation) AssertLabelNotExists(expectedLabel string, msg ...string) *StepIOSValidation {
|
||||
v := Validator{
|
||||
Check: uiSelectorLabel,
|
||||
Assert: assertionNotExists,
|
||||
Check: uixt.SelectorLabel,
|
||||
Assert: uixt.AssertionNotExists,
|
||||
Expect: expectedLabel,
|
||||
}
|
||||
if len(msg) > 0 {
|
||||
@@ -389,8 +381,8 @@ func (s *StepIOSValidation) AssertLabelNotExists(expectedLabel string, msg ...st
|
||||
|
||||
func (s *StepIOSValidation) AssertOCRExists(expectedText string, msg ...string) *StepIOSValidation {
|
||||
v := Validator{
|
||||
Check: uiSelectorOCR,
|
||||
Assert: assertionExists,
|
||||
Check: uixt.SelectorOCR,
|
||||
Assert: uixt.AssertionExists,
|
||||
Expect: expectedText,
|
||||
}
|
||||
if len(msg) > 0 {
|
||||
@@ -404,8 +396,8 @@ func (s *StepIOSValidation) AssertOCRExists(expectedText string, msg ...string)
|
||||
|
||||
func (s *StepIOSValidation) AssertOCRNotExists(expectedText string, msg ...string) *StepIOSValidation {
|
||||
v := Validator{
|
||||
Check: uiSelectorOCR,
|
||||
Assert: assertionNotExists,
|
||||
Check: uixt.SelectorOCR,
|
||||
Assert: uixt.AssertionNotExists,
|
||||
Expect: expectedText,
|
||||
}
|
||||
if len(msg) > 0 {
|
||||
@@ -419,8 +411,8 @@ func (s *StepIOSValidation) AssertOCRNotExists(expectedText string, msg ...strin
|
||||
|
||||
func (s *StepIOSValidation) AssertImageExists(expectedImagePath string, msg ...string) *StepIOSValidation {
|
||||
v := Validator{
|
||||
Check: uiSelectorImage,
|
||||
Assert: assertionExists,
|
||||
Check: uixt.SelectorImage,
|
||||
Assert: uixt.AssertionExists,
|
||||
Expect: expectedImagePath,
|
||||
}
|
||||
if len(msg) > 0 {
|
||||
@@ -434,8 +426,8 @@ func (s *StepIOSValidation) AssertImageExists(expectedImagePath string, msg ...s
|
||||
|
||||
func (s *StepIOSValidation) AssertImageNotExists(expectedImagePath string, msg ...string) *StepIOSValidation {
|
||||
v := Validator{
|
||||
Check: uiSelectorImage,
|
||||
Assert: assertionNotExists,
|
||||
Check: uixt.SelectorImage,
|
||||
Assert: uixt.AssertionNotExists,
|
||||
Expect: expectedImagePath,
|
||||
}
|
||||
if len(msg) > 0 {
|
||||
@@ -463,35 +455,37 @@ func (s *StepIOSValidation) Run(r *SessionRunner) (*StepResult, error) {
|
||||
return runStepIOS(r, s.step)
|
||||
}
|
||||
|
||||
func (r *HRPRunner) InitWDAClient(options *WDAOptions) (client *uiDriver, err error) {
|
||||
func (r *HRPRunner) initUIClient(options uixt.Options) (client *uixt.DriverExt, err error) {
|
||||
uuid := options.UUID()
|
||||
|
||||
// avoid duplicate init
|
||||
if options.UDID == "" && len(r.wdaClients) == 1 {
|
||||
for _, v := range r.wdaClients {
|
||||
if uuid == "" && len(r.uiClients) == 1 {
|
||||
for _, v := range r.uiClients {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
|
||||
// avoid duplicate init
|
||||
if options.UDID != "" {
|
||||
if client, ok := r.wdaClients[options.UDID]; ok {
|
||||
if uuid != "" {
|
||||
if client, ok := r.uiClients[uuid]; ok {
|
||||
return client, nil
|
||||
}
|
||||
}
|
||||
|
||||
driverExt, err := uixt.InitWDAClient(options)
|
||||
if wdaOptions, ok := options.(*uixt.WDAOptions); ok {
|
||||
client, err = uixt.InitWDAClient(wdaOptions)
|
||||
} else if uiaOptions, ok := options.(*uixt.UIAOptions); ok {
|
||||
client, err = uixt.InitUIAClient(uiaOptions)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client = &uiDriver{
|
||||
DriverExt: *driverExt,
|
||||
}
|
||||
|
||||
// cache wda client
|
||||
if r.wdaClients == nil {
|
||||
r.wdaClients = make(map[string]*uiDriver)
|
||||
if r.uiClients == nil {
|
||||
r.uiClients = make(map[string]*uixt.DriverExt)
|
||||
}
|
||||
r.wdaClients[options.UDID] = client
|
||||
r.uiClients[uuid] = client
|
||||
|
||||
return client, nil
|
||||
}
|
||||
@@ -506,11 +500,11 @@ func runStepIOS(s *SessionRunner, step *TStep) (stepResult *StepResult, err erro
|
||||
screenshots := make([]string, 0)
|
||||
|
||||
// init wdaClient driver
|
||||
wdaClient, err := s.hrpRunner.InitWDAClient(&step.IOS.WDAOptions)
|
||||
wdaClient, err := s.hrpRunner.initUIClient(&step.IOS.WDAOptions)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
wdaClient.startTime = s.startTime
|
||||
wdaClient.StartTime = s.startTime
|
||||
|
||||
defer func() {
|
||||
attachments := make(map[string]interface{})
|
||||
@@ -519,7 +513,7 @@ func runStepIOS(s *SessionRunner, step *TStep) (stepResult *StepResult, err erro
|
||||
}
|
||||
|
||||
// save attachments
|
||||
screenshots = append(screenshots, wdaClient.screenShots...)
|
||||
screenshots = append(screenshots, wdaClient.ScreenShots...)
|
||||
attachments["screenshots"] = screenshots
|
||||
stepResult.Attachments = attachments
|
||||
|
||||
@@ -536,9 +530,9 @@ func runStepIOS(s *SessionRunner, step *TStep) (stepResult *StepResult, err erro
|
||||
}()
|
||||
|
||||
// prepare actions
|
||||
var actions []MobileAction
|
||||
var actions []uixt.MobileAction
|
||||
if step.IOS.Actions == nil {
|
||||
actions = []MobileAction{
|
||||
actions = []uixt.MobileAction{
|
||||
{
|
||||
Method: step.IOS.Method,
|
||||
Params: step.IOS.Params,
|
||||
@@ -550,14 +544,14 @@ func runStepIOS(s *SessionRunner, step *TStep) (stepResult *StepResult, err erro
|
||||
|
||||
// run actions
|
||||
for _, action := range actions {
|
||||
if err := wdaClient.doAction(action); err != nil {
|
||||
if err := wdaClient.DoAction(action); err != nil {
|
||||
return stepResult, err
|
||||
}
|
||||
}
|
||||
|
||||
// take snapshot
|
||||
screenshotPath, err := wdaClient.DriverExt.ScreenShot(
|
||||
fmt.Sprintf("%d_validate_%d", wdaClient.startTime.Unix(), time.Now().Unix()))
|
||||
screenshotPath, err := wdaClient.ScreenShot(
|
||||
fmt.Sprintf("%d_validate_%d", wdaClient.StartTime.Unix(), time.Now().Unix()))
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Str("step", step.Name).Msg("take screenshot failed")
|
||||
} else {
|
||||
@@ -566,7 +560,7 @@ func runStepIOS(s *SessionRunner, step *TStep) (stepResult *StepResult, err erro
|
||||
}
|
||||
|
||||
// validate
|
||||
validateResults, err := wdaClient.doValidation(step.Validators)
|
||||
validateResults, err := validateUI(wdaClient, step.Validators)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -576,257 +570,3 @@ func runStepIOS(s *SessionRunner, step *TStep) (stepResult *StepResult, err erro
|
||||
stepResult.Success = true
|
||||
return stepResult, nil
|
||||
}
|
||||
|
||||
var errActionNotImplemented = errors.New("UI action not implemented")
|
||||
|
||||
type uiDriver struct {
|
||||
uixt.DriverExt
|
||||
|
||||
startTime time.Time // used to associate screenshots name
|
||||
screenShots []string // save screenshots path
|
||||
}
|
||||
|
||||
func (ud *uiDriver) doAction(action MobileAction) error {
|
||||
log.Info().Str("method", string(action.Method)).Interface("params", action.Params).Msg("start iOS UI action")
|
||||
|
||||
switch action.Method {
|
||||
case appInstall:
|
||||
// TODO
|
||||
return errActionNotImplemented
|
||||
case appLaunch:
|
||||
if bundleId, ok := action.Params.(string); ok {
|
||||
return ud.AppLaunch(bundleId)
|
||||
}
|
||||
return fmt.Errorf("invalid %s params, should be bundleId(string), got %v",
|
||||
appLaunch, action.Params)
|
||||
case appLaunchUnattached:
|
||||
if bundleId, ok := action.Params.(string); ok {
|
||||
return ud.AppLaunchUnattached(bundleId)
|
||||
}
|
||||
return fmt.Errorf("invalid %s params, should be bundleId(string), got %v",
|
||||
appLaunchUnattached, action.Params)
|
||||
case swipeToTapApp:
|
||||
if appName, ok := action.Params.(string); ok {
|
||||
var x, y, width, height float64
|
||||
findApp := func(d *uixt.DriverExt) error {
|
||||
var err error
|
||||
x, y, width, height, err = d.FindTextByOCR(appName)
|
||||
return err
|
||||
}
|
||||
foundAppAction := func(d *uixt.DriverExt) error {
|
||||
// click app to launch
|
||||
return d.TapFloat(x+width*0.5, y+height*0.5-20)
|
||||
}
|
||||
|
||||
// go to home screen
|
||||
if err := ud.WebDriver.Homescreen(); err != nil {
|
||||
return errors.Wrap(err, "go to home screen failed")
|
||||
}
|
||||
|
||||
// swipe to first screen
|
||||
for i := 0; i < 5; i++ {
|
||||
ud.SwipeRight()
|
||||
}
|
||||
|
||||
// default to retry 5 times
|
||||
if action.MaxRetryTimes == 0 {
|
||||
action.MaxRetryTimes = 5
|
||||
}
|
||||
// swipe next screen until app found
|
||||
return ud.SwipeUntil("left", findApp, foundAppAction, action.MaxRetryTimes)
|
||||
}
|
||||
return fmt.Errorf("invalid %s params, should be app name(string), got %v",
|
||||
swipeToTapApp, action.Params)
|
||||
case swipeToTapText:
|
||||
if text, ok := action.Params.(string); ok {
|
||||
var x, y, width, height float64
|
||||
findText := func(d *uixt.DriverExt) error {
|
||||
var err error
|
||||
x, y, width, height, err = d.FindTextByOCR(text)
|
||||
return err
|
||||
}
|
||||
foundTextAction := func(d *uixt.DriverExt) error {
|
||||
// tap text
|
||||
return d.TapFloat(x+width*0.5, y+height*0.5)
|
||||
}
|
||||
|
||||
// default to retry 10 times
|
||||
if action.MaxRetryTimes == 0 {
|
||||
action.MaxRetryTimes = 10
|
||||
}
|
||||
// swipe until live room found
|
||||
return ud.SwipeUntil("up", findText, foundTextAction, action.MaxRetryTimes)
|
||||
}
|
||||
return fmt.Errorf("invalid %s params, should be app text(string), got %v",
|
||||
swipeToTapText, action.Params)
|
||||
case appTerminate:
|
||||
if bundleId, ok := action.Params.(string); ok {
|
||||
success, err := ud.AppTerminate(bundleId)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to terminate app")
|
||||
}
|
||||
if !success {
|
||||
log.Warn().Str("bundleId", bundleId).Msg("app was not running")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("app_terminate params should be bundleId(string), got %v", action.Params)
|
||||
case uiHome:
|
||||
return ud.Homescreen()
|
||||
case uiTapXY:
|
||||
if location, ok := action.Params.([]float64); ok {
|
||||
// relative x,y of window size: [0.5, 0.5]
|
||||
if len(location) != 2 {
|
||||
return fmt.Errorf("invalid tap location params: %v", location)
|
||||
}
|
||||
return ud.TapXY(location[0], location[1], action.Identifier)
|
||||
}
|
||||
return fmt.Errorf("invalid %s params: %v", uiTapXY, action.Params)
|
||||
case uiTap:
|
||||
if param, ok := action.Params.(string); ok {
|
||||
return ud.Tap(param, action.Identifier, action.IgnoreNotFoundError)
|
||||
}
|
||||
return fmt.Errorf("invalid %s params: %v", uiTap, action.Params)
|
||||
case uiTapByOCR:
|
||||
if ocrText, ok := action.Params.(string); ok {
|
||||
return ud.TapByOCR(ocrText, action.Identifier, action.IgnoreNotFoundError)
|
||||
}
|
||||
return fmt.Errorf("invalid %s params: %v", uiTapByOCR, action.Params)
|
||||
case uiTapByCV:
|
||||
if imagePath, ok := action.Params.(string); ok {
|
||||
return ud.TapByCV(imagePath, action.Identifier, action.IgnoreNotFoundError)
|
||||
}
|
||||
return fmt.Errorf("invalid %s params: %v", uiTapByCV, action.Params)
|
||||
case uiDoubleTapXY:
|
||||
if location, ok := action.Params.([]float64); ok {
|
||||
// relative x,y of window size: [0.5, 0.5]
|
||||
if len(location) != 2 {
|
||||
return fmt.Errorf("invalid tap location params: %v", location)
|
||||
}
|
||||
return ud.DoubleTapXY(location[0], location[1])
|
||||
}
|
||||
return fmt.Errorf("invalid %s params: %v", uiDoubleTapXY, action.Params)
|
||||
case uiDoubleTap:
|
||||
if param, ok := action.Params.(string); ok {
|
||||
return ud.DoubleTap(param)
|
||||
}
|
||||
return fmt.Errorf("invalid %s params: %v", uiDoubleTap, action.Params)
|
||||
case uiSwipe:
|
||||
if positions, ok := action.Params.([]float64); ok {
|
||||
// relative fromX, fromY, toX, toY of window size: [0.5, 0.9, 0.5, 0.1]
|
||||
if len(positions) != 4 {
|
||||
return fmt.Errorf("invalid swipe params [fromX, fromY, toX, toY]: %v", positions)
|
||||
}
|
||||
return ud.SwipeRelative(
|
||||
positions[0], positions[1], positions[2], positions[3], action.Identifier)
|
||||
}
|
||||
if direction, ok := action.Params.(string); ok {
|
||||
return ud.SwipeTo(direction, action.Identifier)
|
||||
}
|
||||
return fmt.Errorf("invalid %s params: %v", uiSwipe, action.Params)
|
||||
case uiInput:
|
||||
// input text on current active element
|
||||
// append \n to send text with enter
|
||||
// send \b\b\b to delete 3 chars
|
||||
param := fmt.Sprintf("%v", action.Params)
|
||||
return ud.SendKeys(param)
|
||||
case ctlSleep:
|
||||
if param, ok := action.Params.(json.Number); ok {
|
||||
seconds, _ := param.Float64()
|
||||
time.Sleep(time.Duration(seconds*1000) * time.Millisecond)
|
||||
return nil
|
||||
} else if param, ok := action.Params.(float64); ok {
|
||||
time.Sleep(time.Duration(param*1000) * time.Millisecond)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("invalid sleep params: %v(%T)", action.Params, action.Params)
|
||||
case ctlScreenShot:
|
||||
// take snapshot
|
||||
log.Info().Msg("take snapshot for current screen")
|
||||
screenshotPath, err := ud.ScreenShot(fmt.Sprintf("%d_screenshot_%d",
|
||||
ud.startTime.Unix(), time.Now().Unix()))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "take screenshot failed")
|
||||
}
|
||||
log.Info().Str("path", screenshotPath).Msg("take screenshot")
|
||||
ud.screenShots = append(ud.screenShots, screenshotPath)
|
||||
return err
|
||||
case ctlStartCamera:
|
||||
// start camera, alias for app_launch com.apple.camera
|
||||
return ud.AppLaunch("com.apple.camera")
|
||||
case ctlStopCamera:
|
||||
// stop camera, alias for app_terminate com.apple.camera
|
||||
success, err := ud.AppTerminate("com.apple.camera")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to terminate camera")
|
||||
}
|
||||
if !success {
|
||||
log.Warn().Msg("camera was not running")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ud *uiDriver) doValidation(iValidators []interface{}) (validateResults []*ValidationResult, err error) {
|
||||
for _, iValidator := range iValidators {
|
||||
validator, ok := iValidator.(Validator)
|
||||
if !ok {
|
||||
return nil, errors.New("validator type error")
|
||||
}
|
||||
|
||||
validataResult := &ValidationResult{
|
||||
Validator: validator,
|
||||
CheckResult: "fail",
|
||||
}
|
||||
|
||||
// parse check value
|
||||
if !strings.HasPrefix(validator.Check, "ui_") {
|
||||
validataResult.CheckResult = "skip"
|
||||
log.Warn().Interface("validator", validator).Msg("skip validator")
|
||||
validateResults = append(validateResults, validataResult)
|
||||
continue
|
||||
}
|
||||
|
||||
expected, ok := validator.Expect.(string)
|
||||
if !ok {
|
||||
return nil, errors.New("validator expect should be string")
|
||||
}
|
||||
|
||||
var exists bool
|
||||
if validator.Assert == assertionExists {
|
||||
exists = true
|
||||
} else {
|
||||
exists = false
|
||||
}
|
||||
var result bool
|
||||
switch validator.Check {
|
||||
case uiSelectorName:
|
||||
result = (ud.IsNameExist(expected) == exists)
|
||||
case uiSelectorLabel:
|
||||
result = (ud.IsLabelExist(expected) == exists)
|
||||
case uiSelectorOCR:
|
||||
result = (ud.IsOCRExist(expected) == exists)
|
||||
case uiSelectorImage:
|
||||
result = (ud.IsImageExist(expected) == exists)
|
||||
}
|
||||
|
||||
if result {
|
||||
log.Info().
|
||||
Str("assert", validator.Assert).
|
||||
Str("expect", expected).
|
||||
Msg("validate UI success")
|
||||
validataResult.CheckResult = "pass"
|
||||
validateResults = append(validateResults, validataResult)
|
||||
} else {
|
||||
log.Error().
|
||||
Str("assert", validator.Assert).
|
||||
Str("expect", expected).
|
||||
Str("msg", validator.Message).
|
||||
Msg("validate UI failed")
|
||||
validateResults = append(validateResults, validataResult)
|
||||
return validateResults, errors.New("step validation failed")
|
||||
}
|
||||
}
|
||||
return validateResults, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user