From d8eb10808f9fe6501736c8069bbc30d5e117380d Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Wed, 13 Nov 2024 20:04:04 +0800 Subject: [PATCH] feat: GetScreenResult with ScreenShotFileName option --- hrp/internal/version/VERSION | 2 +- hrp/pkg/uixt/action.go | 10 ++++++++++ hrp/pkg/uixt/popups.go | 6 +++++- hrp/pkg/uixt/screenshot.go | 20 ++++++++++++++------ hrp/pkg/uixt/swipe.go | 4 ++++ 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/hrp/internal/version/VERSION b/hrp/internal/version/VERSION index cc5dfd39..2cb82726 100644 --- a/hrp/internal/version/VERSION +++ b/hrp/internal/version/VERSION @@ -1 +1 @@ -v5.0.0+2411131451 +v5.0.0+2411132025 diff --git a/hrp/pkg/uixt/action.go b/hrp/pkg/uixt/action.go index 5b6a8ebc..9aa15a46 100644 --- a/hrp/pkg/uixt/action.go +++ b/hrp/pkg/uixt/action.go @@ -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...) diff --git a/hrp/pkg/uixt/popups.go b/hrp/pkg/uixt/popups.go index 7b31b373..6e8360de 100644 --- a/hrp/pkg/uixt/popups.go +++ b/hrp/pkg/uixt/popups.go @@ -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") diff --git a/hrp/pkg/uixt/screenshot.go b/hrp/pkg/uixt/screenshot.go index 8528f3c8..2d06ff9b 100644 --- a/hrp/pkg/uixt/screenshot.go +++ b/hrp/pkg/uixt/screenshot.go @@ -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...) diff --git a/hrp/pkg/uixt/swipe.go b/hrp/pkg/uixt/swipe.go index 184b6c6a..886b6e6e 100644 --- a/hrp/pkg/uixt/swipe.go +++ b/hrp/pkg/uixt/swipe.go @@ -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