feat: GetScreenResult with ScreenShotFileName option

This commit is contained in:
lilong.129
2024-11-13 20:04:04 +08:00
parent f469ddc1d1
commit d8eb10808f
5 changed files with 34 additions and 8 deletions

View File

@@ -136,6 +136,7 @@ type ActionOptions struct {
ScreenShotWithUITypes []string `json:"screenshot_with_ui_types,omitempty" yaml:"screenshot_with_ui_types,omitempty"`
ScreenShotWithClosePopups bool `json:"screenshot_with_close_popups,omitempty" yaml:"screenshot_with_close_popups,omitempty"`
ScreenShotWithOCRCluster string `json:"screenshot_with_ocr_cluster,omitempty" yaml:"screenshot_with_ocr_cluster,omitempty"`
ScreenShotFileName string `json:"screenshot_file_name,omitempty" yaml:"screenshot_file_name,omitempty"`
}
func (o *ActionOptions) Options() []ActionOption {
@@ -251,6 +252,9 @@ func (o *ActionOptions) Options() []ActionOption {
if o.ScreenShotWithOCRCluster != "" {
options = append(options, WithScreenOCRCluster(o.ScreenShotWithOCRCluster))
}
if o.ScreenShotFileName != "" {
options = append(options, WithScreenShotFileName(o.ScreenShotFileName))
}
return options
}
@@ -519,6 +523,12 @@ func WithScreenOCRCluster(ocrCluster string) ActionOption {
}
}
func WithScreenShotFileName(fileName string) ActionOption {
return func(o *ActionOptions) {
o.ScreenShotFileName = fileName
}
}
func (dExt *DriverExt) ParseActionOptions(options ...ActionOption) []ActionOption {
actionOptions := NewActionOptions(options...)

View File

@@ -63,7 +63,10 @@ func (dExt *DriverExt) AutoPopupHandler() error {
// check popup by screenshot
screenResult, err := dExt.GetScreenResult(
WithScreenShotOCR(true), WithScreenShotUpload(true))
WithScreenShotOCR(true),
WithScreenShotUpload(true),
WithScreenShotFileName("check_popup"),
)
if err != nil {
return errors.Wrap(err, "get screen result failed for popup handler")
}
@@ -97,6 +100,7 @@ func (dExt *DriverExt) CheckPopup() (popup *PopupInfo, err error) {
screenResult, err := dExt.GetScreenResult(
WithScreenShotUpload(true),
WithScreenShotClosePopups(true), // get popup area and close area
WithScreenShotFileName("check_popup"),
)
if err != nil {
return nil, errors.Wrap(err, "get screen result failed for popup handler")

View File

@@ -36,16 +36,21 @@ type ScreenResultMap map[string]*ScreenResult // key is date time
// GetScreenResult takes a screenshot, returns the image recognition result
func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *ScreenResult, err error) {
fileName := builtin.GenNameWithTimestamp("%d_screenshot")
actionOptions := NewActionOptions(options...)
screenshotActions := actionOptions.screenshotActions()
if len(screenshotActions) != 0 {
fileName = builtin.GenNameWithTimestamp("%d_" + strings.Join(screenshotActions, "_"))
}
if actionOptions.MaxRetryTimes == 0 {
actionOptions.MaxRetryTimes = 1
}
var fileName string
screenshotActions := actionOptions.screenshotActions()
if actionOptions.ScreenShotFileName != "" {
fileName = builtin.GenNameWithTimestamp("%d_" + actionOptions.ScreenShotFileName)
} else if len(screenshotActions) != 0 {
fileName = builtin.GenNameWithTimestamp("%d_" + strings.Join(screenshotActions, "_"))
} else {
fileName = builtin.GenNameWithTimestamp("%d_screenshot")
}
var bufSource *bytes.Buffer
var imageResult *ImageResult
var imagePath string
@@ -123,7 +128,9 @@ func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *S
func (dExt *DriverExt) GetScreenTexts() (ocrTexts OCRTexts, err error) {
screenResult, err := dExt.GetScreenResult(
WithScreenShotOCR(true), WithScreenShotUpload(true))
WithScreenShotOCR(true), WithScreenShotUpload(true),
WithScreenShotFileName("get_screen_texts"),
)
if err != nil {
return
}
@@ -159,6 +166,7 @@ func (dExt *DriverExt) FindScreenText(text string, options ...ActionOption) (poi
}
func (dExt *DriverExt) FindUIResult(options ...ActionOption) (point PointF, err error) {
options = append(options, WithScreenShotFileName("find_ui_result"))
actionOptions := NewActionOptions(options...)
screenResult, err := dExt.GetScreenResult(options...)

View File

@@ -2,6 +2,7 @@ package uixt
import (
"fmt"
"strings"
"time"
"github.com/pkg/errors"
@@ -151,6 +152,9 @@ func (dExt *DriverExt) swipeToTapTexts(texts []string, options ...ActionOption)
screenResult, err := d.GetScreenResult(
WithScreenShotOCR(true),
WithScreenShotUpload(true),
WithScreenShotFileName(
fmt.Sprintf("swipe_to_tap_texts_%s", strings.Join(texts, "_")),
),
)
if err != nil {
return err