feat: driver DataCache

This commit is contained in:
lilong.129
2024-09-02 00:18:07 +08:00
parent f1e4caeef8
commit 5f61858b4d
4 changed files with 42 additions and 41 deletions
+32 -21
View File
@@ -20,21 +20,45 @@ import (
"github.com/httprunner/httprunner/v4/hrp/internal/env" "github.com/httprunner/httprunner/v4/hrp/internal/env"
) )
type cacheStepData struct { type dataCache struct {
// cache step screenshot paths
screenShots []string
// cache step screenshot ocr results, key is image path, value is ScreenResult // cache step screenshot ocr results, key is image path, value is ScreenResult
screenResults ScreenResultMap screenResults ScreenResultMap
// cache e2e delay // cache e2e delay
e2eDelay []timeLog e2eDelay []timeLog
} }
func (d *cacheStepData) reset() { func (d *dataCache) addScreenResult(screenResult *ScreenResult) {
d.screenShots = make([]string, 0) if screenResult == nil {
return
}
d.screenResults[screenResult.imagePath] = screenResult
}
func (d *dataCache) Clear() {
d.screenResults = make(map[string]*ScreenResult) d.screenResults = make(map[string]*ScreenResult)
d.e2eDelay = nil 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 { type DriverExt struct {
Device Device Device Device
Driver WebDriver Driver WebDriver
@@ -45,7 +69,7 @@ type DriverExt struct {
interruptSignal chan os.Signal interruptSignal chan os.Signal
// cache step data // cache step data
cacheStepData cacheStepData DataCache dataCache
// funplugin // funplugin
plugin funplugin.IPlugin plugin funplugin.IPlugin
@@ -61,11 +85,11 @@ func newDriverExt(device Device, driver WebDriver, options ...DriverOption) (dEx
Device: device, Device: device,
Driver: driver, Driver: driver,
plugin: driverOptions.plugin, plugin: driverOptions.plugin,
cacheStepData: cacheStepData{}, DataCache: dataCache{},
interruptSignal: make(chan os.Signal, 1), interruptSignal: make(chan os.Signal, 1),
} }
dExt.cacheStepData.reset() dExt.DataCache.Clear()
signal.Notify(dExt.interruptSignal, syscall.SIGTERM, syscall.SIGINT) signal.Notify(dExt.interruptSignal, syscall.SIGTERM, syscall.SIGINT)
dExt.doneMjpegStream = make(chan bool, 1) dExt.doneMjpegStream = make(chan bool, 1)
@@ -91,19 +115,6 @@ func newDriverExt(device Device, driver WebDriver, options ...DriverOption) (dEx
return dExt, nil 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 { func (dExt *DriverExt) AssertOCR(text, assert string) bool {
var err error var err error
switch assert { switch assert {
+1 -1
View File
@@ -48,7 +48,7 @@ func (dExt *DriverExt) CollectEndToEndDelay(options ...ActionOption) {
endToEndDelay.Start() endToEndDelay.Start()
dExt.cacheStepData.e2eDelay = endToEndDelay.Timelines dExt.DataCache.e2eDelay = endToEndDelay.Timelines
} }
func (ete *EndToEndDelay) getCurrentLiveTime(utcTime time.Time) error { func (ete *EndToEndDelay) getCurrentLiveTime(utcTime time.Time) error {
+5 -18
View File
@@ -8,7 +8,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@@ -32,19 +31,7 @@ type ScreenResult struct {
type ScreenResultMap map[string]*ScreenResult // key is date time type ScreenResultMap map[string]*ScreenResult // key is date time
// getScreenShotUrls returns screenShotsUrls using imagePath as key and uploaded URL as value // GetScreenResult takes a screenshot, returns the image recognition result
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
func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *ScreenResult, err error) { func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *ScreenResult, err error) {
fileName := builtin.GenNameWithTimestamp("%d_screenshot") fileName := builtin.GenNameWithTimestamp("%d_screenshot")
actionOptions := NewActionOptions(options...) actionOptions := NewActionOptions(options...)
@@ -56,7 +43,6 @@ func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *S
if err != nil { if err != nil {
return return
} }
dExt.cacheStepData.screenShots = append(dExt.cacheStepData.screenShots, imagePath)
screenResult = &ScreenResult{ screenResult = &ScreenResult{
bufSource: bufSource, bufSource: bufSource,
@@ -64,6 +50,8 @@ func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *S
Tags: nil, Tags: nil,
Resolution: dExt.WindowSize, Resolution: dExt.WindowSize,
} }
// cache screen result
dExt.DataCache.addScreenResult(screenResult)
imageResult, err := dExt.ImageService.GetImage(bufSource, options...) imageResult, err := dExt.ImageService.GetImage(bufSource, options...)
if err != nil { if err != nil {
@@ -90,8 +78,6 @@ func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *S
} }
} }
dExt.cacheStepData.screenResults[time.Now().String()] = screenResult
log.Debug(). log.Debug().
Str("imagePath", imagePath). Str("imagePath", imagePath).
Str("imageUrl", screenResult.UploadedURL). 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) { 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) { if !builtin.IsPathExists(search) {
return dExt.FindScreenText(search, options...) return dExt.FindScreenText(search, options...)
} }
// TODO: find image using CV
err = errors.New("ocr text not found") err = errors.New("ocr text not found")
return return
} }
+4 -1
View File
@@ -648,10 +648,13 @@ func runStepMobileUI(s *SessionRunner, step *TStep) (stepResult *StepResult, err
} }
// save attachments // save attachments
cacheData := uiDriver.GetStepCacheData() cacheData := uiDriver.DataCache.GetAll()
for key, value := range cacheData { for key, value := range cacheData {
attachments[key] = value attachments[key] = value
} }
uiDriver.DataCache.Clear() // clear step cache
attachments["driver_request_results"] = uiDriver.Driver.GetDriverResults()
stepResult.Attachments = attachments stepResult.Attachments = attachments
}() }()