mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-08 00:47:36 +08:00
refactor: IImageService.GetImage options reuse ActionOptions
This commit is contained in:
@@ -208,22 +208,22 @@ func (o *ActionOptions) Options() []ActionOption {
|
|||||||
return options
|
return options
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *ActionOptions) screenshotActionOptions() screenshotActionOptions {
|
func (o *ActionOptions) screenshotActions() []string {
|
||||||
options := screenshotActionOptions{}
|
actions := []string{}
|
||||||
if o.ScreenShotWithOCR {
|
if o.ScreenShotWithOCR {
|
||||||
options = append(options, "ocr")
|
actions = append(actions, "ocr")
|
||||||
}
|
}
|
||||||
if o.ScreenShotWithUpload {
|
if o.ScreenShotWithUpload {
|
||||||
options = append(options, "upload")
|
actions = append(actions, "upload")
|
||||||
}
|
}
|
||||||
if o.ScreenShotWithLiveType {
|
if o.ScreenShotWithLiveType {
|
||||||
options = append(options, "liveType")
|
actions = append(actions, "liveType")
|
||||||
}
|
}
|
||||||
// UI detection
|
// UI detection
|
||||||
if len(o.ScreenShotWithUITypes) > 0 {
|
if len(o.ScreenShotWithUITypes) > 0 {
|
||||||
options = append(options, "ui")
|
actions = append(actions, "ui")
|
||||||
}
|
}
|
||||||
return options
|
return actions
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewActionOptions(options ...ActionOption) *ActionOptions {
|
func NewActionOptions(options ...ActionOption) *ActionOptions {
|
||||||
|
|||||||
@@ -176,28 +176,24 @@ func newVEDEMImageService() (*veDEMImageService, error) {
|
|||||||
// close - get close popup
|
// close - get close popup
|
||||||
// ui - get ui position by type(s)
|
// ui - get ui position by type(s)
|
||||||
type veDEMImageService struct{}
|
type veDEMImageService struct{}
|
||||||
type (
|
|
||||||
screenshotActionOptions []string
|
|
||||||
screenshotUITypeOptions []string
|
|
||||||
)
|
|
||||||
|
|
||||||
func (s *veDEMImageService) GetImage(imageBuf *bytes.Buffer, options ...interface{}) (imageResult ImageResult, err error) {
|
func (s *veDEMImageService) GetImage(imageBuf *bytes.Buffer, options ...ActionOption) (imageResult *ImageResult, err error) {
|
||||||
|
actionOptions := NewActionOptions(options...)
|
||||||
|
screenshotActions := actionOptions.screenshotActions()
|
||||||
|
if len(screenshotActions) == 0 {
|
||||||
|
// skip
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
bodyBuf := &bytes.Buffer{}
|
bodyBuf := &bytes.Buffer{}
|
||||||
bodyWriter := multipart.NewWriter(bodyBuf)
|
bodyWriter := multipart.NewWriter(bodyBuf)
|
||||||
for _, option := range options {
|
for _, action := range screenshotActions {
|
||||||
switch ov := option.(type) {
|
|
||||||
case screenshotActionOptions:
|
|
||||||
for _, action := range ov {
|
|
||||||
bodyWriter.WriteField("actions", action)
|
bodyWriter.WriteField("actions", action)
|
||||||
}
|
}
|
||||||
case screenshotUITypeOptions:
|
for _, uiType := range actionOptions.ScreenShotWithUITypes {
|
||||||
for _, uiType := range ov {
|
|
||||||
bodyWriter.WriteField("uiTypes", uiType)
|
bodyWriter.WriteField("uiTypes", uiType)
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
log.Warn().Interface("option", ov).Msgf("unexpected image service option")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bodyWriter.WriteField("ocrCluster", "highPrecision")
|
bodyWriter.WriteField("ocrCluster", "highPrecision")
|
||||||
|
|
||||||
formWriter, err := bodyWriter.CreateFormFile("image", "screenshot.png")
|
formWriter, err := bodyWriter.CreateFormFile("image", "screenshot.png")
|
||||||
@@ -304,7 +300,7 @@ func (s *veDEMImageService) GetImage(imageBuf *bytes.Buffer, options ...interfac
|
|||||||
Msg("request veDEM OCR service failed")
|
Msg("request veDEM OCR service failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
imageResult = imageResponse.Result
|
imageResult = &imageResponse.Result
|
||||||
log.Debug().Interface("imageResult", imageResult).Msg("get image data by veDEM")
|
log.Debug().Interface("imageResult", imageResult).Msg("get image data by veDEM")
|
||||||
return imageResult, nil
|
return imageResult, nil
|
||||||
}
|
}
|
||||||
@@ -337,23 +333,13 @@ func getLogID(header http.Header) string {
|
|||||||
|
|
||||||
type IImageService interface {
|
type IImageService interface {
|
||||||
// GetImage returns image result including ocr texts, uploaded image url, etc
|
// GetImage returns image result including ocr texts, uploaded image url, etc
|
||||||
GetImage(imageBuf *bytes.Buffer, options ...interface{}) (imageResult ImageResult, err error)
|
GetImage(imageBuf *bytes.Buffer, options ...ActionOption) (imageResult *ImageResult, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetScreenResult takes a screenshot, returns the image recognization result
|
// 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) {
|
||||||
actionOptions := NewActionOptions(options...)
|
|
||||||
screenActionOptions := actionOptions.screenshotActionOptions()
|
|
||||||
screenUITypeOptions := screenshotUITypeOptions(actionOptions.ScreenShotWithUITypes)
|
|
||||||
|
|
||||||
var fileName string
|
|
||||||
if len(screenActionOptions) == 0 {
|
|
||||||
fileName = builtin.GenNameWithTimestamp("%d_screenshot")
|
|
||||||
} else {
|
|
||||||
fileName = builtin.GenNameWithTimestamp("%d_cv")
|
|
||||||
}
|
|
||||||
|
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
|
fileName := builtin.GenNameWithTimestamp("%d_screenshot")
|
||||||
bufSource, imagePath, err := dExt.takeScreenShot(fileName)
|
bufSource, imagePath, err := dExt.takeScreenShot(fileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -368,13 +354,12 @@ func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *S
|
|||||||
}
|
}
|
||||||
|
|
||||||
var imageUrl string
|
var imageUrl string
|
||||||
if len(screenActionOptions) > 0 {
|
imageResult, err := dExt.ImageService.GetImage(bufSource, options...)
|
||||||
imageResult, err := dExt.ImageService.GetImage(bufSource,
|
|
||||||
screenActionOptions, screenUITypeOptions)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("GetImage from ImageService failed")
|
log.Error().Err(err).Msg("GetImage from ImageService failed")
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if imageResult != nil {
|
||||||
screenResult.ScreenshotCVElapsed = time.Since(startTime).Milliseconds() - screenshotTakeElapsed
|
screenResult.ScreenshotCVElapsed = time.Since(startTime).Milliseconds() - screenshotTakeElapsed
|
||||||
screenResult.Texts = imageResult.OCRResult.ToOCRTexts()
|
screenResult.Texts = imageResult.OCRResult.ToOCRTexts()
|
||||||
|
|
||||||
@@ -530,9 +515,7 @@ func (dExt *DriverExt) GetUIResultMap(uiTypes []string) (uiResultMap UIResultMap
|
|||||||
imagePath := screenResult.imagePath
|
imagePath := screenResult.imagePath
|
||||||
|
|
||||||
imageResult, err := dExt.ImageService.GetImage(bufSource,
|
imageResult, err := dExt.ImageService.GetImage(bufSource,
|
||||||
screenshotActionOptions{"ui"}, // turn on UI type detection
|
WithScreenShotUITypes(uiTypes...)) // turn on UI type detection
|
||||||
screenshotUITypeOptions(uiTypes),
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("GetImage from ImageService failed")
|
log.Error().Err(err).Msg("GetImage from ImageService failed")
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user