fix: modify function call parameters

This commit is contained in:
xucong.053
2022-10-16 23:33:53 +08:00
parent 7b0a442a7a
commit 1ed4fcd1e0
5 changed files with 116 additions and 77 deletions
+1 -1
View File
@@ -32,7 +32,7 @@ func TestIOSDemo(t *testing.T) {
// 持续监测手机屏幕,直到出现青少年模式弹窗后,点击「我知道了」 // 持续监测手机屏幕,直到出现青少年模式弹窗后,点击「我知道了」
for { for {
points, err := driverExt.GetTextXYs([]string{"青少年模式", "我知道了"}, nil) points, err := driverExt.GetTextXYs([]string{"青少年模式", "我知道了"})
if err != nil { if err != nil {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
continue continue
+68 -10
View File
@@ -67,7 +67,7 @@ type MobileAction struct {
Identifier string `json:"identifier,omitempty" yaml:"identifier,omitempty"` // used to identify the action in log Identifier string `json:"identifier,omitempty" yaml:"identifier,omitempty"` // used to identify the action in log
MaxRetryTimes int `json:"max_retry_times,omitempty" yaml:"max_retry_times,omitempty"` // max retry times MaxRetryTimes int `json:"max_retry_times,omitempty" yaml:"max_retry_times,omitempty"` // max retry times
Direction interface{} `json:"direction,omitempty" yaml:"direction,omitempty"` // used by swipe to tap text or app Direction interface{} `json:"direction,omitempty" yaml:"direction,omitempty"` // used by swipe to tap text or app
RecognitionArea []float64 `json:"recognition_area,omitempty" yaml:"recognition_area,omitempty"` // used by ocr to get text position in the recognition area Scope []float64 `json:"scope,omitempty" yaml:"scope,omitempty"` // used by ocr to get text position in the scope
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, should start from 1
Timeout int `json:"timeout,omitempty" yaml:"timeout,omitempty"` // TODO: wait timeout in seconds for mobile action Timeout int `json:"timeout,omitempty" yaml:"timeout,omitempty"` // TODO: wait timeout in seconds for mobile action
IgnoreNotFoundError bool `json:"ignore_NotFoundError,omitempty" yaml:"ignore_NotFoundError,omitempty"` // ignore error if target element not found IgnoreNotFoundError bool `json:"ignore_NotFoundError,omitempty" yaml:"ignore_NotFoundError,omitempty"` // ignore error if target element not found
@@ -104,10 +104,10 @@ func WithCustomDirection(sx, sy, ex, ey float64) ActionOption {
} }
} }
// WithRecognitionArea inputs area of [(x1,y1), (x2,y2)] // WithScope inputs area of [(x1,y1), (x2,y2)]
func WithRecognitionArea(x1, y1, x2, y2 float64) ActionOption { func WithScope(x1, y1, x2, y2 float64) ActionOption {
return func(o *MobileAction) { return func(o *MobileAction) {
o.RecognitionArea = []float64{x1, y1, x2, y2} o.Scope = []float64{x1, y1, x2, y2}
} }
} }
@@ -310,7 +310,7 @@ func (dExt *DriverExt) FindUIElement(param string) (ele WebElement, err error) {
func (dExt *DriverExt) FindUIRectInUIKit(search string, index ...int) (x, y, width, height float64, err error) { func (dExt *DriverExt) FindUIRectInUIKit(search string, index ...int) (x, y, width, height float64, err error) {
// click on text, using OCR // click on text, using OCR
if !isPathExists(search) { if !isPathExists(search) {
return dExt.FindTextByOCR(search, nil, index...) return dExt.FindTextByOCR(search, WithCustomOption("index", index))
} }
// click on image, using opencv // click on image, using opencv
return dExt.FindImageRectInUIKit(search, index...) return dExt.FindImageRectInUIKit(search, index...)
@@ -347,7 +347,7 @@ func (dExt *DriverExt) IsLabelExist(label string) bool {
} }
func (dExt *DriverExt) IsOCRExist(text string) bool { func (dExt *DriverExt) IsOCRExist(text string) bool {
_, _, _, _, err := dExt.FindTextByOCR(text, nil) _, _, _, _, err := dExt.FindTextByOCR(text)
return err == nil return err == nil
} }
@@ -379,10 +379,25 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
AppLaunchUnattached, action.Params) AppLaunchUnattached, action.Params)
case ACTION_SwipeToTapApp: case ACTION_SwipeToTapApp:
if appName, ok := action.Params.(string); ok { if appName, ok := action.Params.(string); ok {
if len(action.Scope) != 4 {
action.Scope = []float64{0, 0, 1, 1}
}
var options []DataOption
options = append(options,
WithCustomOption("index", []int{action.Index}),
WithCustomOption("scope", []int{
int(action.Scope[0] * float64(dExt.windowSize.Width) * dExt.scale),
int(action.Scope[1] * float64(dExt.windowSize.Height) * dExt.scale),
int(action.Scope[2] * float64(dExt.windowSize.Width) * dExt.scale),
int(action.Scope[3] * float64(dExt.windowSize.Height) * dExt.scale),
}),
)
var point PointF var point PointF
findApp := func(d *DriverExt) error { findApp := func(d *DriverExt) error {
var err error var err error
point, err = d.GetTextXY(appName, action.RecognitionArea, action.Index) point, err = d.GetTextXY(appName, options...)
return err return err
} }
foundAppAction := func(d *DriverExt) error { foundAppAction := func(d *DriverExt) error {
@@ -411,10 +426,25 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
ACTION_SwipeToTapApp, action.Params) ACTION_SwipeToTapApp, action.Params)
case ACTION_SwipeToTapText: case ACTION_SwipeToTapText:
if text, ok := action.Params.(string); ok { if text, ok := action.Params.(string); ok {
if len(action.Scope) != 4 {
action.Scope = []float64{0, 0, 1, 1}
}
var options []DataOption
options = append(options,
WithCustomOption("index", []int{action.Index}),
WithCustomOption("scope", []int{
int(action.Scope[0] * float64(dExt.windowSize.Width) * dExt.scale),
int(action.Scope[1] * float64(dExt.windowSize.Height) * dExt.scale),
int(action.Scope[2] * float64(dExt.windowSize.Width) * dExt.scale),
int(action.Scope[3] * float64(dExt.windowSize.Height) * dExt.scale),
}),
)
var point PointF var point PointF
findText := func(d *DriverExt) error { findText := func(d *DriverExt) error {
var err error var err error
point, err = d.GetTextXY(text, action.RecognitionArea, action.Index) point, err = d.GetTextXY(text, options...)
return err return err
} }
foundTextAction := func(d *DriverExt) error { foundTextAction := func(d *DriverExt) error {
@@ -444,10 +474,24 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
action.Params = textList action.Params = textList
} }
if texts, ok := action.Params.([]string); ok { if texts, ok := action.Params.([]string); ok {
if len(action.Scope) != 4 {
action.Scope = []float64{0, 0, 1, 1}
}
var options []DataOption
options = append(options,
WithCustomOption("scope", []int{
int(action.Scope[0] * float64(dExt.windowSize.Width) * dExt.scale),
int(action.Scope[1] * float64(dExt.windowSize.Height) * dExt.scale),
int(action.Scope[2] * float64(dExt.windowSize.Width) * dExt.scale),
int(action.Scope[3] * float64(dExt.windowSize.Height) * dExt.scale),
}),
)
var point PointF var point PointF
findText := func(d *DriverExt) error { findText := func(d *DriverExt) error {
var err error var err error
points, err := d.GetTextXYs(texts, action.RecognitionArea) points, err := d.GetTextXYs(texts, options...)
if err != nil { if err != nil {
return err return err
} }
@@ -519,7 +563,21 @@ func (dExt *DriverExt) DoAction(action MobileAction) error {
return fmt.Errorf("invalid %s params: %v", ACTION_Tap, action.Params) return fmt.Errorf("invalid %s params: %v", ACTION_Tap, action.Params)
case ACTION_TapByOCR: case ACTION_TapByOCR:
if ocrText, ok := action.Params.(string); ok { if ocrText, ok := action.Params.(string); ok {
return dExt.TapByOCR(ocrText, action.Identifier, action.IgnoreNotFoundError, action.RecognitionArea, action.Index) if len(action.Scope) != 4 {
action.Scope = []float64{0, 0, 1, 1}
}
var options []DataOption
options = append(options,
WithCustomOption("index", []int{action.Index}),
WithCustomOption("scope", []int{
int(action.Scope[0] * float64(dExt.windowSize.Width) * dExt.scale),
int(action.Scope[1] * float64(dExt.windowSize.Height) * dExt.scale),
int(action.Scope[2] * float64(dExt.windowSize.Width) * dExt.scale),
int(action.Scope[3] * float64(dExt.windowSize.Height) * dExt.scale),
}),
)
return dExt.TapByOCR(ocrText, action.Identifier, action.IgnoreNotFoundError, options...)
} }
return fmt.Errorf("invalid %s params: %v", ACTION_TapByOCR, action.Params) return fmt.Errorf("invalid %s params: %v", ACTION_TapByOCR, action.Params)
case ACTION_TapByCV: case ACTION_TapByCV:
+40 -59
View File
@@ -110,9 +110,29 @@ func getLogID(header http.Header) string {
return logID[0] return logID[0]
} }
func (s *veDEMOCRService) FindText(text string, imageBuf []byte, recAbsArea []int, index ...int) (rect image.Rectangle, err error) { func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...DataOption) (rect image.Rectangle, err error) {
if len(index) == 0 { data := map[string]interface{}{}
index = []int{0} // index not specified for _, option := range options {
option(data)
}
if _, ok := data["index"]; !ok {
data["index"] = []int{0} // index not specified
}
index, ok := data["index"].([]int)
if !ok || len(index) == 0 {
index = []int{0}
}
_, ok = data["scope"]
if !ok {
data["scope"] = []int{0, 0, math.MaxInt64, math.MaxInt64} // scope not specified
}
scope, ok := data["scope"].([]int)
if !ok || len(scope) != 4 {
scope = []int{0, 0, math.MaxInt64, math.MaxInt64}
} }
ocrResults, err := s.getOCRResult(imageBuf) ocrResults, err := s.getOCRResult(imageBuf)
@@ -121,22 +141,6 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, recAbsArea []in
return return
} }
if len(recAbsArea) != 4 {
recAbsArea = []int{0, 0, math.MaxInt64, math.MaxInt64}
}
var minX, minY, maxX, maxY int
if recAbsArea[0] < recAbsArea[2] {
minX, maxX = recAbsArea[0], recAbsArea[2]
} else {
minX, maxX = recAbsArea[2], recAbsArea[0]
}
if recAbsArea[1] < recAbsArea[3] {
minY, maxY = recAbsArea[1], recAbsArea[3]
} else {
minY, maxY = recAbsArea[3], recAbsArea[1]
}
var rects []image.Rectangle var rects []image.Rectangle
var ocrTexts []string var ocrTexts []string
for _, ocrResult := range ocrResults { for _, ocrResult := range ocrResults {
@@ -151,7 +155,7 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, recAbsArea []in
Y: int(ocrResult.Points[2].Y), Y: int(ocrResult.Points[2].Y),
}, },
} }
if rect.Min.X > minX && rect.Max.X < maxX && rect.Min.Y < maxY && rect.Max.Y > minY { if rect.Min.X > scope[0] && rect.Max.X < scope[2] && rect.Min.Y > scope[1] && rect.Max.Y < scope[3] {
ocrTexts = append(ocrTexts, ocrResult.Text) ocrTexts = append(ocrTexts, ocrResult.Text)
// not contains text // not contains text
@@ -196,27 +200,26 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, recAbsArea []in
return rects[idx], nil return rects[idx], nil
} }
func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte, recAbsArea []int) (rects []image.Rectangle, err error) { func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte, options ...DataOption) (rects []image.Rectangle, err error) {
ocrResults, err := s.getOCRResult(imageBuf) ocrResults, err := s.getOCRResult(imageBuf)
if err != nil { if err != nil {
log.Error().Err(err).Msg("getOCRResult failed") log.Error().Err(err).Msg("getOCRResult failed")
return return
} }
if len(recAbsArea) != 4 { data := map[string]interface{}{}
recAbsArea = []int{0, 0, math.MaxInt64, math.MaxInt64} for _, option := range options {
option(data)
} }
var minX, minY, maxX, maxY int _, ok := data["scope"]
if recAbsArea[0] < recAbsArea[2] { if !ok {
minX, maxX = recAbsArea[0], recAbsArea[2] data["scope"] = []int{0, 0, math.MaxInt64, math.MaxInt64} // scope not specified
} else {
minX, maxX = recAbsArea[2], recAbsArea[0]
} }
if recAbsArea[1] < recAbsArea[3] {
minY, maxY = recAbsArea[1], recAbsArea[3] scope, ok := data["scope"].([]int)
} else { if !ok || len(scope) != 4 {
minY, maxY = recAbsArea[3], recAbsArea[1] scope = []int{0, 0, math.MaxInt64, math.MaxInt64}
} }
var success bool var success bool
@@ -237,7 +240,7 @@ func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte, recAbsArea
}, },
} }
if rect.Min.X > minX && rect.Max.X < maxX && rect.Min.Y < maxY && rect.Max.Y > minY { if rect.Min.X > scope[0] && rect.Max.X < scope[2] && rect.Min.Y > scope[1] && rect.Max.Y < scope[3] {
ocrTexts = append(ocrTexts, ocrResult.Text) ocrTexts = append(ocrTexts, ocrResult.Text)
// not contains text // not contains text
@@ -268,26 +271,15 @@ type OCRService interface {
FindText(text string, imageBuf []byte, index ...int) (rect image.Rectangle, err error) FindText(text string, imageBuf []byte, index ...int) (rect image.Rectangle, err error)
} }
func (dExt *DriverExt) FindTextByOCR(ocrText string, recognitionArea []float64, index ...int) (x, y, width, height float64, err error) { func (dExt *DriverExt) FindTextByOCR(ocrText string, options ...DataOption) (x, y, width, height float64, err error) {
var bufSource *bytes.Buffer var bufSource *bytes.Buffer
if bufSource, err = dExt.takeScreenShot(); err != nil { if bufSource, err = dExt.takeScreenShot(); err != nil {
err = fmt.Errorf("takeScreenShot error: %v", err) err = fmt.Errorf("takeScreenShot error: %v", err)
return return
} }
if len(recognitionArea) != 4 {
recognitionArea = []float64{0, 0, 1, 1}
}
absArea := []int{
int(recognitionArea[0] * float64(dExt.windowSize.Width) * dExt.scale),
int(recognitionArea[1] * float64(dExt.windowSize.Height) * dExt.scale),
int(recognitionArea[2] * float64(dExt.windowSize.Width) * dExt.scale),
int(recognitionArea[3] * float64(dExt.windowSize.Height) * dExt.scale),
}
service := &veDEMOCRService{} service := &veDEMOCRService{}
rect, err := service.FindText(ocrText, bufSource.Bytes(), absArea, index...) rect, err := service.FindText(ocrText, bufSource.Bytes(), options...)
if err != nil { if err != nil {
log.Warn().Msgf("FindText failed: %s", err.Error()) log.Warn().Msgf("FindText failed: %s", err.Error())
err = fmt.Errorf("FindText failed: %v", err) err = fmt.Errorf("FindText failed: %v", err)
@@ -300,26 +292,15 @@ func (dExt *DriverExt) FindTextByOCR(ocrText string, recognitionArea []float64,
return return
} }
func (dExt *DriverExt) FindTextsByOCR(ocrTexts []string, recognitionArea []float64) (points [][]float64, err error) { func (dExt *DriverExt) FindTextsByOCR(ocrTexts []string, options ...DataOption) (points [][]float64, err error) {
var bufSource *bytes.Buffer var bufSource *bytes.Buffer
if bufSource, err = dExt.takeScreenShot(); err != nil { if bufSource, err = dExt.takeScreenShot(); err != nil {
err = fmt.Errorf("takeScreenShot error: %v", err) err = fmt.Errorf("takeScreenShot error: %v", err)
return return
} }
if len(recognitionArea) != 4 {
recognitionArea = []float64{0, 0, 1, 1}
}
absArea := []int{
int(recognitionArea[0] * float64(dExt.windowSize.Width) * dExt.scale),
int(recognitionArea[1] * float64(dExt.windowSize.Height) * dExt.scale),
int(recognitionArea[2] * float64(dExt.windowSize.Width) * dExt.scale),
int(recognitionArea[3] * float64(dExt.windowSize.Height) * dExt.scale),
}
service := &veDEMOCRService{} service := &veDEMOCRService{}
rects, err := service.FindTexts(ocrTexts, bufSource.Bytes(), absArea) rects, err := service.FindTexts(ocrTexts, bufSource.Bytes(), options...)
if err != nil { if err != nil {
log.Warn().Msgf("FindTexts failed: %s", err.Error()) log.Warn().Msgf("FindTexts failed: %s", err.Error())
err = fmt.Errorf("FindTexts failed: %v", err) err = fmt.Errorf("FindTexts failed: %v", err)
+6 -6
View File
@@ -28,8 +28,8 @@ func (dExt *DriverExt) TapXY(x, y float64, identifier string) error {
return dExt.TapAbsXY(x, y, identifier) return dExt.TapAbsXY(x, y, identifier)
} }
func (dExt *DriverExt) GetTextXY(ocrText string, recognitionArea []float64, index ...int) (point PointF, err error) { func (dExt *DriverExt) GetTextXY(ocrText string, options ...DataOption) (point PointF, err error) {
x, y, width, height, err := dExt.FindTextByOCR(ocrText, recognitionArea, index...) x, y, width, height, err := dExt.FindTextByOCR(ocrText, options...)
if err != nil { if err != nil {
return PointF{}, err return PointF{}, err
} }
@@ -41,8 +41,8 @@ func (dExt *DriverExt) GetTextXY(ocrText string, recognitionArea []float64, inde
return point, nil return point, nil
} }
func (dExt *DriverExt) GetTextXYs(ocrText []string, recognitionArea []float64) (points []PointF, err error) { func (dExt *DriverExt) GetTextXYs(ocrText []string, options ...DataOption) (points []PointF, err error) {
ps, err := dExt.FindTextsByOCR(ocrText, recognitionArea) ps, err := dExt.FindTextsByOCR(ocrText, options...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -71,8 +71,8 @@ func (dExt *DriverExt) GetImageXY(imagePath string, index ...int) (point PointF,
return point, nil return point, nil
} }
func (dExt *DriverExt) TapByOCR(ocrText string, identifier string, ignoreNotFoundError bool, recognitionArea []float64, index ...int) error { func (dExt *DriverExt) TapByOCR(ocrText string, identifier string, ignoreNotFoundError bool, options ...DataOption) error {
point, err := dExt.GetTextXY(ocrText, recognitionArea, index...) point, err := dExt.GetTextXY(ocrText, options...)
if err != nil { if err != nil {
if ignoreNotFoundError { if ignoreNotFoundError {
return nil return nil
+1 -1
View File
@@ -31,7 +31,7 @@ var (
WithDescription = uixt.WithDescription WithDescription = uixt.WithDescription
WithDirection = uixt.WithDirection WithDirection = uixt.WithDirection
WithCustomDirection = uixt.WithCustomDirection WithCustomDirection = uixt.WithCustomDirection
WithRecognitionArea = uixt.WithRecognitionArea WithScope = uixt.WithScope
) )
var ( var (