mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-05 22:41:22 +08:00
Merge pull request #1680 from httprunner/fix-swipeToTapTexts
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"`
|
Scope Scope `json:"scope,omitempty" yaml:"scope,omitempty"`
|
||||||
AbsScope AbsScope `json:"abs_scope,omitempty" yaml:"abs_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
|
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
|
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
|
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
|
// set custiom options such as textview, id, description
|
||||||
Custom map[string]interface{} `json:"custom,omitempty" yaml:"custom,omitempty"`
|
Custom map[string]interface{} `json:"custom,omitempty" yaml:"custom,omitempty"`
|
||||||
@@ -183,6 +184,12 @@ func (o *ActionOptions) Options() []ActionOption {
|
|||||||
if o.Regex {
|
if o.Regex {
|
||||||
options = append(options, WithRegex(true))
|
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
|
// custom options
|
||||||
if o.Custom != nil {
|
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 {
|
func WithFrequency(frequency int) ActionOption {
|
||||||
return func(o *ActionOptions) {
|
return func(o *ActionOptions) {
|
||||||
o.Frequency = frequency
|
o.Frequency = frequency
|
||||||
@@ -496,8 +509,10 @@ func (dExt *DriverExt) DoAction(action MobileAction) (err error) {
|
|||||||
if texts, ok := action.Params.([]string); ok {
|
if texts, ok := action.Params.([]string); ok {
|
||||||
return dExt.swipeToTapTexts(texts, action.GetOptions()...)
|
return dExt.swipeToTapTexts(texts, action.GetOptions()...)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("invalid %s params, should be app text([]string), got %v",
|
if texts, err := convertToStringSlice(action.Params); err == nil {
|
||||||
ACTION_SwipeToTapText, action.Params)
|
return dExt.swipeToTapTexts(texts, action.GetOptions()...)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("invalid %s params: %v", ACTION_SwipeToTapTexts, action.Params)
|
||||||
case ACTION_AppTerminate:
|
case ACTION_AppTerminate:
|
||||||
if bundleId, ok := action.Params.(string); ok {
|
if bundleId, ok := action.Params.(string); ok {
|
||||||
success, err := dExt.Driver.AppTerminate(bundleId)
|
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)
|
// getSimulationDuration returns simulation duration by given params (in seconds)
|
||||||
func getSimulationDuration(params []interface{}) (milliseconds int64) {
|
func getSimulationDuration(params []interface{}) (milliseconds int64) {
|
||||||
if len(params) == 1 {
|
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) {
|
func (t OCRTexts) FindTexts(texts []string, options ...ActionOption) (results OCRTexts, err error) {
|
||||||
|
actionOptions := NewActionOptions(options...)
|
||||||
for _, text := range texts {
|
for _, text := range texts {
|
||||||
ocrText, err := t.FindText(text, options...)
|
ocrText, err := t.FindText(text, options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -162,11 +163,16 @@ func (t OCRTexts) FindTexts(texts []string, options ...ActionOption) (results OC
|
|||||||
results = append(results, ocrText)
|
results = append(results, ocrText)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(results) != len(texts) {
|
if len(results) == len(texts) {
|
||||||
return nil, errors.Wrap(code.CVResultNotFoundError,
|
return results, nil
|
||||||
fmt.Sprintf("texts %s not found in %v", texts, t.texts()))
|
|
||||||
}
|
}
|
||||||
return results, nil
|
|
||||||
|
if actionOptions.MatchOne && len(results) > 0 {
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, errors.Wrap(code.CVResultNotFoundError,
|
||||||
|
fmt.Sprintf("texts %s not found in %v", texts, t.texts()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func newVEDEMImageService() (*veDEMImageService, error) {
|
func newVEDEMImageService() (*veDEMImageService, error) {
|
||||||
|
|||||||
@@ -132,6 +132,7 @@ func (dExt *DriverExt) swipeToTapTexts(texts []string, options ...ActionOption)
|
|||||||
return errors.New("no text to tap")
|
return errors.New("no text to tap")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
options = append(options, WithMatchOne(true))
|
||||||
var point PointF
|
var point PointF
|
||||||
findTexts := func(d *DriverExt) error {
|
findTexts := func(d *DriverExt) error {
|
||||||
var err error
|
var err error
|
||||||
|
|||||||
Reference in New Issue
Block a user