Merge pull request #1531 from httprunner/wcl

release v4.3.1

- feat: add option WithScreenShot
- feat: run xctest before start ios automation
- feat: run step with specified loop times
- feat: add options for FindTexts
- refactor: move all UI APIs to uixt pkg
- docs: add examples for UI APIs
This commit is contained in:
debugtalk
2022-12-14 00:31:04 +08:00
40 changed files with 1054 additions and 592 deletions

View File

@@ -85,11 +85,13 @@ var listDevicesCmd = &cobra.Command{
}
for _, d := range devices {
deviceByte, _ := json.Marshal(d.Properties())
deviceProperties := d.Properties()
device := &Device{
d: d,
d: d,
UDID: deviceProperties.SerialNumber,
ConnectionType: deviceProperties.ConnectionType,
ConnectionSpeed: deviceProperties.ConnectionSpeed,
}
json.Unmarshal(deviceByte, device)
device.Status = device.GetStatus()
if isDetail {

14
hrp/cmd/ios/ios_test.go Normal file
View File

@@ -0,0 +1,14 @@
//go:build localtest
package ios
import "testing"
func TestGetDevice(t *testing.T) {
device, err := getDevice(udid)
if err != nil {
t.Fatal(err)
}
t.Logf("device: %v", device)
}

View File

@@ -62,12 +62,13 @@ var mountCmd = &cobra.Command{
dmgPath = filepath.Join(developerDiskImageDir, version, "DeveloperDiskImage.dmg")
signaturePath = filepath.Join(developerDiskImageDir, version, "DeveloperDiskImage.dmg.signature")
} else {
log.Error().Str("dir", developerDiskImageDir).Msg("developer disk image not found in directory")
return fmt.Errorf("developer disk image not found")
log.Error().Str("dir", developerDiskImageDir).Msgf(
"developer disk image %s not found in directory", version)
return fmt.Errorf("developer disk image %s not found", version)
}
if err = device.MountDeveloperDiskImage(dmgPath, signaturePath); err != nil {
return fmt.Errorf("mount developer disk image failed: %s", err)
return fmt.Errorf("mount developer disk image %s failed: %s", version, err)
}
log.Info().Msg("mount developer disk image successfully")

View File

@@ -1 +1 @@
v4.3.0
v4.3.1

View File

@@ -5,6 +5,7 @@ import (
"encoding/binary"
"encoding/json"
"fmt"
"log"
"net"
"strconv"
"time"
@@ -32,8 +33,8 @@ type PerfOptions struct {
func defaulPerfOption() *PerfOptions {
return &PerfOptions{
SysCPU: true, // default on
SysMem: true, // default on
SysCPU: false,
SysMem: false,
SysDisk: false,
SysNetwork: false,
gpu: false,
@@ -179,10 +180,13 @@ type perfdSysmontap struct {
func (c *perfdSysmontap) Start() (data <-chan []byte, err error) {
// set config
interval := time.Millisecond * time.Duration(c.options.OutputInterval)
log.Printf("set sysmontap sample interval: %dms\n", c.options.OutputInterval)
config := map[string]interface{}{
"bm": 0,
"cpuUsage": true,
"sampleInterval": time.Second * 1, // 1s
"sampleInterval": interval, // time.Duration
"ur": c.options.OutputInterval, // 输出频率
"procAttrs": c.options.ProcessAttributes, // process performance
"sysAttrs": c.options.SystemAttributes, // system performance
@@ -740,7 +744,7 @@ func (c *perfdGraphicsOpengl) Start() (data <-chan []byte, err error) {
if _, err = c.i.call(
instrumentsServiceGraphicsOpengl,
"setSamplingRate:",
float64(c.options.OutputInterval)/100,
float64(c.options.OutputInterval)/100, // FIXME: unable to set sampling rate, always 1.0
); err != nil {
return nil, err
}

View File

@@ -68,6 +68,8 @@ func GetAndroidDeviceOptions(dev *AndroidDevice) (deviceOptions []AndroidDeviceO
return
}
// uiautomator2 server must be started before
// adb shell am instrument -w io.appium.uiautomator2.server.test/androidx.test.runner.AndroidJUnitRunner
func NewAndroidDevice(options ...AndroidDeviceOption) (device *AndroidDevice, err error) {
deviceList, err := DeviceList()
if err != nil {
@@ -114,7 +116,7 @@ func DeviceList() (devices []gadb.Device, err error) {
type AndroidDevice struct {
d gadb.Device
logcat *DeviceLogcat
logcat *AdbLogcat
SerialNumber string `json:"serial,omitempty" yaml:"serial,omitempty"`
IP string `json:"ip,omitempty" yaml:"ip,omitempty"`
Port int `json:"port,omitempty" yaml:"port,omitempty"`
@@ -201,7 +203,7 @@ func getFreePort() (int, error) {
return l.Addr().(*net.TCPAddr).Port, nil
}
type DeviceLogcat struct {
type AdbLogcat struct {
serial string
logBuffer *bytes.Buffer
errs []error
@@ -210,8 +212,8 @@ type DeviceLogcat struct {
cmd *exec.Cmd
}
func NewAdbLogcat(serial string) *DeviceLogcat {
return &DeviceLogcat{
func NewAdbLogcat(serial string) *AdbLogcat {
return &AdbLogcat{
serial: serial,
logBuffer: new(bytes.Buffer),
stopping: make(chan struct{}),
@@ -220,7 +222,7 @@ func NewAdbLogcat(serial string) *DeviceLogcat {
}
// CatchLogcatContext starts logcat with timeout context
func (l *DeviceLogcat) CatchLogcatContext(timeoutCtx context.Context) (err error) {
func (l *AdbLogcat) CatchLogcatContext(timeoutCtx context.Context) (err error) {
if err = l.CatchLogcat(); err != nil {
return
}
@@ -234,7 +236,7 @@ func (l *DeviceLogcat) CatchLogcatContext(timeoutCtx context.Context) (err error
return
}
func (l *DeviceLogcat) Stop() error {
func (l *AdbLogcat) Stop() error {
select {
case <-l.stopping:
default:
@@ -245,7 +247,7 @@ func (l *DeviceLogcat) Stop() error {
return l.Errors()
}
func (l *DeviceLogcat) Errors() (err error) {
func (l *AdbLogcat) Errors() (err error) {
for _, e := range l.errs {
if err != nil {
err = fmt.Errorf("%v |[DeviceLogcatErr] %v", err, e)
@@ -256,7 +258,7 @@ func (l *DeviceLogcat) Errors() (err error) {
return
}
func (l *DeviceLogcat) CatchLogcat() (err error) {
func (l *AdbLogcat) CatchLogcat() (err error) {
if l.cmd != nil {
log.Warn().Msg("logcat already start")
return nil
@@ -284,7 +286,7 @@ func (l *DeviceLogcat) CatchLogcat() (err error) {
return
}
func (l *DeviceLogcat) BufferedLogcat() (err error) {
func (l *AdbLogcat) BufferedLogcat() (err error) {
// -d: dump the current buffered logcat result and exits
cmd := myexec.Command("adb", "-s", l.serial, "logcat", "-d")
cmd.Stdout = l.logBuffer

View File

@@ -35,7 +35,7 @@ type uiaDriver struct {
Driver
adbDevice gadb.Device
logcat *DeviceLogcat
logcat *AdbLogcat
localPort int
}
@@ -478,7 +478,7 @@ func (ud *uiaDriver) AppTerminate(bundleId string) (successful bool, err error)
return false, err
}
_, err = ud.adbDevice.RunShellCommand("am force-stop", bundleId)
_, err = ud.adbDevice.RunShellCommand("am", "force-stop", bundleId)
return err == nil, err
}
@@ -508,9 +508,9 @@ func (ud *uiaDriver) TapFloat(x, y float64, options ...DataOption) (err error) {
"y": y,
}
// new data options in post data for extra uiautomator configurations
d := NewData(data, options...)
newData := NewData(data, options...)
_, err = ud.httpPOST(d.Data, "/session", ud.sessionId, "appium/tap")
_, err = ud.httpPOST(newData, "/session", ud.sessionId, "appium/tap")
return
}
@@ -566,9 +566,9 @@ func (ud *uiaDriver) DragFloat(fromX, fromY, toX, toY float64, options ...DataOp
}
// new data options in post data for extra uiautomator configurations
d := NewData(data, options...)
newData := NewData(data, options...)
return ud._drag(d.Data)
return ud._drag(newData)
}
func (ud *uiaDriver) _swipe(startX, startY, endX, endY interface{}, options ...DataOption) (err error) {
@@ -581,9 +581,9 @@ func (ud *uiaDriver) _swipe(startX, startY, endX, endY interface{}, options ...D
}
// new data options in post data for extra uiautomator configurations
d := NewData(data, options...)
newData := NewData(data, options...)
_, err = ud.httpPOST(d.Data, "/session", ud.sessionId, "touch/perform")
_, err = ud.httpPOST(newData, "/session", ud.sessionId, "touch/perform")
return
}
@@ -672,9 +672,9 @@ func (ud *uiaDriver) SendKeys(text string, options ...DataOption) (err error) {
"text": text,
}
// new data options in post data for extra uiautomator configurations
d := NewData(data, options...)
newData := NewData(data, options...)
_, err = ud.httpPOST(d.Data, "/session", ud.sessionId, "keys")
_, err = ud.httpPOST(newData, "/session", ud.sessionId, "keys")
return
}
@@ -683,14 +683,14 @@ func (ud *uiaDriver) Input(text string, options ...DataOption) (err error) {
"view": text,
}
// new data options in post data for extra uiautomator configurations
d := NewData(data, options...)
newData := NewData(data, options...)
var element WebElement
if valuetext, ok := d.Data["textview"]; ok {
if valuetext, ok := newData["textview"]; ok {
element, err = ud.FindElement(BySelector{UiAutomator: NewUiSelectorHelper().TextContains(fmt.Sprintf("%v", valuetext)).String()})
} else if valueid, ok := d.Data["id"]; ok {
} else if valueid, ok := newData["id"]; ok {
element, err = ud.FindElement(BySelector{ResourceIdID: fmt.Sprintf("%v", valueid)})
} else if valuedesc, ok := d.Data["description"]; ok {
} else if valuedesc, ok := newData["description"]; ok {
element, err = ud.FindElement(BySelector{UiAutomator: NewUiSelectorHelper().Description(fmt.Sprintf("%v", valuedesc)).String()})
} else {
element, err = ud.FindElement(BySelector{ClassName: ElementType{EditText: true}})

View File

@@ -30,9 +30,9 @@ func (ue uiaElement) SendKeys(text string, options ...DataOption) (err error) {
}
// new data options in post data for extra uiautomator configurations
d := NewData(data, options...)
newData := NewData(data, options...)
_, err = ue.parent.httpPOST(d.Data, "/session", ue.parent.sessionId, "/element", ue.id, "/value")
_, err = ue.parent.httpPOST(newData, "/session", ue.parent.sessionId, "/element", ue.id, "/value")
return
}

View File

@@ -205,6 +205,7 @@ type DriverExt struct {
frame *bytes.Buffer
doneMjpegStream chan bool
scale float64
ocrService OCRService // used to get text from image
StartTime time.Time // used to associate screenshots name
ScreenShots []string // save screenshots path
perfStop chan struct{} // stop performance monitor
@@ -227,6 +228,10 @@ func extend(driver WebDriver) (dExt *DriverExt, err error) {
return nil, err
}
if dExt.ocrService, err = newVEDEMOCRService(); err != nil {
return nil, err
}
return dExt, nil
}
@@ -254,21 +259,14 @@ func (dExt *DriverExt) takeScreenShot() (raw *bytes.Buffer, err error) {
return raw, nil
}
// saveScreenShot saves image file to $CWD/screenshots/ folder
func (dExt *DriverExt) saveScreenShot(raw *bytes.Buffer, fileName string) (string, error) {
// saveScreenShot saves image file with file name
func saveScreenShot(raw *bytes.Buffer, fileName string) (string, error) {
img, format, err := image.Decode(raw)
if err != nil {
return "", errors.Wrap(err, "decode screenshot image failed")
}
dir, _ := os.Getwd()
screenshotsDir := filepath.Join(dir, "screenshots")
if err = os.MkdirAll(screenshotsDir, os.ModePerm); err != nil {
return "", errors.Wrap(err, "create screenshots directory failed")
}
screenshotPath := filepath.Join(screenshotsDir,
fmt.Sprintf("%s.%s", fileName, format))
screenshotPath := filepath.Join(fmt.Sprintf("%s.%s", fileName, format))
file, err := os.Create(screenshotPath)
if err != nil {
return "", errors.Wrap(err, "create screenshot image file failed")
@@ -299,7 +297,13 @@ func (dExt *DriverExt) ScreenShot(fileName string) (string, error) {
return "", errors.Wrap(err, "screenshot failed")
}
path, err := dExt.saveScreenShot(raw, fileName)
dir, _ := os.Getwd()
screenshotsDir := filepath.Join(dir, "screenshots")
if err = os.MkdirAll(screenshotsDir, os.ModePerm); err != nil {
return "", errors.Wrap(err, "create screenshots directory failed")
}
fileName = filepath.Join(screenshotsDir, fileName)
path, err := saveScreenShot(raw, fileName)
if err != nil {
return "", errors.Wrap(err, "save screenshot failed")
}

5
hrp/pkg/uixt/input.go Normal file
View File

@@ -0,0 +1,5 @@
package uixt
func (dExt *DriverExt) Input(text string) (err error) {
return dExt.Driver.Input(text)
}

View File

@@ -784,6 +784,7 @@ type DataOptions struct {
IgnoreNotFoundError bool // ignore error if target element not found
MaxRetryTimes int // max retry times if target element not found
Interval float64 // interval between retries in seconds
ScreenShotFilename string // turn on screenshot and specify file name
}
type DataOption func(data *DataOptions)
@@ -860,12 +861,19 @@ func WithDataWaitTime(sec float64) DataOption {
}
}
func NewData(data map[string]interface{}, options ...DataOption) *DataOptions {
if data == nil {
data = make(map[string]interface{})
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 {
dataOptions := &DataOptions{
Data: data,
Data: make(map[string]interface{}),
}
for _, option := range options {
option(dataOptions)
@@ -874,7 +882,18 @@ func NewData(data map[string]interface{}, options ...DataOption) *DataOptions {
if len(dataOptions.Scope) == 0 {
dataOptions.Scope = []int{0, 0, math.MaxInt64, math.MaxInt64} // default scope
}
return dataOptions
}
func NewData(data map[string]interface{}, options ...DataOption) map[string]interface{} {
dataOptions := NewDataOptions(options...)
// merge with data options
for k, v := range dataOptions.Data {
data[k] = v
}
// handle point offset
if len(dataOptions.Offset) == 2 {
if x, ok := data["x"]; ok {
xf, _ := builtin.Interface2Float64(x)
@@ -886,23 +905,24 @@ func NewData(data map[string]interface{}, options ...DataOption) *DataOptions {
}
}
if _, ok := dataOptions.Data["steps"]; !ok {
dataOptions.Data["steps"] = 12 // default steps
// add default options
if _, ok := data["steps"]; !ok {
data["steps"] = 12 // default steps
}
if _, ok := dataOptions.Data["duration"]; !ok {
dataOptions.Data["duration"] = 0 // default duration
if _, ok := data["duration"]; !ok {
data["duration"] = 0 // default duration
}
if _, ok := dataOptions.Data["frequency"]; !ok {
dataOptions.Data["frequency"] = 60 // default frequency
if _, ok := data["frequency"]; !ok {
data["frequency"] = 60 // default frequency
}
if _, ok := dataOptions.Data["isReplace"]; !ok {
dataOptions.Data["isReplace"] = true // default true
if _, ok := data["isReplace"]; !ok {
data["isReplace"] = true // default true
}
return dataOptions
return data
}
// current implemeted device: IOSDevice, AndroidDevice

View File

@@ -2,10 +2,12 @@ package uixt
import (
"bytes"
"context"
"encoding/base64"
builtinJSON "encoding/json"
"fmt"
"io"
builtinLog "log"
"mime"
"mime/multipart"
"net"
@@ -46,6 +48,23 @@ const (
dismissAlertButtonSelector = "**/XCUIElementTypeButton[`label IN {'不允许','暂不'}`]"
)
type IOSPerfOption = gidevice.PerfOption
var (
WithIOSPerfSystemCPU = gidevice.WithPerfSystemCPU
WithIOSPerfSystemMem = gidevice.WithPerfSystemMem
WithIOSPerfSystemDisk = gidevice.WithPerfSystemDisk
WithIOSPerfSystemNetwork = gidevice.WithPerfSystemNetwork
WithIOSPerfGPU = gidevice.WithPerfGPU
WithIOSPerfFPS = gidevice.WithPerfFPS
WithIOSPerfNetwork = gidevice.WithPerfNetwork
WithIOSPerfBundleID = gidevice.WithPerfBundleID
WithIOSPerfPID = gidevice.WithPerfPID
WithIOSPerfOutputInterval = gidevice.WithPerfOutputInterval
WithIOSPerfProcessAttributes = gidevice.WithPerfProcessAttributes
WithIOSPerfSystemAttributes = gidevice.WithPerfSystemAttributes
)
type IOSDeviceOption func(*IOSDevice)
func WithUDID(udid string) IOSDeviceOption {
@@ -66,7 +85,7 @@ func WithWDAMjpegPort(port int) IOSDeviceOption {
}
}
func WithLogOn(logOn bool) IOSDeviceOption {
func WithWDALogOn(logOn bool) IOSDeviceOption {
return func(device *IOSDevice) {
device.LogOn = logOn
}
@@ -96,7 +115,13 @@ func WithDismissAlertButtonSelector(selector string) IOSDeviceOption {
}
}
func WithPerfOptions(options ...gidevice.PerfOption) IOSDeviceOption {
func WithXCTest(bundleID string) IOSDeviceOption {
return func(device *IOSDevice) {
device.XCTestBundleID = bundleID
}
}
func WithIOSPerfOptions(options ...gidevice.PerfOption) IOSDeviceOption {
return func(device *IOSDevice) {
device.PerfOptions = &gidevice.PerfOptions{}
for _, option := range options {
@@ -124,6 +149,10 @@ func IOSDevices(udid ...string) (devices []gidevice.Device, err error) {
if u != "" && u != d.Properties().SerialNumber {
continue
}
// filter non-usb ios devices
if d.Properties().ConnectionType != "USB" {
continue
}
deviceList = append(deviceList, d)
}
}
@@ -142,10 +171,13 @@ func GetIOSDeviceOptions(dev *IOSDevice) (deviceOptions []IOSDeviceOption) {
deviceOptions = append(deviceOptions, WithWDAMjpegPort(dev.MjpegPort))
}
if dev.LogOn {
deviceOptions = append(deviceOptions, WithLogOn(true))
deviceOptions = append(deviceOptions, WithWDALogOn(true))
}
if dev.PerfOptions != nil {
deviceOptions = append(deviceOptions, WithPerfOptions(dev.perfOpitons()...))
deviceOptions = append(deviceOptions, WithIOSPerfOptions(dev.perfOpitons()...))
}
if dev.XCTestBundleID != "" {
deviceOptions = append(deviceOptions, WithXCTest(dev.XCTestBundleID))
}
if dev.ResetHomeOnStartup {
deviceOptions = append(deviceOptions, WithResetHomeOnStartup(true))
@@ -182,10 +214,21 @@ func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) {
return nil, err
}
if len(deviceList) > 0 {
device.UDID = deviceList[0].Properties().SerialNumber
for _, dev := range deviceList {
udid := dev.Properties().SerialNumber
device.UDID = udid
device.d = dev
// run xctest if XCTestBundleID is set
if device.XCTestBundleID != "" {
_, err = device.RunXCTest(device.XCTestBundleID)
if err != nil {
log.Error().Err(err).Str("udid", udid).Msg("failed to init XCTest")
continue
}
}
log.Info().Str("udid", device.UDID).Msg("select device")
device.d = deviceList[0]
return device, nil
}
@@ -194,12 +237,13 @@ func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) {
}
type IOSDevice struct {
d gidevice.Device
PerfOptions *gidevice.PerfOptions `json:"perf_options,omitempty" yaml:"perf_options,omitempty"`
UDID string `json:"udid,omitempty" yaml:"udid,omitempty"`
Port int `json:"port,omitempty" yaml:"port,omitempty"` // WDA remote port
MjpegPort int `json:"mjpeg_port,omitempty" yaml:"mjpeg_port,omitempty"` // WDA remote MJPEG port
LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"`
d gidevice.Device
PerfOptions *gidevice.PerfOptions `json:"perf_options,omitempty" yaml:"perf_options,omitempty"`
UDID string `json:"udid,omitempty" yaml:"udid,omitempty"`
Port int `json:"port,omitempty" yaml:"port,omitempty"` // WDA remote port
MjpegPort int `json:"mjpeg_port,omitempty" yaml:"mjpeg_port,omitempty"` // WDA remote MJPEG port
LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"`
XCTestBundleID string `json:"xctest_bundle_id,omitempty" yaml:"xctest_bundle_id,omitempty"`
// switch to iOS springboard before init WDA session
ResetHomeOnStartup bool `json:"reset_home_on_startup,omitempty" yaml:"reset_home_on_startup,omitempty"`
@@ -475,6 +519,33 @@ func (dev *IOSDevice) NewUSBDriver(capabilities Capabilities) (driver WebDriver,
return wd, nil
}
func (dev *IOSDevice) RunXCTest(bundleID string) (cancel context.CancelFunc, err error) {
log.Info().Str("bundleID", bundleID).Msg("run xctest")
out, cancel, err := dev.d.XCTest(bundleID)
if err != nil {
return nil, errors.Wrap(err, "run xctest failed")
}
// wait for xctest to start
time.Sleep(5 * time.Second)
f, err := os.OpenFile(fmt.Sprintf("xctest_%s.log", dev.UDID),
os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o666)
if err != nil {
return nil, err
}
defer builtinLog.SetOutput(f)
// print xctest running logs
go func() {
for s := range out {
builtinLog.Print(s)
}
f.Close()
}()
return cancel, nil
}
func (dExt *DriverExt) ConnectMjpegStream(httpClient *http.Client) (err error) {
if httpClient == nil {
return errors.New(`'httpClient' can't be nil`)

View File

@@ -381,9 +381,9 @@ func (wd *wdaDriver) TapFloat(x, y float64, options ...DataOption) (err error) {
"y": y,
}
// new data options in post data for extra WDA configurations
d := NewData(data, options...)
newData := NewData(data, options...)
_, err = wd.httpPOST(d.Data, "/session", wd.sessionId, "/wda/tap/0")
_, err = wd.httpPOST(newData, "/session", wd.sessionId, "/wda/tap/0")
return
}
@@ -433,9 +433,9 @@ func (wd *wdaDriver) DragFloat(fromX, fromY, toX, toY float64, options ...DataOp
}
// new data options in post data for extra WDA configurations
d := NewData(data, options...)
newData := NewData(data, options...)
_, err = wd.httpPOST(d.Data, "/session", wd.sessionId, "/wda/dragfromtoforduration")
_, err = wd.httpPOST(newData, "/session", wd.sessionId, "/wda/dragfromtoforduration")
return
}
@@ -506,9 +506,9 @@ func (wd *wdaDriver) SendKeys(text string, options ...DataOption) (err error) {
data := map[string]interface{}{"value": strings.Split(text, "")}
// new data options in post data for extra WDA configurations
d := NewData(data, options...)
newData := NewData(data, options...)
_, err = wd.httpPOST(d.Data, "/session", wd.sessionId, "/wda/keys")
_, err = wd.httpPOST(newData, "/session", wd.sessionId, "/wda/keys")
return
}
@@ -541,9 +541,9 @@ func (wd *wdaDriver) PressBack(options ...DataOption) (err error) {
}
// new data options in post data for extra WDA configurations
d := NewData(data, options...)
newData := NewData(data, options...)
_, err = wd.httpPOST(d.Data, "/session", wd.sessionId, "/wda/dragfromtoforduration")
_, err = wd.httpPOST(newData, "/session", wd.sessionId, "/wda/dragfromtoforduration")
return
}

View File

@@ -31,9 +31,9 @@ func (we wdaElement) SendKeys(text string, options ...DataOption) (err error) {
"value": strings.Split(text, ""),
}
// new data options in post data for extra uiautomator configurations
d := NewData(data, options...)
newData := NewData(data, options...)
_, err = we.parent.httpPOST(d.Data, "/session", we.parent.sessionId, "/element", we.id, "/value")
_, err = we.parent.httpPOST(newData, "/session", we.parent.sessionId, "/element", we.id, "/value")
return
}

View File

@@ -56,7 +56,7 @@ func checkEnv() error {
return nil
}
func (s *veDEMOCRService) getOCRResult(imageBuf []byte) ([]OCRResult, error) {
func (s *veDEMOCRService) getOCRResult(imageBuf *bytes.Buffer) ([]OCRResult, error) {
bodyBuf := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuf)
bodyWriter.WriteField("withDet", "true")
@@ -67,7 +67,7 @@ func (s *veDEMOCRService) getOCRResult(imageBuf []byte) ([]OCRResult, error) {
return nil, errors.Wrap(code.OCRRequestError,
fmt.Sprintf("create form file error: %v", err))
}
size, err := formWriter.Write(imageBuf)
size, err := formWriter.Write(imageBuf.Bytes())
if err != nil {
return nil, errors.Wrap(code.OCRRequestError,
fmt.Sprintf("write form error: %v", err))
@@ -147,8 +147,22 @@ func getLogID(header http.Header) string {
return logID[0]
}
func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...DataOption) (rect image.Rectangle, err error) {
data := NewData(map[string]interface{}{}, options...)
type OCRText struct {
Text string
Rect image.Rectangle
}
type OCRTexts []OCRText
func (t OCRTexts) Texts() (texts []string) {
for _, text := range t {
texts = append(texts, text.Text)
}
return texts
}
func (s *veDEMOCRService) GetTexts(imageBuf *bytes.Buffer, options ...DataOption) (
ocrTexts OCRTexts, err error) {
ocrResults, err := s.getOCRResult(imageBuf)
if err != nil {
@@ -156,10 +170,18 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...Data
return
}
var rects []image.Rectangle
var ocrTexts []string
dataOptions := NewDataOptions(options...)
if dataOptions.ScreenShotFilename != "" {
path, err := saveScreenShot(imageBuf, dataOptions.ScreenShotFilename)
if err != nil {
return nil, errors.Wrap(err, "save screenshot failed")
}
log.Debug().Str("path", path).Msg("save screenshot")
}
for _, ocrResult := range ocrResults {
rect = image.Rectangle{
rect := image.Rectangle{
// ocrResult.Points 顺序:左上 -> 右上 -> 右下 -> 左下
Min: image.Point{
X: int(ocrResult.Points[0].X),
@@ -170,35 +192,62 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...Data
Y: int(ocrResult.Points[2].Y),
},
}
if rect.Min.X >= data.Scope[0] && rect.Max.X <= data.Scope[2] && rect.Min.Y >= data.Scope[1] && rect.Max.Y <= data.Scope[3] {
ocrTexts = append(ocrTexts, ocrResult.Text)
// not contains text
if !strings.Contains(ocrResult.Text, text) {
continue
}
// check if text in scope
if rect.Min.X < dataOptions.Scope[0] || rect.Max.X > dataOptions.Scope[2] ||
rect.Min.Y < dataOptions.Scope[1] || rect.Max.Y > dataOptions.Scope[3] {
// not in scope
continue
}
rects = append(rects, rect)
ocrTexts = append(ocrTexts, OCRText{
Text: ocrResult.Text,
Rect: rect,
})
}
return
}
// contains text while not match exactly
if ocrResult.Text != text {
continue
}
func (s *veDEMOCRService) FindText(text string, imageBuf *bytes.Buffer, options ...DataOption) (
rect image.Rectangle, err error) {
// match exactly, and not specify index, return the first one
if data.Index == 0 {
return rect, nil
}
ocrTexts, err := s.GetTexts(imageBuf, options...)
if err != nil {
log.Error().Err(err).Msg("GetTexts failed")
return
}
dataOptions := NewDataOptions(options...)
var rects []image.Rectangle
for _, ocrText := range ocrTexts {
rect = ocrText.Rect
// not contains text
if !strings.Contains(ocrText.Text, text) {
continue
}
rects = append(rects, rect)
// contains text while not match exactly
if ocrText.Text != text {
continue
}
// match exactly, and not specify index, return the first one
if dataOptions.Index == 0 {
return rect, nil
}
}
if len(rects) == 0 {
return image.Rectangle{}, errors.Wrap(code.OCRTextNotFoundError,
fmt.Sprintf("text %s not found in %v", text, ocrTexts))
fmt.Sprintf("text %s not found in %v", text, ocrTexts.Texts()))
}
// get index
idx := data.Index
idx := dataOptions.Index
if idx > 0 {
// NOTICE: index start from 1
idx = idx - 1
@@ -215,45 +264,29 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...Data
return rects[idx], nil
}
func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte, options ...DataOption) (rects []image.Rectangle, err error) {
ocrResults, err := s.getOCRResult(imageBuf)
func (s *veDEMOCRService) FindTexts(texts []string, imageBuf *bytes.Buffer, options ...DataOption) (
rects []image.Rectangle, err error) {
ocrTexts, err := s.GetTexts(imageBuf, options...)
if err != nil {
log.Error().Err(err).Msg("getOCRResult failed")
log.Error().Err(err).Msg("GetTexts failed")
return
}
data := NewData(map[string]interface{}{}, options...)
ocrTexts := map[string]bool{}
var success bool
var rect image.Rectangle
for _, text := range texts {
var found bool
for _, ocrResult := range ocrResults {
rect = image.Rectangle{
// ocrResult.Points 顺序:左上 -> 右上 -> 右下 -> 左下
Min: image.Point{
X: int(ocrResult.Points[0].X),
Y: int(ocrResult.Points[0].Y),
},
Max: image.Point{
X: int(ocrResult.Points[2].X),
Y: int(ocrResult.Points[2].Y),
},
for _, ocrText := range ocrTexts {
rect := ocrText.Rect
// not contains text
if !strings.Contains(ocrText.Text, text) {
continue
}
if rect.Min.X >= data.Scope[0] && rect.Max.X <= data.Scope[2] && rect.Min.Y >= data.Scope[1] && rect.Max.Y <= data.Scope[3] {
ocrTexts[ocrResult.Text] = true
// not contains text
if !strings.Contains(ocrResult.Text, text) {
continue
}
found = true
rects = append(rects, rect)
break
}
found = true
rects = append(rects, rect)
break
}
if !found {
rects = append(rects, image.Rectangle{})
@@ -263,14 +296,32 @@ func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte, options ...
if !success {
return rects, errors.Wrap(code.OCRTextNotFoundError,
fmt.Sprintf("texts %s not found in %v", texts, ocrTexts))
fmt.Sprintf("texts %s not found in %v", texts, ocrTexts.Texts()))
}
return rects, nil
}
type OCRService interface {
FindText(text string, imageBuf []byte, index ...int) (rect image.Rectangle, err error)
GetTexts(imageBuf *bytes.Buffer, options ...DataOption) (ocrTexts OCRTexts, err error)
FindText(text string, imageBuf *bytes.Buffer, options ...DataOption) (rect 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) {
var bufSource *bytes.Buffer
if bufSource, err = dExt.takeScreenShot(); err != nil {
err = fmt.Errorf("takeScreenShot error: %v", err)
return
}
ocrTexts, err := dExt.ocrService.GetTexts(bufSource, options...)
if err != nil {
log.Error().Err(err).Msg("GetTexts failed")
return
}
return ocrTexts, nil
}
func (dExt *DriverExt) FindTextByOCR(ocrText string, options ...DataOption) (x, y, width, height float64, err error) {
@@ -280,11 +331,7 @@ func (dExt *DriverExt) FindTextByOCR(ocrText string, options ...DataOption) (x,
return
}
service, err := newVEDEMOCRService()
if err != nil {
return
}
rect, err := service.FindText(ocrText, bufSource.Bytes(), options...)
rect, err := dExt.ocrService.FindText(ocrText, bufSource, options...)
if err != nil {
log.Warn().Msgf("FindText failed: %s", err.Error())
return
@@ -303,11 +350,7 @@ func (dExt *DriverExt) FindTextsByOCR(ocrTexts []string, options ...DataOption)
return
}
service, err := newVEDEMOCRService()
if err != nil {
return
}
rects, err := service.FindTexts(ocrTexts, bufSource.Bytes(), options...)
rects, err := dExt.ocrService.FindTexts(ocrTexts, bufSource, options...)
if err != nil {
log.Warn().Msgf("FindTexts failed: %s", err.Error())
return

View File

@@ -3,12 +3,13 @@
package uixt
import (
"bytes"
"fmt"
"os"
"testing"
)
func checkOCR(buff []byte) error {
func checkOCR(buff *bytes.Buffer) error {
service, err := newVEDEMOCRService()
if err != nil {
return err
@@ -33,19 +34,23 @@ func TestOCRWithScreenshot(t *testing.T) {
t.Fatal(err)
}
if err := checkOCR(raw.Bytes()); err != nil {
if err := checkOCR(raw); err != nil {
t.Fatal(err)
}
}
func TestOCRWithLocalFile(t *testing.T) {
imagePath := "/Users/debugtalk/Downloads/s1.png"
file, err := os.ReadFile(imagePath)
if err != nil {
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)
}
}

View File

@@ -69,9 +69,9 @@ type Action func(driver *DriverExt) error
// findCondition indicates the condition to find a UI element
// foundAction indicates the action to do after a UI element is found
func (dExt *DriverExt) SwipeUntil(direction interface{}, findCondition Action, foundAction Action, options ...DataOption) error {
d := NewData(nil, options...)
maxRetryTimes := d.MaxRetryTimes
interval := d.Interval
dataOptions := NewDataOptions(options...)
maxRetryTimes := dataOptions.MaxRetryTimes
interval := dataOptions.Interval
for i := 0; i < maxRetryTimes; i++ {
if err := findCondition(dExt); err == nil {
@@ -103,9 +103,9 @@ func (dExt *DriverExt) SwipeUntil(direction interface{}, findCondition Action, f
}
func (dExt *DriverExt) LoopUntil(findAction, findCondition, foundAction Action, options ...DataOption) error {
d := NewData(nil, options...)
maxRetryTimes := d.MaxRetryTimes
interval := d.Interval
dataOptions := NewDataOptions(options...)
maxRetryTimes := dataOptions.MaxRetryTimes
interval := dataOptions.Interval
for i := 0; i < maxRetryTimes; i++ {
if err := findCondition(dExt); err == nil {

View File

@@ -65,11 +65,11 @@ func (dExt *DriverExt) GetImageXY(imagePath string, options ...DataOption) (poin
}
func (dExt *DriverExt) TapByOCR(ocrText string, options ...DataOption) error {
data := NewData(map[string]interface{}{}, options...)
dataOptions := NewDataOptions(options...)
point, err := dExt.GetTextXY(ocrText, options...)
if err != nil {
if data.IgnoreNotFoundError {
if dataOptions.IgnoreNotFoundError {
return nil
}
return err
@@ -79,11 +79,11 @@ func (dExt *DriverExt) TapByOCR(ocrText string, options ...DataOption) error {
}
func (dExt *DriverExt) TapByCV(imagePath string, options ...DataOption) error {
data := NewData(map[string]interface{}{}, options...)
dataOptions := NewDataOptions(options...)
point, err := dExt.GetImageXY(imagePath, options...)
if err != nil {
if data.IgnoreNotFoundError {
if dataOptions.IgnoreNotFoundError {
return nil
}
return err
@@ -103,11 +103,11 @@ func (dExt *DriverExt) TapOffset(param string, xOffset, yOffset float64, options
return ele.Click()
}
data := NewData(map[string]interface{}{}, options...)
dataOptions := NewDataOptions(options...)
x, y, width, height, err := dExt.FindUIRectInUIKit(param, options...)
if err != nil {
if data.IgnoreNotFoundError {
if dataOptions.IgnoreNotFoundError {
return nil
}
return err

View File

@@ -3,6 +3,7 @@ package hrp
import (
"crypto/tls"
_ "embed"
"fmt"
"net"
"net/http"
"net/http/cookiejar"
@@ -499,12 +500,34 @@ func (r *SessionRunner) Start(givenVars map[string]interface{}) error {
log.Info().Str("step", stepName).
Str("type", string(step.Type())).Msg("run step start")
// run step
stepResult, err := step.Run(r)
stepResult.Name = stepName
// run times of step
loopTimes := step.Struct().Loops
if loopTimes < 0 {
log.Warn().Int("loops", loopTimes).Msg("loop times should be positive, set to 1")
loopTimes = 1
} else if loopTimes == 0 {
loopTimes = 1
} else if loopTimes > 1 {
log.Info().Int("loops", loopTimes).Msg("run step with specified loop times")
}
// run step with specified loop times
var stepResult *StepResult
for i := 1; i <= loopTimes; i++ {
var loopIndex string
if loopTimes > 1 {
log.Info().Int("index", i).Msg("start running step in loop")
loopIndex = fmt.Sprintf("_loop_%d", i)
}
// run step
stepResult, err = step.Run(r)
stepResult.Name = stepName + loopIndex
// update summary
r.summary.Records = append(r.summary.Records, stepResult)
}
// update summary
r.summary.Records = append(r.summary.Records, stepResult)
r.summary.Stat.Total += 1
if stepResult.Success {
r.summary.Stat.Successes += 1

View File

@@ -1,10 +1,5 @@
package hrp
import (
"github.com/httprunner/httprunner/v4/hrp/pkg/gidevice"
"github.com/httprunner/httprunner/v4/hrp/pkg/uixt"
)
type StepType string
const (
@@ -19,39 +14,6 @@ const (
stepTypeIOS StepType = "ios"
)
var (
WithIdentifier = uixt.WithIdentifier
WithMaxRetryTimes = uixt.WithMaxRetryTimes
WithWaitTime = uixt.WithWaitTime // only applicable to SwipeToTap* action
WithIndex = uixt.WithIndex // index of the target element, should start from 1, only applicable to ocr actions
WithTimeout = uixt.WithTimeout
WithIgnoreNotFoundError = uixt.WithIgnoreNotFoundError
WithText = uixt.WithText
WithID = uixt.WithID
WithDescription = uixt.WithDescription
WithDuration = uixt.WithDuration // only applicable to ios swipe action
WithSteps = uixt.WithSteps // only applicable to android swipe action
WithDirection = uixt.WithDirection
WithCustomDirection = uixt.WithCustomDirection
WithScope = uixt.WithScope // only applicable to ocr actions
WithOffset = uixt.WithOffset
)
var (
WithPerfSystemCPU = gidevice.WithPerfSystemCPU
WithPerfSystemMem = gidevice.WithPerfSystemMem
WithPerfSystemDisk = gidevice.WithPerfSystemDisk
WithPerfSystemNetwork = gidevice.WithPerfSystemNetwork
WithPerfGPU = gidevice.WithPerfGPU
WithPerfFPS = gidevice.WithPerfFPS
WithPerfNetwork = gidevice.WithPerfNetwork
WithPerfBundleID = gidevice.WithPerfBundleID
WithPerfPID = gidevice.WithPerfPID
WithPerfOutputInterval = gidevice.WithPerfOutputInterval
WithPerfProcessAttributes = gidevice.WithPerfProcessAttributes
WithPerfSystemAttributes = gidevice.WithPerfSystemAttributes
)
type StepResult struct {
Name string `json:"name" yaml:"name"` // step name
StepType StepType `json:"step_type" yaml:"step_type"` // step type, testcase/request/transaction/rendezvous
@@ -83,6 +45,7 @@ type TStep struct {
Extract map[string]string `json:"extract,omitempty" yaml:"extract,omitempty"`
Validators []interface{} `json:"validate,omitempty" yaml:"validate,omitempty"`
Export []string `json:"export,omitempty" yaml:"export,omitempty"`
Loops int `json:"loops,omitempty" yaml:"loops,omitempty"`
}
// IStep represents interface for all types for teststeps, includes:

View File

@@ -11,27 +11,6 @@ import (
"github.com/httprunner/httprunner/v4/hrp/pkg/uixt"
)
// ios setting options
var (
WithUDID = uixt.WithUDID
WithWDAPort = uixt.WithWDAPort
WithWDAMjpegPort = uixt.WithWDAMjpegPort
WithLogOn = uixt.WithLogOn
WithResetHomeOnStartup = uixt.WithResetHomeOnStartup
WithSnapshotMaxDepth = uixt.WithSnapshotMaxDepth
WithAcceptAlertButtonSelector = uixt.WithAcceptAlertButtonSelector
WithDismissAlertButtonSelector = uixt.WithDismissAlertButtonSelector
WithPerfOptions = uixt.WithPerfOptions
)
// android setting options
var (
WithSerialNumber = uixt.WithSerialNumber
WithAdbIP = uixt.WithAdbIP
WithAdbPort = uixt.WithAdbPort
WithAdbLogOn = uixt.WithAdbLogOn
)
type MobileStep struct {
Serial string `json:"serial,omitempty" yaml:"serial,omitempty"`
uixt.MobileAction `yaml:",inline"`
@@ -301,28 +280,6 @@ func (s *StepMobile) Input(text string, options ...uixt.ActionOption) *StepMobil
return &StepMobile{step: s.step}
}
// Times specify running times for run last action
func (s *StepMobile) Times(n int) *StepMobile {
if n <= 0 {
log.Warn().Int("n", n).Msg("times should be positive, set to 1")
n = 1
}
mobileStep := s.mobileStep()
actionsTotal := len(mobileStep.Actions)
if actionsTotal == 0 {
return s
}
// actionsTotal >=1 && n >= 1
lastAction := mobileStep.Actions[actionsTotal-1 : actionsTotal][0]
for i := 0; i < n-1; i++ {
// duplicate last action n-1 times
mobileStep.Actions = append(mobileStep.Actions, lastAction)
}
return &StepMobile{step: s.step}
}
// Sleep specify sleep seconds after last action
func (s *StepMobile) Sleep(n float64) *StepMobile {
s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{

View File

@@ -4,12 +4,14 @@ package hrp
import (
"testing"
"github.com/httprunner/httprunner/v4/hrp/pkg/uixt"
)
func TestIOSSettingsAction(t *testing.T) {
testCase := &TestCase{
Config: NewConfig("ios ui action on Settings").
SetIOS(WithWDAPort(8700), WithWDAMjpegPort(8800)),
SetIOS(uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800)),
TestSteps: []IStep{
NewStep("launch Settings").
IOS().Home().Tap("设置").
@@ -32,7 +34,7 @@ func TestIOSSearchApp(t *testing.T) {
Config: NewConfig("ios ui action on Search App 资源库"),
TestSteps: []IStep{
NewStep("进入 App 资源库 搜索框").
IOS().Home().SwipeLeft().Times(2).Tap("dewey-search-field").
IOS().Home().SwipeLeft().SwipeLeft().Tap("dewey-search-field").
Validate().
AssertLabelExists("取消"),
NewStep("搜索抖音").
@@ -48,7 +50,7 @@ func TestIOSSearchApp(t *testing.T) {
func TestIOSAppLaunch(t *testing.T) {
testCase := &TestCase{
Config: NewConfig("启动 & 关闭 App").
SetIOS(WithWDAPort(8700), WithWDAMjpegPort(8800)),
SetIOS(uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800)),
TestSteps: []IStep{
NewStep("终止今日头条").
IOS().AppTerminate("com.ss.iphone.article.News"),
@@ -69,7 +71,7 @@ func TestIOSAppLaunch(t *testing.T) {
func TestIOSWeixinLive(t *testing.T) {
testCase := &TestCase{
Config: NewConfig("ios ui action on 微信直播").
SetIOS(WithLogOn(true), WithWDAPort(8100), WithWDAMjpegPort(9100)),
SetIOS(uixt.WithWDALogOn(true), uixt.WithWDAPort(8100), uixt.WithWDAMjpegPort(9100)),
TestSteps: []IStep{
NewStep("启动微信").
IOS().
@@ -84,10 +86,10 @@ func TestIOSWeixinLive(t *testing.T) {
TapByOCR("直播"). // 通过 OCR 识别「直播」
Validate().
AssertLabelExists("直播"),
NewStep("向上滑动 5 次").
NewStep("向上滑动 3,截图保存").
Loop(3). // 整体循环 3 次
IOS().
SwipeUp().Times(3).ScreenShot(). // 上划 3 次,截图保存
SwipeUp().Times(2).ScreenShot(), // 再上划 2 次,截图保存
SwipeUp().SwipeUp().ScreenShot(), // 上划 2 次,截图保存
},
}
err := NewRunner(t).Run(testCase)
@@ -154,7 +156,9 @@ func TestIOSDouyinAction(t *testing.T) {
AssertLabelExists("首页", "首页 tab 不存在").
AssertLabelExists("消息", "消息 tab 不存在"),
NewStep("swipe up and down").
IOS().SwipeUp().Times(3).SwipeDown(),
Loop(3).
IOS().
SwipeUp().SwipeDown(),
},
}
err := NewRunner(t).Run(testCase)

View File

@@ -566,6 +566,12 @@ func (s *StepRequest) HTTP2() *StepRequest {
return s
}
// Loop specify running times for the current step
func (s *StepRequest) Loop(times int) *StepRequest {
s.step.Loops = times
return s
}
// GET makes a HTTP GET request.
func (s *StepRequest) GET(url string) *StepRequestWithOptionalArgs {
if s.step.Request != nil {

View File

@@ -45,7 +45,7 @@ type Summary struct {
func (s *Summary) appendCaseSummary(caseSummary *TestCaseSummary) {
s.Success = s.Success && caseSummary.Success
s.Stat.TestCases.Total += 1
s.Stat.TestSteps.Total += len(caseSummary.Records)
s.Stat.TestSteps.Total += caseSummary.Stat.Total
if caseSummary.Success {
s.Stat.TestCases.Success += 1
} else {