mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-07 00:17:37 +08:00
feat: find text with regex
This commit is contained in:
+11
-6
@@ -85,13 +85,9 @@ type ActionOptions struct {
|
|||||||
Scope []float64 `json:"scope,omitempty" yaml:"scope,omitempty"` // [x1, y1, x2, y2] in percentage of the screen
|
Scope []float64 `json:"scope,omitempty" yaml:"scope,omitempty"` // [x1, y1, x2, y2] in percentage of the screen
|
||||||
AbsScope []int `json:"abs_scope,omitempty" yaml:"abs_scope,omitempty"` // [x1, y1, x2, y2] in absolute pixels
|
AbsScope []int `json:"abs_scope,omitempty" yaml:"abs_scope,omitempty"` // [x1, y1, x2, y2] in absolute pixels
|
||||||
|
|
||||||
|
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, should start from 1
|
Index int `json:"index,omitempty" yaml:"index,omitempty"` // index of the target element
|
||||||
|
|
||||||
// element related
|
|
||||||
Text string `json:"text,omitempty" yaml:"text,omitempty"`
|
|
||||||
ID string `json:"id,omitempty" yaml:"id,omitempty"`
|
|
||||||
Description string `json:"description,omitempty" yaml:"description,omitempty"`
|
|
||||||
|
|
||||||
// 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"`
|
||||||
@@ -154,6 +150,9 @@ func (o *ActionOptions) Options() []ActionOption {
|
|||||||
if len(o.Offset) == 2 {
|
if len(o.Offset) == 2 {
|
||||||
options = append(options, WithOffset(o.Offset[0], o.Offset[1]))
|
options = append(options, WithOffset(o.Offset[0], o.Offset[1]))
|
||||||
}
|
}
|
||||||
|
if o.Regex {
|
||||||
|
options = append(options, WithRegex(true))
|
||||||
|
}
|
||||||
|
|
||||||
// custom options
|
// custom options
|
||||||
if o.Custom != nil {
|
if o.Custom != nil {
|
||||||
@@ -307,6 +306,12 @@ func WithOffset(offsetX, offsetY int) ActionOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithRegex(regex bool) ActionOption {
|
||||||
|
return func(o *ActionOptions) {
|
||||||
|
o.Regex = regex
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func WithFrequency(frequency int) ActionOption {
|
func WithFrequency(frequency int) ActionOption {
|
||||||
return func(o *ActionOptions) {
|
return func(o *ActionOptions) {
|
||||||
o.Frequency = frequency
|
o.Frequency = frequency
|
||||||
|
|||||||
+12
-17
@@ -7,7 +7,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@@ -68,22 +68,19 @@ func (t OCRTexts) FindText(text string, options ...ActionOption) (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// not contains text
|
if actionOptions.Regex {
|
||||||
if !strings.Contains(ocrText.Text, text) {
|
// regex on, check if match regex
|
||||||
|
if !regexp.MustCompile(text).MatchString(ocrText.Text) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
rects = append(rects, rect)
|
// regex off, check if match exactly
|
||||||
|
|
||||||
// contains text while not match exactly
|
|
||||||
if ocrText.Text != text {
|
if ocrText.Text != text {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// match exactly, and not specify index, return the first one
|
|
||||||
if actionOptions.Index == 0 {
|
|
||||||
return getRectangleCenterPoint(rect), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rects = append(rects, rect)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(rects) == 0 {
|
if len(rects) == 0 {
|
||||||
@@ -93,15 +90,12 @@ func (t OCRTexts) FindText(text string, options ...ActionOption) (
|
|||||||
|
|
||||||
// get index
|
// get index
|
||||||
idx := actionOptions.Index
|
idx := actionOptions.Index
|
||||||
if idx > 0 {
|
if idx < 0 {
|
||||||
// NOTICE: index start from 1
|
|
||||||
idx = idx - 1
|
|
||||||
} else if idx < 0 {
|
|
||||||
idx = len(rects) + idx
|
idx = len(rects) + idx
|
||||||
}
|
}
|
||||||
|
|
||||||
// index out of range
|
// index out of range
|
||||||
if idx >= len(rects) {
|
if idx >= len(rects) || idx < 0 {
|
||||||
return PointF{}, errors.Wrap(code.OCRTextNotFoundError,
|
return PointF{}, errors.Wrap(code.OCRTextNotFoundError,
|
||||||
fmt.Sprintf("text %s found %d, index %d out of range", text, len(rects), idx))
|
fmt.Sprintf("text %s found %d, index %d out of range", text, len(rects), idx))
|
||||||
}
|
}
|
||||||
@@ -109,7 +103,8 @@ func (t OCRTexts) FindText(text string, options ...ActionOption) (
|
|||||||
return getRectangleCenterPoint(rects[idx]), nil
|
return getRectangleCenterPoint(rects[idx]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t OCRTexts) FindTexts(texts []string, options ...ActionOption) (points []PointF, err error) {
|
func (t OCRTexts) FindTexts(texts []string, options ...ActionOption) (
|
||||||
|
points []PointF, err error) {
|
||||||
for _, text := range texts {
|
for _, text := range texts {
|
||||||
point, err := t.FindText(text, options...)
|
point, err := t.FindText(text, options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user