From 5f61858b4d71667e84a9f248a006e75ca006585a Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Mon, 2 Sep 2024 00:18:07 +0800 Subject: [PATCH] feat: driver DataCache --- hrp/pkg/uixt/ext.go | 53 +++++++++++++++++++++++--------------- hrp/pkg/uixt/live_e2e.go | 2 +- hrp/pkg/uixt/screenshot.go | 23 ++++------------- hrp/step_mobile_ui.go | 5 +++- 4 files changed, 42 insertions(+), 41 deletions(-) diff --git a/hrp/pkg/uixt/ext.go b/hrp/pkg/uixt/ext.go index 2ff92666..a7211edd 100644 --- a/hrp/pkg/uixt/ext.go +++ b/hrp/pkg/uixt/ext.go @@ -20,21 +20,45 @@ import ( "github.com/httprunner/httprunner/v4/hrp/internal/env" ) -type cacheStepData struct { - // cache step screenshot paths - screenShots []string +type dataCache struct { // cache step screenshot ocr results, key is image path, value is ScreenResult screenResults ScreenResultMap // cache e2e delay e2eDelay []timeLog } -func (d *cacheStepData) reset() { - d.screenShots = make([]string, 0) +func (d *dataCache) addScreenResult(screenResult *ScreenResult) { + if screenResult == nil { + return + } + d.screenResults[screenResult.imagePath] = screenResult +} + +func (d *dataCache) Clear() { d.screenResults = make(map[string]*ScreenResult) d.e2eDelay = nil } +func (d *dataCache) GetAll() map[string]interface{} { + screenShots := make([]string, 0) + screenShotsUrls := make(map[string]string) + for _, screenResult := range d.screenResults { + screenShots = append(screenShots, screenResult.imagePath) + if screenResult.UploadedURL == "" { + continue + } + screenShotsUrls[screenResult.imagePath] = screenResult.UploadedURL + } + + data := map[string]interface{}{ + "screenshots": screenShots, + "screenshots_urls": screenShotsUrls, + "screen_results": d.screenResults, + "e2e_results": d.e2eDelay, + } + return data +} + type DriverExt struct { Device Device Driver WebDriver @@ -45,7 +69,7 @@ type DriverExt struct { interruptSignal chan os.Signal // cache step data - cacheStepData cacheStepData + DataCache dataCache // funplugin plugin funplugin.IPlugin @@ -61,11 +85,11 @@ func newDriverExt(device Device, driver WebDriver, options ...DriverOption) (dEx Device: device, Driver: driver, plugin: driverOptions.plugin, - cacheStepData: cacheStepData{}, + DataCache: dataCache{}, interruptSignal: make(chan os.Signal, 1), } - dExt.cacheStepData.reset() + dExt.DataCache.Clear() signal.Notify(dExt.interruptSignal, syscall.SIGTERM, syscall.SIGINT) dExt.doneMjpegStream = make(chan bool, 1) @@ -91,19 +115,6 @@ func newDriverExt(device Device, driver WebDriver, options ...DriverOption) (dEx return dExt, nil } -func (dExt *DriverExt) GetStepCacheData() map[string]interface{} { - cacheData := make(map[string]interface{}) - cacheData["screenshots"] = dExt.cacheStepData.screenShots - - cacheData["screenshots_urls"] = dExt.cacheStepData.screenResults.getScreenShotUrls() - cacheData["screen_results"] = dExt.cacheStepData.screenResults - cacheData["e2e_results"] = dExt.cacheStepData.e2eDelay - cacheData["driver_request_results"] = dExt.Driver.GetDriverResults() - // clear cache - dExt.cacheStepData.reset() - return cacheData -} - func (dExt *DriverExt) AssertOCR(text, assert string) bool { var err error switch assert { diff --git a/hrp/pkg/uixt/live_e2e.go b/hrp/pkg/uixt/live_e2e.go index cda4d72c..d57661cf 100644 --- a/hrp/pkg/uixt/live_e2e.go +++ b/hrp/pkg/uixt/live_e2e.go @@ -48,7 +48,7 @@ func (dExt *DriverExt) CollectEndToEndDelay(options ...ActionOption) { endToEndDelay.Start() - dExt.cacheStepData.e2eDelay = endToEndDelay.Timelines + dExt.DataCache.e2eDelay = endToEndDelay.Timelines } func (ete *EndToEndDelay) getCurrentLiveTime(utcTime time.Time) error { diff --git a/hrp/pkg/uixt/screenshot.go b/hrp/pkg/uixt/screenshot.go index 75fd3eec..44883a0f 100644 --- a/hrp/pkg/uixt/screenshot.go +++ b/hrp/pkg/uixt/screenshot.go @@ -8,7 +8,6 @@ import ( "os" "path/filepath" "strings" - "time" "github.com/pkg/errors" "github.com/rs/zerolog/log" @@ -32,19 +31,7 @@ type ScreenResult struct { type ScreenResultMap map[string]*ScreenResult // key is date time -// getScreenShotUrls returns screenShotsUrls using imagePath as key and uploaded URL as value -func (screenResults ScreenResultMap) getScreenShotUrls() map[string]string { - screenShotsUrls := make(map[string]string) - for _, screenResult := range screenResults { - if screenResult.UploadedURL == "" { - continue - } - screenShotsUrls[screenResult.imagePath] = screenResult.UploadedURL - } - return screenShotsUrls -} - -// GetScreenResult takes a screenshot, returns the image recognization result +// 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...) @@ -56,7 +43,6 @@ func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *S if err != nil { return } - dExt.cacheStepData.screenShots = append(dExt.cacheStepData.screenShots, imagePath) screenResult = &ScreenResult{ bufSource: bufSource, @@ -64,6 +50,8 @@ func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *S Tags: nil, Resolution: dExt.WindowSize, } + // cache screen result + dExt.DataCache.addScreenResult(screenResult) imageResult, err := dExt.ImageService.GetImage(bufSource, options...) if err != nil { @@ -90,8 +78,6 @@ func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *S } } - dExt.cacheStepData.screenResults[time.Now().String()] = screenResult - log.Debug(). Str("imagePath", imagePath). Str("imageUrl", screenResult.UploadedURL). @@ -109,10 +95,11 @@ func (dExt *DriverExt) GetScreenTexts() (ocrTexts OCRTexts, err error) { } func (dExt *DriverExt) FindUIRectInUIKit(search string, options ...ActionOption) (point PointF, err error) { - // click on text, using OCR + // find text using OCR if !builtin.IsPathExists(search) { return dExt.FindScreenText(search, options...) } + // TODO: find image using CV err = errors.New("ocr text not found") return } diff --git a/hrp/step_mobile_ui.go b/hrp/step_mobile_ui.go index 5e1a97da..a596d98a 100644 --- a/hrp/step_mobile_ui.go +++ b/hrp/step_mobile_ui.go @@ -648,10 +648,13 @@ func runStepMobileUI(s *SessionRunner, step *TStep) (stepResult *StepResult, err } // save attachments - cacheData := uiDriver.GetStepCacheData() + cacheData := uiDriver.DataCache.GetAll() for key, value := range cacheData { attachments[key] = value } + uiDriver.DataCache.Clear() // clear step cache + + attachments["driver_request_results"] = uiDriver.Driver.GetDriverResults() stepResult.Attachments = attachments }()