mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-07 06:22:43 +08:00
feat: driver DataCache
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user