mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-31 21:39:41 +08:00
refactor: run step with specified times
This commit is contained in:
@@ -872,7 +872,9 @@ func WithScreenShot(fileName ...string) DataOption {
|
||||
}
|
||||
|
||||
func NewDataOptions(options ...DataOption) *DataOptions {
|
||||
dataOptions := &DataOptions{}
|
||||
dataOptions := &DataOptions{
|
||||
Data: make(map[string]interface{}),
|
||||
}
|
||||
for _, option := range options {
|
||||
option(dataOptions)
|
||||
}
|
||||
|
||||
@@ -159,6 +159,9 @@ func GetIOSDeviceOptions(dev *IOSDevice) (deviceOptions []IOSDeviceOption) {
|
||||
if dev.PerfOptions != nil {
|
||||
deviceOptions = append(deviceOptions, WithPerfOptions(dev.perfOpitons()...))
|
||||
}
|
||||
if dev.XCTestBundleID != "" {
|
||||
deviceOptions = append(deviceOptions, WithXCTest(dev.XCTestBundleID))
|
||||
}
|
||||
if dev.ResetHomeOnStartup {
|
||||
deviceOptions = append(deviceOptions, WithResetHomeOnStartup(true))
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ var (
|
||||
WithAcceptAlertButtonSelector = uixt.WithAcceptAlertButtonSelector
|
||||
WithDismissAlertButtonSelector = uixt.WithDismissAlertButtonSelector
|
||||
WithPerfOptions = uixt.WithPerfOptions
|
||||
WithXCTest = uixt.WithXCTest
|
||||
)
|
||||
|
||||
// android setting options
|
||||
@@ -34,6 +35,7 @@ var (
|
||||
|
||||
type MobileStep struct {
|
||||
Serial string `json:"serial,omitempty" yaml:"serial,omitempty"`
|
||||
Times int `json:"times,omitempty" yaml:"times,omitempty"`
|
||||
uixt.MobileAction `yaml:",inline"`
|
||||
Actions []uixt.MobileAction `json:"actions,omitempty" yaml:"actions,omitempty"`
|
||||
}
|
||||
@@ -301,25 +303,9 @@ func (s *StepMobile) Input(text string, options ...uixt.ActionOption) *StepMobil
|
||||
return &StepMobile{step: s.step}
|
||||
}
|
||||
|
||||
// Times specify running times for run last action
|
||||
func (s *StepMobile) Times(n int) *StepMobile {
|
||||
if n <= 0 {
|
||||
log.Warn().Int("n", n).Msg("times should be positive, set to 1")
|
||||
n = 1
|
||||
}
|
||||
|
||||
mobileStep := s.mobileStep()
|
||||
actionsTotal := len(mobileStep.Actions)
|
||||
if actionsTotal == 0 {
|
||||
return s
|
||||
}
|
||||
|
||||
// actionsTotal >=1 && n >= 1
|
||||
lastAction := mobileStep.Actions[actionsTotal-1 : actionsTotal][0]
|
||||
for i := 0; i < n-1; i++ {
|
||||
// duplicate last action n-1 times
|
||||
mobileStep.Actions = append(mobileStep.Actions, lastAction)
|
||||
}
|
||||
// LoopTimes specify running times for the current step
|
||||
func (s *StepMobile) LoopTimes(n int) *StepMobile {
|
||||
s.mobileStep().Times = n
|
||||
return &StepMobile{step: s.step}
|
||||
}
|
||||
|
||||
@@ -622,20 +608,33 @@ func runStepMobileUI(s *SessionRunner, step *TStep) (stepResult *StepResult, err
|
||||
actions = mobileStep.Actions
|
||||
}
|
||||
|
||||
// run actions
|
||||
for _, action := range actions {
|
||||
if action.Params, err = s.caseRunner.parser.Parse(action.Params, stepVariables); err != nil {
|
||||
if !code.IsErrorPredefined(err) {
|
||||
err = errors.Wrap(code.ParseError,
|
||||
fmt.Sprintf("parse action params failed: %v", err))
|
||||
// run times
|
||||
times := mobileStep.Times
|
||||
if times < 0 {
|
||||
log.Warn().Int("times", times).Msg("times should be positive, set to 1")
|
||||
times = 1
|
||||
} else if times == 0 {
|
||||
times = 1
|
||||
} else if times > 1 {
|
||||
log.Info().Int("times", times).Msg("run actions with specified times")
|
||||
}
|
||||
|
||||
// run actions with specified times
|
||||
for i := 0; i < times; i++ {
|
||||
for _, action := range actions {
|
||||
if action.Params, err = s.caseRunner.parser.Parse(action.Params, stepVariables); err != nil {
|
||||
if !code.IsErrorPredefined(err) {
|
||||
err = errors.Wrap(code.ParseError,
|
||||
fmt.Sprintf("parse action params failed: %v", err))
|
||||
}
|
||||
return stepResult, err
|
||||
}
|
||||
return stepResult, err
|
||||
}
|
||||
if err := uiDriver.DoAction(action); err != nil {
|
||||
if !code.IsErrorPredefined(err) {
|
||||
err = errors.Wrap(code.MobileUIDriverError, err.Error())
|
||||
if err := uiDriver.DoAction(action); err != nil {
|
||||
if !code.IsErrorPredefined(err) {
|
||||
err = errors.Wrap(code.MobileUIDriverError, err.Error())
|
||||
}
|
||||
return stepResult, err
|
||||
}
|
||||
return stepResult, err
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ func TestIOSSearchApp(t *testing.T) {
|
||||
Config: NewConfig("ios ui action on Search App 资源库"),
|
||||
TestSteps: []IStep{
|
||||
NewStep("进入 App 资源库 搜索框").
|
||||
IOS().Home().SwipeLeft().Times(2).Tap("dewey-search-field").
|
||||
IOS().Home().SwipeLeft().SwipeLeft().Tap("dewey-search-field").
|
||||
Validate().
|
||||
AssertLabelExists("取消"),
|
||||
NewStep("搜索抖音").
|
||||
@@ -84,10 +84,10 @@ func TestIOSWeixinLive(t *testing.T) {
|
||||
TapByOCR("直播"). // 通过 OCR 识别「直播」
|
||||
Validate().
|
||||
AssertLabelExists("直播"),
|
||||
NewStep("向上滑动 5 次").
|
||||
NewStep("向上滑动 3 次,截图保存").
|
||||
IOS().
|
||||
SwipeUp().Times(3).ScreenShot(). // 上划 3 次,截图保存
|
||||
SwipeUp().Times(2).ScreenShot(), // 再上划 2 次,截图保存
|
||||
LoopTimes(3). // 整体循环 3 次
|
||||
SwipeUp().SwipeUp().ScreenShot(), // 上划 2 次,截图保存
|
||||
},
|
||||
}
|
||||
err := NewRunner(t).Run(testCase)
|
||||
@@ -154,7 +154,9 @@ func TestIOSDouyinAction(t *testing.T) {
|
||||
AssertLabelExists("首页", "首页 tab 不存在").
|
||||
AssertLabelExists("消息", "消息 tab 不存在"),
|
||||
NewStep("swipe up and down").
|
||||
IOS().SwipeUp().Times(3).SwipeDown(),
|
||||
IOS().
|
||||
LoopTimes(3).
|
||||
SwipeUp().SwipeDown(),
|
||||
},
|
||||
}
|
||||
err := NewRunner(t).Run(testCase)
|
||||
|
||||
Reference in New Issue
Block a user