mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-07 15:31:23 +08:00
feat: add option WithScreenShot
This commit is contained in:
@@ -3,7 +3,6 @@
|
|||||||
package demo
|
package demo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -45,30 +44,3 @@ func TestIOSDemo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIOSGetOCRTexts(t *testing.T) {
|
|
||||||
device, err := uixt.NewIOSDevice(
|
|
||||||
uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800),
|
|
||||||
uixt.WithResetHomeOnStartup(false), // not reset home on startup
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
capabilities := uixt.NewCapabilities()
|
|
||||||
capabilities.WithDefaultAlertAction(uixt.AlertActionAccept) // or uixt.AlertActionDismiss
|
|
||||||
driverExt, err := device.NewDriver(capabilities)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
|
||||||
texts, err := driverExt.GetTextsByOCR()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(texts)
|
|
||||||
time.Sleep(3 * time.Second)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -260,7 +260,7 @@ func (dExt *DriverExt) takeScreenShot() (raw *bytes.Buffer, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// saveScreenShot saves image file to $CWD/screenshots/ folder
|
// saveScreenShot saves image file to $CWD/screenshots/ folder
|
||||||
func (dExt *DriverExt) saveScreenShot(raw *bytes.Buffer, fileName string) (string, error) {
|
func saveScreenShot(raw *bytes.Buffer, fileName string) (string, error) {
|
||||||
img, format, err := image.Decode(raw)
|
img, format, err := image.Decode(raw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.Wrap(err, "decode screenshot image failed")
|
return "", errors.Wrap(err, "decode screenshot image failed")
|
||||||
@@ -304,7 +304,7 @@ func (dExt *DriverExt) ScreenShot(fileName string) (string, error) {
|
|||||||
return "", errors.Wrap(err, "screenshot failed")
|
return "", errors.Wrap(err, "screenshot failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
path, err := dExt.saveScreenShot(raw, fileName)
|
path, err := saveScreenShot(raw, fileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.Wrap(err, "save screenshot failed")
|
return "", errors.Wrap(err, "save screenshot failed")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -784,6 +784,7 @@ type DataOptions struct {
|
|||||||
IgnoreNotFoundError bool // ignore error if target element not found
|
IgnoreNotFoundError bool // ignore error if target element not found
|
||||||
MaxRetryTimes int // max retry times if target element not found
|
MaxRetryTimes int // max retry times if target element not found
|
||||||
Interval float64 // interval between retries in seconds
|
Interval float64 // interval between retries in seconds
|
||||||
|
ScreenShotFilename string // turn on screenshot and specify file name
|
||||||
}
|
}
|
||||||
|
|
||||||
type DataOption func(data *DataOptions)
|
type DataOption func(data *DataOptions)
|
||||||
@@ -860,6 +861,16 @@ func WithDataWaitTime(sec float64) DataOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithScreenShot(fileName ...string) DataOption {
|
||||||
|
return func(data *DataOptions) {
|
||||||
|
if len(fileName) > 0 {
|
||||||
|
data.ScreenShotFilename = fileName[0]
|
||||||
|
} else {
|
||||||
|
data.ScreenShotFilename = fmt.Sprintf("screenshot_%d", time.Now().Unix())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func NewDataOptions(options ...DataOption) *DataOptions {
|
func NewDataOptions(options ...DataOption) *DataOptions {
|
||||||
dataOptions := &DataOptions{}
|
dataOptions := &DataOptions{}
|
||||||
for _, option := range options {
|
for _, option := range options {
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ func checkEnv() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *veDEMOCRService) getOCRResult(imageBuf []byte) ([]OCRResult, error) {
|
func (s *veDEMOCRService) getOCRResult(imageBuf *bytes.Buffer) ([]OCRResult, error) {
|
||||||
bodyBuf := &bytes.Buffer{}
|
bodyBuf := &bytes.Buffer{}
|
||||||
bodyWriter := multipart.NewWriter(bodyBuf)
|
bodyWriter := multipart.NewWriter(bodyBuf)
|
||||||
bodyWriter.WriteField("withDet", "true")
|
bodyWriter.WriteField("withDet", "true")
|
||||||
@@ -67,7 +67,7 @@ func (s *veDEMOCRService) getOCRResult(imageBuf []byte) ([]OCRResult, error) {
|
|||||||
return nil, errors.Wrap(code.OCRRequestError,
|
return nil, errors.Wrap(code.OCRRequestError,
|
||||||
fmt.Sprintf("create form file error: %v", err))
|
fmt.Sprintf("create form file error: %v", err))
|
||||||
}
|
}
|
||||||
size, err := formWriter.Write(imageBuf)
|
size, err := formWriter.Write(imageBuf.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(code.OCRRequestError,
|
return nil, errors.Wrap(code.OCRRequestError,
|
||||||
fmt.Sprintf("write form error: %v", err))
|
fmt.Sprintf("write form error: %v", err))
|
||||||
@@ -161,7 +161,7 @@ func (t OCRTexts) Texts() (texts []string) {
|
|||||||
return texts
|
return texts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *veDEMOCRService) GetTexts(imageBuf []byte, options ...DataOption) (
|
func (s *veDEMOCRService) GetTexts(imageBuf *bytes.Buffer, options ...DataOption) (
|
||||||
ocrTexts OCRTexts, err error) {
|
ocrTexts OCRTexts, err error) {
|
||||||
|
|
||||||
ocrResults, err := s.getOCRResult(imageBuf)
|
ocrResults, err := s.getOCRResult(imageBuf)
|
||||||
@@ -172,6 +172,14 @@ func (s *veDEMOCRService) GetTexts(imageBuf []byte, options ...DataOption) (
|
|||||||
|
|
||||||
dataOptions := NewDataOptions(options...)
|
dataOptions := NewDataOptions(options...)
|
||||||
|
|
||||||
|
if dataOptions.ScreenShotFilename != "" {
|
||||||
|
path, err := saveScreenShot(imageBuf, dataOptions.ScreenShotFilename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "save screenshot failed")
|
||||||
|
}
|
||||||
|
log.Info().Str("path", path).Msg("save screenshot")
|
||||||
|
}
|
||||||
|
|
||||||
for _, ocrResult := range ocrResults {
|
for _, ocrResult := range ocrResults {
|
||||||
rect := image.Rectangle{
|
rect := image.Rectangle{
|
||||||
// ocrResult.Points 顺序:左上 -> 右上 -> 右下 -> 左下
|
// ocrResult.Points 顺序:左上 -> 右上 -> 右下 -> 左下
|
||||||
@@ -200,7 +208,7 @@ func (s *veDEMOCRService) GetTexts(imageBuf []byte, options ...DataOption) (
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...DataOption) (
|
func (s *veDEMOCRService) FindText(text string, imageBuf *bytes.Buffer, options ...DataOption) (
|
||||||
rect image.Rectangle, err error) {
|
rect image.Rectangle, err error) {
|
||||||
|
|
||||||
ocrTexts, err := s.GetTexts(imageBuf, options...)
|
ocrTexts, err := s.GetTexts(imageBuf, options...)
|
||||||
@@ -256,7 +264,7 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...Data
|
|||||||
return rects[idx], nil
|
return rects[idx], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte, options ...DataOption) (
|
func (s *veDEMOCRService) FindTexts(texts []string, imageBuf *bytes.Buffer, options ...DataOption) (
|
||||||
rects []image.Rectangle, err error) {
|
rects []image.Rectangle, err error) {
|
||||||
|
|
||||||
ocrTexts, err := s.GetTexts(imageBuf, options...)
|
ocrTexts, err := s.GetTexts(imageBuf, options...)
|
||||||
@@ -295,9 +303,9 @@ func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte, options ...
|
|||||||
}
|
}
|
||||||
|
|
||||||
type OCRService interface {
|
type OCRService interface {
|
||||||
GetTexts(imageBuf []byte, options ...DataOption) (ocrTexts OCRTexts, err error)
|
GetTexts(imageBuf *bytes.Buffer, options ...DataOption) (ocrTexts OCRTexts, err error)
|
||||||
FindText(text string, imageBuf []byte, options ...DataOption) (rect image.Rectangle, err error)
|
FindText(text string, imageBuf *bytes.Buffer, options ...DataOption) (rect image.Rectangle, err error)
|
||||||
FindTexts(texts []string, imageBuf []byte, options ...DataOption) (rects []image.Rectangle, err error)
|
FindTexts(texts []string, imageBuf *bytes.Buffer, options ...DataOption) (rects []image.Rectangle, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dExt *DriverExt) GetTextsByOCR(options ...DataOption) (texts OCRTexts, err error) {
|
func (dExt *DriverExt) GetTextsByOCR(options ...DataOption) (texts OCRTexts, err error) {
|
||||||
@@ -307,7 +315,7 @@ func (dExt *DriverExt) GetTextsByOCR(options ...DataOption) (texts OCRTexts, err
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ocrTexts, err := dExt.ocrService.GetTexts(bufSource.Bytes(), options...)
|
ocrTexts, err := dExt.ocrService.GetTexts(bufSource, options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("GetTexts failed")
|
log.Error().Err(err).Msg("GetTexts failed")
|
||||||
return
|
return
|
||||||
@@ -323,7 +331,7 @@ func (dExt *DriverExt) FindTextByOCR(ocrText string, options ...DataOption) (x,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rect, err := dExt.ocrService.FindText(ocrText, bufSource.Bytes(), options...)
|
rect, err := dExt.ocrService.FindText(ocrText, bufSource, options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn().Msgf("FindText failed: %s", err.Error())
|
log.Warn().Msgf("FindText failed: %s", err.Error())
|
||||||
return
|
return
|
||||||
@@ -342,7 +350,7 @@ func (dExt *DriverExt) FindTextsByOCR(ocrTexts []string, options ...DataOption)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rects, err := dExt.ocrService.FindTexts(ocrTexts, bufSource.Bytes(), options...)
|
rects, err := dExt.ocrService.FindTexts(ocrTexts, bufSource, options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn().Msgf("FindTexts failed: %s", err.Error())
|
log.Warn().Msgf("FindTexts failed: %s", err.Error())
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -3,12 +3,13 @@
|
|||||||
package uixt
|
package uixt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func checkOCR(buff []byte) error {
|
func checkOCR(buff *bytes.Buffer) error {
|
||||||
service, err := newVEDEMOCRService()
|
service, err := newVEDEMOCRService()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -33,19 +34,23 @@ func TestOCRWithScreenshot(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := checkOCR(raw.Bytes()); err != nil {
|
if err := checkOCR(raw); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOCRWithLocalFile(t *testing.T) {
|
func TestOCRWithLocalFile(t *testing.T) {
|
||||||
imagePath := "/Users/debugtalk/Downloads/s1.png"
|
imagePath := "/Users/debugtalk/Downloads/s1.png"
|
||||||
|
|
||||||
file, err := os.ReadFile(imagePath)
|
file, err := os.ReadFile(imagePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := checkOCR(file); err != nil {
|
buf := new(bytes.Buffer)
|
||||||
|
buf.Read(file)
|
||||||
|
|
||||||
|
if err := checkOCR(buf); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user