refactor: add DriverSession, move step cache to Driver session

This commit is contained in:
lilong.129
2024-09-02 12:16:06 +08:00
parent 5f61858b4d
commit 4c71fc02f9
13 changed files with 85 additions and 88 deletions
-1
View File
@@ -26,7 +26,6 @@ require (
github.com/shirou/gopsutil v3.21.11+incompatible github.com/shirou/gopsutil v3.21.11+incompatible
github.com/spf13/cobra v1.5.0 github.com/spf13/cobra v1.5.0
github.com/stretchr/testify v1.9.0 github.com/stretchr/testify v1.9.0
gocv.io/x/gocv v0.32.1
golang.org/x/net v0.25.0 golang.org/x/net v0.25.0
golang.org/x/oauth2 v0.8.0 golang.org/x/oauth2 v0.8.0
golang.org/x/text v0.15.0 golang.org/x/text v0.15.0
-4
View File
@@ -197,7 +197,6 @@ github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
github.com/httprunner/funplugin v0.5.5 h1:VU1a6kj1AsJ/ucIhhI5NLHXOP4xnW2JGgk50vBV3Zis= github.com/httprunner/funplugin v0.5.5 h1:VU1a6kj1AsJ/ucIhhI5NLHXOP4xnW2JGgk50vBV3Zis=
github.com/httprunner/funplugin v0.5.5/go.mod h1:YZzBBSOSdLZEpHZz0P2E5SOQ+o1+Fbn30oWS4RGHBz0= github.com/httprunner/funplugin v0.5.5/go.mod h1:YZzBBSOSdLZEpHZz0P2E5SOQ+o1+Fbn30oWS4RGHBz0=
github.com/hybridgroup/mjpeg v0.0.0-20140228234708-4680f319790e/go.mod h1:eagM805MRKrioHYuU7iKLUyFPVKqVV6um5DAvCkUtXs=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
@@ -280,7 +279,6 @@ github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU= github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
@@ -375,8 +373,6 @@ go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
gocv.io/x/gocv v0.32.1 h1:BC9hHs5+47nVgySUFVKntc6RsF3SULFzqk6OV9xz+C0=
gocv.io/x/gocv v0.32.1/go.mod h1:oc6FvfYqfBp99p+yOEzs9tbYF9gOrAQSeL/dyIPefJU=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc= golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
+2 -2
View File
@@ -1,7 +1,7 @@
package cmd package cmd
import ( import (
"io/ioutil" "os"
"path/filepath" "path/filepath"
"github.com/httprunner/funplugin/myexec" "github.com/httprunner/funplugin/myexec"
@@ -56,7 +56,7 @@ var convertCmd = &cobra.Command{
var files []string var files []string
for _, arg := range args { for _, arg := range args {
if builtin.IsFolderPathExists(arg) { if builtin.IsFolderPathExists(arg) {
fs, err := ioutil.ReadDir(arg) fs, err := os.ReadDir(arg)
if err != nil { if err != nil {
log.Error().Err(err).Str("path", arg).Msg("read dir failed") log.Error().Err(err).Str("path", arg).Msg("read dir failed")
continue continue
+2 -2
View File
@@ -727,8 +727,8 @@ func (ad *adbDriver) StopCaptureLog() (result interface{}, err error) {
return pointRes, nil return pointRes, nil
} }
func (ad *adbDriver) GetDriverResults() []*DriverResult { func (ad *adbDriver) GetSession() *DriverSession {
return nil return ad.Driver.session
} }
func (ad *adbDriver) GetForegroundApp() (app AppInfo, err error) { func (ad *adbDriver) GetForegroundApp() (app AppInfo, err error) {
-7
View File
@@ -654,10 +654,3 @@ func (ud *uiaDriver) TapByTexts(actions ...TapTextAction) error {
} }
return nil return nil
} }
func (ud *uiaDriver) GetDriverResults() []*DriverResult {
defer func() {
ud.Driver.driverResults = nil
}()
return ud.Driver.driverResults
}
+60 -8
View File
@@ -5,7 +5,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"io" "io"
"io/ioutil"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@@ -16,12 +15,65 @@ import (
"github.com/rs/zerolog/log" "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 { type Driver struct {
urlPrefix *url.URL urlPrefix *url.URL
sessionId string sessionId string
client *http.Client client *http.Client
scale float64 scale float64
driverResults []*DriverResult // cache session data
session *DriverSession
} }
type DriverResult struct { type DriverResult struct {
@@ -76,7 +128,7 @@ func (wd *Driver) httpRequest(method string, rawURL string, rawBody []byte) (raw
} }
defer func() { defer func() {
// https://github.com/etcd-io/etcd/blob/v3.3.25/pkg/httputil/httputil.go#L16-L22 // 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() _ = resp.Body.Close()
}() }()
@@ -88,7 +140,7 @@ func (wd *Driver) httpRequest(method string, rawURL string, rawBody []byte) (raw
RequestDuration: duration, RequestDuration: duration,
RequestTime: time.Now(), RequestTime: time.Now(),
} }
wd.driverResults = append(wd.driverResults, driverResult) wd.session.addRequestResult(driverResult)
logger := log.Debug().Int("statusCode", resp.StatusCode).Str("duration", duration.String()) logger := log.Debug().Int("statusCode", resp.StatusCode).Str("duration", duration.String())
if !strings.HasSuffix(rawURL, "screenshot") { if !strings.HasSuffix(rawURL, "screenshot") {
// avoid printing screenshot data // avoid printing screenshot data
+9 -50
View File
@@ -20,59 +20,19 @@ import (
"github.com/httprunner/httprunner/v4/hrp/internal/env" "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 { type DriverExt struct {
Device Device Device Device
Driver WebDriver Driver WebDriver
WindowSize Size ImageService IImageService // used to extract image data
frame *bytes.Buffer
doneMjpegStream chan bool
ImageService IImageService // used to extract image data
interruptSignal chan os.Signal
// cache step data WindowSize Size
DataCache dataCache
// funplugin // funplugin
plugin funplugin.IPlugin 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) { 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) option(driverOptions)
} }
driver.GetSession().Clear()
dExt = &DriverExt{ dExt = &DriverExt{
Device: device, Device: device,
Driver: driver, Driver: driver,
plugin: driverOptions.plugin, plugin: driverOptions.plugin,
DataCache: dataCache{},
interruptSignal: make(chan os.Signal, 1), interruptSignal: make(chan os.Signal, 1),
} }
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)
+3 -1
View File
@@ -507,6 +507,9 @@ type WebDriver interface {
// 2) testedApplicationBundleId terminate // 2) testedApplicationBundleId terminate
DeleteSession() error DeleteSession() error
// GetSession returns session cache, including requests, screenshots, etc.
GetSession() *DriverSession
Status() (DeviceStatus, error) Status() (DeviceStatus, error)
DeviceInfo() (DeviceInfo, error) DeviceInfo() (DeviceInfo, error)
@@ -635,5 +638,4 @@ type WebDriver interface {
// triggers the log capture and returns the log entries // triggers the log capture and returns the log entries
StartCaptureLog(identifier ...string) (err error) StartCaptureLog(identifier ...string) (err error)
StopCaptureLog() (result interface{}, err error) StopCaptureLog() (result interface{}, err error)
GetDriverResults() []*DriverResult
} }
+2 -5
View File
@@ -897,11 +897,8 @@ func (wd *wdaDriver) StopCaptureLog() (result interface{}, err error) {
return reply.Value, nil return reply.Value, nil
} }
func (wd *wdaDriver) GetDriverResults() []*DriverResult { func (ud *wdaDriver) GetSession() *DriverSession {
defer func() { return ud.Driver.session
wd.Driver.driverResults = nil
}()
return wd.Driver.driverResults
} }
type rawResponse []byte type rawResponse []byte
+1 -1
View File
@@ -300,7 +300,7 @@ func Test_remoteWD_SetPasteboard(t *testing.T) {
// err := driver.SetPasteboard(PasteboardTypePlaintext, "gwda") // err := driver.SetPasteboard(PasteboardTypePlaintext, "gwda")
err := driver.SetPasteboard(PasteboardTypeUrl, "Clock-stopwatch://") err := driver.SetPasteboard(PasteboardTypeUrl, "Clock-stopwatch://")
// userHomeDir, _ := os.UserHomeDir() // 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)) // err := driver.SetPasteboard(PasteboardTypeImage, string(bytesImg))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
+2 -1
View File
@@ -48,7 +48,8 @@ func (dExt *DriverExt) CollectEndToEndDelay(options ...ActionOption) {
endToEndDelay.Start() endToEndDelay.Start()
dExt.DataCache.e2eDelay = endToEndDelay.Timelines // TODO: remove
dExt.Driver.GetSession().e2eDelay = endToEndDelay.Timelines
} }
func (ete *EndToEndDelay) getCurrentLiveTime(utcTime time.Time) error { func (ete *EndToEndDelay) getCurrentLiveTime(utcTime time.Time) error {
+1 -1
View File
@@ -51,7 +51,7 @@ func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *S
Resolution: dExt.WindowSize, Resolution: dExt.WindowSize,
} }
// cache screen result // cache screen result
dExt.DataCache.addScreenResult(screenResult) dExt.Driver.GetSession().addScreenResult(screenResult)
imageResult, err := dExt.ImageService.GetImage(bufSource, options...) imageResult, err := dExt.ImageService.GetImage(bufSource, options...)
if err != nil { if err != nil {
+3 -5
View File
@@ -648,13 +648,11 @@ func runStepMobileUI(s *SessionRunner, step *TStep) (stepResult *StepResult, err
} }
// save attachments // save attachments
cacheData := uiDriver.DataCache.GetAll() session := uiDriver.Driver.GetSession()
for key, value := range cacheData { for key, value := range session.GetAll() {
attachments[key] = value attachments[key] = value
} }
uiDriver.DataCache.Clear() // clear step cache session.Clear() // clear step cache
attachments["driver_request_results"] = uiDriver.Driver.GetDriverResults()
stepResult.Attachments = attachments stepResult.Attachments = attachments
}() }()