refactor: add DriverSession, move step cache to Driver session

This commit is contained in:
lilong.129
2024-09-02 12:14:45 +08:00
parent 5f61858b4d
commit 4c71fc02f9
13 changed files with 85 additions and 88 deletions

View File

@@ -727,8 +727,8 @@ func (ad *adbDriver) StopCaptureLog() (result interface{}, err error) {
return pointRes, nil
}
func (ad *adbDriver) GetDriverResults() []*DriverResult {
return nil
func (ad *adbDriver) GetSession() *DriverSession {
return ad.Driver.session
}
func (ad *adbDriver) GetForegroundApp() (app AppInfo, err error) {

View File

@@ -654,10 +654,3 @@ func (ud *uiaDriver) TapByTexts(actions ...TapTextAction) error {
}
return nil
}
func (ud *uiaDriver) GetDriverResults() []*DriverResult {
defer func() {
ud.Driver.driverResults = nil
}()
return ud.Driver.driverResults
}

View File

@@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
@@ -16,12 +15,65 @@ import (
"github.com/rs/zerolog/log"
)
type DriverSession struct {
// cache device window size
windowSize Size
// cache uia2/wda request and response
requests []*DriverResult
// cache session screenshot ocr results, key is image path, value is ScreenResult
screenResults ScreenResultMap
// cache e2e delay
e2eDelay []timeLog
}
func (d *DriverSession) addScreenResult(screenResult *ScreenResult) {
if screenResult == nil {
return
}
d.screenResults[screenResult.imagePath] = screenResult
}
func (d *DriverSession) addRequestResult(driverResult *DriverResult) {
if driverResult == nil {
return
}
d.requests = append(d.requests, driverResult)
}
func (d *DriverSession) Clear() {
d.screenResults = make(map[string]*ScreenResult)
d.requests = nil
d.e2eDelay = nil
}
func (d *DriverSession) 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,
"requests": d.requests,
"e2e_results": d.e2eDelay,
}
return data
}
type Driver struct {
urlPrefix *url.URL
sessionId string
client *http.Client
scale float64
driverResults []*DriverResult
urlPrefix *url.URL
sessionId string
client *http.Client
scale float64
// cache session data
session *DriverSession
}
type DriverResult struct {
@@ -76,7 +128,7 @@ func (wd *Driver) httpRequest(method string, rawURL string, rawBody []byte) (raw
}
defer func() {
// https://github.com/etcd-io/etcd/blob/v3.3.25/pkg/httputil/httputil.go#L16-L22
_, _ = io.Copy(ioutil.Discard, resp.Body)
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
}()
@@ -88,7 +140,7 @@ func (wd *Driver) httpRequest(method string, rawURL string, rawBody []byte) (raw
RequestDuration: duration,
RequestTime: time.Now(),
}
wd.driverResults = append(wd.driverResults, driverResult)
wd.session.addRequestResult(driverResult)
logger := log.Debug().Int("statusCode", resp.StatusCode).Str("duration", duration.String())
if !strings.HasSuffix(rawURL, "screenshot") {
// avoid printing screenshot data

View File

@@ -20,59 +20,19 @@ import (
"github.com/httprunner/httprunner/v4/hrp/internal/env"
)
type dataCache struct {
// cache step screenshot ocr results, key is image path, value is ScreenResult
screenResults ScreenResultMap
// cache e2e delay
e2eDelay []timeLog
}
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
WindowSize Size
frame *bytes.Buffer
doneMjpegStream chan bool
ImageService IImageService // used to extract image data
interruptSignal chan os.Signal
Device Device
Driver WebDriver
ImageService IImageService // used to extract image data
// cache step data
DataCache dataCache
WindowSize Size
// funplugin
plugin funplugin.IPlugin
frame *bytes.Buffer
doneMjpegStream chan bool
interruptSignal chan os.Signal
}
func newDriverExt(device Device, driver WebDriver, options ...DriverOption) (dExt *DriverExt, err error) {
@@ -81,15 +41,14 @@ func newDriverExt(device Device, driver WebDriver, options ...DriverOption) (dEx
option(driverOptions)
}
driver.GetSession().Clear()
dExt = &DriverExt{
Device: device,
Driver: driver,
plugin: driverOptions.plugin,
DataCache: dataCache{},
interruptSignal: make(chan os.Signal, 1),
}
dExt.DataCache.Clear()
signal.Notify(dExt.interruptSignal, syscall.SIGTERM, syscall.SIGINT)
dExt.doneMjpegStream = make(chan bool, 1)

View File

@@ -507,6 +507,9 @@ type WebDriver interface {
// 2) testedApplicationBundleId terminate
DeleteSession() error
// GetSession returns session cache, including requests, screenshots, etc.
GetSession() *DriverSession
Status() (DeviceStatus, error)
DeviceInfo() (DeviceInfo, error)
@@ -635,5 +638,4 @@ type WebDriver interface {
// triggers the log capture and returns the log entries
StartCaptureLog(identifier ...string) (err error)
StopCaptureLog() (result interface{}, err error)
GetDriverResults() []*DriverResult
}

View File

@@ -897,11 +897,8 @@ func (wd *wdaDriver) StopCaptureLog() (result interface{}, err error) {
return reply.Value, nil
}
func (wd *wdaDriver) GetDriverResults() []*DriverResult {
defer func() {
wd.Driver.driverResults = nil
}()
return wd.Driver.driverResults
func (ud *wdaDriver) GetSession() *DriverSession {
return ud.Driver.session
}
type rawResponse []byte

View File

@@ -300,7 +300,7 @@ func Test_remoteWD_SetPasteboard(t *testing.T) {
// err := driver.SetPasteboard(PasteboardTypePlaintext, "gwda")
err := driver.SetPasteboard(PasteboardTypeUrl, "Clock-stopwatch://")
// userHomeDir, _ := os.UserHomeDir()
// bytesImg, _ := ioutil.ReadFile(userHomeDir + "/Pictures/IMG_0806.jpg")
// bytesImg, _ := os.ReadFile(userHomeDir + "/Pictures/IMG_0806.jpg")
// err := driver.SetPasteboard(PasteboardTypeImage, string(bytesImg))
if err != nil {
t.Fatal(err)

View File

@@ -48,7 +48,8 @@ func (dExt *DriverExt) CollectEndToEndDelay(options ...ActionOption) {
endToEndDelay.Start()
dExt.DataCache.e2eDelay = endToEndDelay.Timelines
// TODO: remove
dExt.Driver.GetSession().e2eDelay = endToEndDelay.Timelines
}
func (ete *EndToEndDelay) getCurrentLiveTime(utcTime time.Time) error {

View File

@@ -51,7 +51,7 @@ func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *S
Resolution: dExt.WindowSize,
}
// cache screen result
dExt.DataCache.addScreenResult(screenResult)
dExt.Driver.GetSession().addScreenResult(screenResult)
imageResult, err := dExt.ImageService.GetImage(bufSource, options...)
if err != nil {