mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-13 08:59:44 +08:00
fix: add option match_one for FindTexts method
This commit is contained in:
@@ -105,9 +105,10 @@ type ActionOptions struct {
|
||||
Scope Scope `json:"scope,omitempty" yaml:"scope,omitempty"`
|
||||
AbsScope AbsScope `json:"abs_scope,omitempty" yaml:"abs_scope,omitempty"`
|
||||
|
||||
Regex bool `json:"regex,omitempty" yaml:"regex,omitempty"` // use regex to match text
|
||||
Offset []int `json:"offset,omitempty" yaml:"offset,omitempty"` // used to tap offset of point
|
||||
Index int `json:"index,omitempty" yaml:"index,omitempty"` // index of the target element
|
||||
Regex bool `json:"regex,omitempty" yaml:"regex,omitempty"` // use regex to match text
|
||||
Offset []int `json:"offset,omitempty" yaml:"offset,omitempty"` // used to tap offset of point
|
||||
Index int `json:"index,omitempty" yaml:"index,omitempty"` // index of the target element
|
||||
MatchOne bool `json:"match_one,omitempty" yaml:"match_one,omitempty"` // match one of the targets if existed
|
||||
|
||||
// set custiom options such as textview, id, description
|
||||
Custom map[string]interface{} `json:"custom,omitempty" yaml:"custom,omitempty"`
|
||||
@@ -183,6 +184,12 @@ func (o *ActionOptions) Options() []ActionOption {
|
||||
if o.Regex {
|
||||
options = append(options, WithRegex(true))
|
||||
}
|
||||
if o.Index != 0 {
|
||||
options = append(options, WithIndex(o.Index))
|
||||
}
|
||||
if o.MatchOne {
|
||||
options = append(options, WithMatchOne(true))
|
||||
}
|
||||
|
||||
// custom options
|
||||
if o.Custom != nil {
|
||||
@@ -377,6 +384,12 @@ func WithRegex(regex bool) ActionOption {
|
||||
}
|
||||
}
|
||||
|
||||
func WithMatchOne(matchOne bool) ActionOption {
|
||||
return func(o *ActionOptions) {
|
||||
o.MatchOne = matchOne
|
||||
}
|
||||
}
|
||||
|
||||
func WithFrequency(frequency int) ActionOption {
|
||||
return func(o *ActionOptions) {
|
||||
o.Frequency = frequency
|
||||
@@ -496,8 +509,10 @@ func (dExt *DriverExt) DoAction(action MobileAction) (err error) {
|
||||
if texts, ok := action.Params.([]string); ok {
|
||||
return dExt.swipeToTapTexts(texts, action.GetOptions()...)
|
||||
}
|
||||
return fmt.Errorf("invalid %s params, should be app text([]string), got %v",
|
||||
ACTION_SwipeToTapText, action.Params)
|
||||
if texts, err := convertToStringSlice(action.Params); err == nil {
|
||||
return dExt.swipeToTapTexts(texts, action.GetOptions()...)
|
||||
}
|
||||
return fmt.Errorf("invalid %s params: %v", ACTION_SwipeToTapTexts, action.Params)
|
||||
case ACTION_AppTerminate:
|
||||
if bundleId, ok := action.Params.(string); ok {
|
||||
success, err := dExt.Driver.AppTerminate(bundleId)
|
||||
@@ -636,6 +651,21 @@ func convertToFloat64(val interface{}) (float64, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func convertToStringSlice(val interface{}) ([]string, error) {
|
||||
if valSlice, ok := val.([]interface{}); ok {
|
||||
var res []string
|
||||
for _, iVal := range valSlice {
|
||||
valString, ok := iVal.(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid type for converting one of the elements to string: %T, value: %v", iVal, iVal)
|
||||
}
|
||||
res = append(res, valString)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
return nil, fmt.Errorf("invalid type for conversion to []string")
|
||||
}
|
||||
|
||||
// getSimulationDuration returns simulation duration by given params (in seconds)
|
||||
func getSimulationDuration(params []interface{}) (milliseconds int64) {
|
||||
if len(params) == 1 {
|
||||
|
||||
@@ -154,6 +154,7 @@ func (t OCRTexts) FindText(text string, options ...ActionOption) (result OCRText
|
||||
}
|
||||
|
||||
func (t OCRTexts) FindTexts(texts []string, options ...ActionOption) (results OCRTexts, err error) {
|
||||
actionOptions := NewActionOptions(options...)
|
||||
for _, text := range texts {
|
||||
ocrText, err := t.FindText(text, options...)
|
||||
if err != nil {
|
||||
@@ -162,7 +163,12 @@ func (t OCRTexts) FindTexts(texts []string, options ...ActionOption) (results OC
|
||||
results = append(results, ocrText)
|
||||
}
|
||||
|
||||
if len(results) != len(texts) {
|
||||
if actionOptions.MatchOne && len(results) == 0 {
|
||||
return nil, errors.Wrap(code.CVResultNotFoundError,
|
||||
fmt.Sprintf("texts %s not found in %v", texts, t.texts()))
|
||||
}
|
||||
|
||||
if !actionOptions.MatchOne && len(results) != len(texts) {
|
||||
return nil, errors.Wrap(code.CVResultNotFoundError,
|
||||
fmt.Sprintf("texts %s not found in %v", texts, t.texts()))
|
||||
}
|
||||
|
||||
@@ -132,6 +132,7 @@ func (dExt *DriverExt) swipeToTapTexts(texts []string, options ...ActionOption)
|
||||
return errors.New("no text to tap")
|
||||
}
|
||||
|
||||
options = append(options, WithMatchOne(true))
|
||||
var point PointF
|
||||
findTexts := func(d *DriverExt) error {
|
||||
var err error
|
||||
|
||||
Reference in New Issue
Block a user