mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-07 05:32:43 +08:00
refactor: add DriverSession, move step cache to Driver session
This commit is contained in:
1
go.mod
1
go.mod
@@ -26,7 +26,6 @@ require (
|
||||
github.com/shirou/gopsutil v3.21.11+incompatible
|
||||
github.com/spf13/cobra v1.5.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/oauth2 v0.8.0
|
||||
golang.org/x/text v0.15.0
|
||||
|
||||
4
go.sum
4
go.sum
@@ -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/httprunner/funplugin v0.5.5 h1:VU1a6kj1AsJ/ucIhhI5NLHXOP4xnW2JGgk50vBV3Zis=
|
||||
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/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||
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/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
|
||||
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/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
||||
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.3/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.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
|
||||
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/httprunner/funplugin/myexec"
|
||||
@@ -56,7 +56,7 @@ var convertCmd = &cobra.Command{
|
||||
var files []string
|
||||
for _, arg := range args {
|
||||
if builtin.IsFolderPathExists(arg) {
|
||||
fs, err := ioutil.ReadDir(arg)
|
||||
fs, err := os.ReadDir(arg)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("path", arg).Msg("read dir failed")
|
||||
continue
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -648,13 +648,11 @@ func runStepMobileUI(s *SessionRunner, step *TStep) (stepResult *StepResult, err
|
||||
}
|
||||
|
||||
// save attachments
|
||||
cacheData := uiDriver.DataCache.GetAll()
|
||||
for key, value := range cacheData {
|
||||
session := uiDriver.Driver.GetSession()
|
||||
for key, value := range session.GetAll() {
|
||||
attachments[key] = value
|
||||
}
|
||||
uiDriver.DataCache.Clear() // clear step cache
|
||||
|
||||
attachments["driver_request_results"] = uiDriver.Driver.GetDriverResults()
|
||||
session.Clear() // clear step cache
|
||||
stepResult.Attachments = attachments
|
||||
}()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user