From ab8b5b133fa844a9d09dcd92f11269141f63ea70 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Thu, 4 May 2023 13:06:57 +0800 Subject: [PATCH] fix: GetStepCacheData --- .../uitest/demo_android_video_crawler.json | 3 +- .../uitest/demo_android_video_crawler_test.go | 4 +-- hrp/pkg/uixt/ext.go | 32 +++++++++++++++---- hrp/step_mobile_ui.go | 14 ++++---- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/examples/uitest/demo_android_video_crawler.json b/examples/uitest/demo_android_video_crawler.json index 356d62e6..6ad87edd 100644 --- a/examples/uitest/demo_android_video_crawler.json +++ b/examples/uitest/demo_android_video_crawler.json @@ -38,7 +38,7 @@ 1, 1 ], - "target": 2, + "target": 1, "text": "^广告$" }, { @@ -49,6 +49,7 @@ 1, 1 ], + "target": 1, "text": "^图文$" }, { diff --git a/examples/uitest/demo_android_video_crawler_test.go b/examples/uitest/demo_android_video_crawler_test.go index 4cda04b3..2d6aaa45 100644 --- a/examples/uitest/demo_android_video_crawler_test.go +++ b/examples/uitest/demo_android_video_crawler_test.go @@ -25,8 +25,8 @@ func TestAndroidVideoCrawlerTest(t *testing.T) { "feed": map[string]interface{}{ "target_count": 5, "target_labels": []map[string]interface{}{ - {"text": "^广告$", "scope": []float64{0, 0.5, 1, 1}, "regex": true, "target": 2}, - {"text": "^图文$", "scope": []float64{0, 0.5, 1, 1}, "regex": true}, + {"text": "^广告$", "scope": []float64{0, 0.5, 1, 1}, "regex": true, "target": 1}, + {"text": "^图文$", "scope": []float64{0, 0.5, 1, 1}, "regex": true, "target": 1}, {"text": `^特效\|`, "scope": []float64{0, 0.5, 1, 1}, "regex": true}, {"text": `^模板\|`, "scope": []float64{0, 0.5, 1, 1}, "regex": true}, {"text": `^购物\|`, "scope": []float64{0, 0.5, 1, 1}, "regex": true}, diff --git a/hrp/pkg/uixt/ext.go b/hrp/pkg/uixt/ext.go index fec1690e..d7dd00d5 100644 --- a/hrp/pkg/uixt/ext.go +++ b/hrp/pkg/uixt/ext.go @@ -2,6 +2,7 @@ package uixt import ( "bytes" + "encoding/json" "fmt" "image" "image/gif" @@ -61,6 +62,12 @@ type cacheStepData struct { VideoStat *VideoStat } +func (d *cacheStepData) reset() { + d.ScreenShots = make([]string, 0) + d.OcrResults = make(map[string]*OcrResult) + d.VideoStat = nil +} + type DriverExt struct { CVArgs Device Device @@ -188,14 +195,25 @@ func (dExt *DriverExt) saveScreenShot(raw *bytes.Buffer, fileName string) (strin return screenshotPath, nil } -func (dExt *DriverExt) GetStepCacheData() cacheStepData { - copied := dExt.cacheStepData - // clear cache - dExt.cacheStepData = cacheStepData{ - ScreenShots: []string{}, - OcrResults: make(map[string]*OcrResult), +func (dExt *DriverExt) GetStepCacheData() map[string]interface{} { + cacheData := make(map[string]interface{}) + cacheData["video_stat"] = dExt.cacheStepData.VideoStat + cacheData["screenshots"] = dExt.cacheStepData.ScreenShots + + ocrResults := make(map[string]interface{}) + for imagePath, ocrResult := range dExt.cacheStepData.OcrResults { + o, _ := json.Marshal(ocrResult.Texts) + data := map[string]interface{}{ + "tags": ocrResult.Tags, + "texts": string(o), + } + ocrResults[imagePath] = data } - return copied + cacheData["ocr_results"] = ocrResults + + // clear cache + dExt.cacheStepData.reset() + return cacheData } // isPathExists returns true if path exists, whether path is file or dir diff --git a/hrp/step_mobile_ui.go b/hrp/step_mobile_ui.go index 850330a0..d947baca 100644 --- a/hrp/step_mobile_ui.go +++ b/hrp/step_mobile_ui.go @@ -592,10 +592,10 @@ func runStepMobileUI(s *SessionRunner, step *TStep) (stepResult *StepResult, err // check if app is in foreground packageName := uiDriver.Driver.GetLastLaunchedApp() - err := uiDriver.Driver.AssertAppForeground(packageName) - if packageName != "" && err != nil { - log.Error().Err(err).Str("packageName", packageName).Msg("app is not in foreground") - err = errors.Wrap(code.MobileUIAppNotInForegroundError, err.Error()) + err2 := uiDriver.Driver.AssertAppForeground(packageName) + if packageName != "" && err2 != nil { + log.Error().Err(err2).Str("packageName", packageName).Msg("app is not in foreground") + err = errors.Wrap(code.MobileUIAppNotInForegroundError, err2.Error()) } } @@ -608,9 +608,9 @@ func runStepMobileUI(s *SessionRunner, step *TStep) (stepResult *StepResult, err // save attachments cacheData := uiDriver.GetStepCacheData() - attachments["screenshots"] = cacheData.ScreenShots - attachments["ocr_results"] = cacheData.OcrResults - attachments["video_stat"] = cacheData.VideoStat + for key, value := range cacheData { + attachments[key] = value + } stepResult.Attachments = attachments }()