mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-03 06:49:38 +08:00
feat: get wda logs
This commit is contained in:
@@ -5,7 +5,7 @@ import (
|
||||
)
|
||||
|
||||
func TestDriverExt_Drag(t *testing.T) {
|
||||
driverExt, err := InitWDAClient()
|
||||
driverExt, err := InitWDAClient(nil)
|
||||
checkErr(t, err)
|
||||
|
||||
pathSearch := "/Users/hero/Documents/temp/2020-05/opencv/IMG_map.png"
|
||||
|
||||
@@ -46,6 +46,7 @@ type DriverExt struct {
|
||||
frame *bytes.Buffer
|
||||
doneMjpegStream chan bool
|
||||
scale float64
|
||||
host string
|
||||
|
||||
CVArgs
|
||||
}
|
||||
|
||||
@@ -1,9 +1,16 @@
|
||||
package uixt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/electricbubble/gwda"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/json"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -21,9 +28,53 @@ const (
|
||||
dismissAlertButtonSelector = "**/XCUIElementTypeButton[`label IN {'不允许','暂不'}`]"
|
||||
)
|
||||
|
||||
func InitWDAClient(options ...gwda.DeviceOption) (*DriverExt, error) {
|
||||
type WDAOptions struct {
|
||||
UDID string `json:"udid,omitempty" yaml:"udid,omitempty"`
|
||||
Port int `json:"port,omitempty" yaml:"port,omitempty"`
|
||||
MjpegPort int `json:"mjpeg_port,omitempty" yaml:"mjpeg_port,omitempty"`
|
||||
LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"`
|
||||
}
|
||||
|
||||
type WDAOption func(*WDAOptions)
|
||||
|
||||
func WithUDID(udid string) WDAOption {
|
||||
return func(device *WDAOptions) {
|
||||
device.UDID = udid
|
||||
}
|
||||
}
|
||||
|
||||
func WithPort(port int) WDAOption {
|
||||
return func(device *WDAOptions) {
|
||||
device.Port = port
|
||||
}
|
||||
}
|
||||
|
||||
func WithMjpegPort(port int) WDAOption {
|
||||
return func(device *WDAOptions) {
|
||||
device.MjpegPort = port
|
||||
}
|
||||
}
|
||||
|
||||
func WithLogOn(logOn bool) WDAOption {
|
||||
return func(device *WDAOptions) {
|
||||
device.LogOn = logOn
|
||||
}
|
||||
}
|
||||
|
||||
func InitWDAClient(options *WDAOptions) (*DriverExt, error) {
|
||||
var deviceOptions []gwda.DeviceOption
|
||||
if options.UDID != "" {
|
||||
deviceOptions = append(deviceOptions, gwda.WithSerialNumber(options.UDID))
|
||||
}
|
||||
if options.Port != 0 {
|
||||
deviceOptions = append(deviceOptions, gwda.WithPort(options.Port))
|
||||
}
|
||||
if options.MjpegPort != 0 {
|
||||
deviceOptions = append(deviceOptions, gwda.WithMjpegPort(options.MjpegPort))
|
||||
}
|
||||
|
||||
// init wda device
|
||||
targetDevice, err := gwda.NewDevice(options...)
|
||||
targetDevice, err := gwda.NewDevice(deviceOptions...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -58,5 +109,71 @@ func InitWDAClient(options ...gwda.DeviceOption) (*DriverExt, error) {
|
||||
}
|
||||
log.Info().Interface("appiumWDASettings", settings).Msg("set appium WDA settings")
|
||||
|
||||
driverExt.host = fmt.Sprintf("http://127.0.0.1:%d", targetDevice.Port)
|
||||
if options.LogOn {
|
||||
err = driverExt.StartWDALog("hrp_wda_log")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return driverExt, nil
|
||||
}
|
||||
|
||||
type wdaResponse struct {
|
||||
Value string `json:"value"`
|
||||
SessionID string `json:"sessionId"`
|
||||
}
|
||||
|
||||
func (dExt *DriverExt) StartWDALog(identifier string) error {
|
||||
log.Info().Msg("start WDA log recording")
|
||||
data := map[string]interface{}{"action": "start", "type": 2, "identifier": identifier}
|
||||
_, err := dExt.triggerWDALog(data)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to start WDA log recording")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dExt *DriverExt) GetWDALog() (string, error) {
|
||||
log.Info().Msg("stop WDA log recording")
|
||||
data := map[string]interface{}{"action": "stop"}
|
||||
reply, err := dExt.triggerWDALog(data)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to get WDA logs")
|
||||
}
|
||||
|
||||
return reply.Value, nil
|
||||
}
|
||||
|
||||
func (dExt *DriverExt) triggerWDALog(data map[string]interface{}) (*wdaResponse, error) {
|
||||
// [[FBRoute POST:@"/gtf/automation/log"].withoutSession respondWithTarget:self action:@selector(handleAutomationLog:)]
|
||||
postJSON, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/gtf/automation/log", dExt.host)
|
||||
log.Info().Str("url", url).Interface("data", data).Msg("trigger WDA log")
|
||||
resp, err := http.DefaultClient.Post(url, "application/json", bytes.NewBuffer(postJSON))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, errors.Errorf("failed to trigger wda log, response status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
rawResp, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
reply := new(wdaResponse)
|
||||
if err = json.Unmarshal(rawResp, reply); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return reply, nil
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ package uixt
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"image"
|
||||
"io/ioutil"
|
||||
@@ -13,6 +12,8 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/json"
|
||||
)
|
||||
|
||||
var client = &http.Client{
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
)
|
||||
|
||||
func TestSwipeUntil(t *testing.T) {
|
||||
driverExt, err := InitWDAClient()
|
||||
driverExt, err := InitWDAClient(nil)
|
||||
checkErr(t, err)
|
||||
|
||||
var x, y, width, height float64
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
)
|
||||
|
||||
func TestDriverExt_TapWithNumber(t *testing.T) {
|
||||
driverExt, err := InitWDAClient()
|
||||
driverExt, err := InitWDAClient(nil)
|
||||
checkErr(t, err)
|
||||
|
||||
pathSearch := "/Users/hero/Documents/temp/2020-05/opencv/flag7.png"
|
||||
@@ -20,7 +20,7 @@ func TestDriverExt_TapWithNumber(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDriverExt_TapXY(t *testing.T) {
|
||||
driverExt, err := InitWDAClient()
|
||||
driverExt, err := InitWDAClient(nil)
|
||||
checkErr(t, err)
|
||||
|
||||
err = driverExt.TapXY(0.4, 0.5, "")
|
||||
@@ -28,7 +28,7 @@ func TestDriverExt_TapXY(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDriverExt_TapWithOCR(t *testing.T) {
|
||||
driverExt, err := InitWDAClient()
|
||||
driverExt, err := InitWDAClient(nil)
|
||||
checkErr(t, err)
|
||||
|
||||
// 需要点击文字上方的图标
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
)
|
||||
|
||||
func TestDriverExt_ForceTouch(t *testing.T) {
|
||||
driverExt, err := InitWDAClient()
|
||||
driverExt, err := InitWDAClient(nil)
|
||||
checkErr(t, err)
|
||||
|
||||
pathSearch := "/Users/hero/Documents/temp/2020-05/opencv/IMG_ft.png"
|
||||
@@ -21,7 +21,7 @@ func TestDriverExt_ForceTouch(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDriverExt_TouchAndHold(t *testing.T) {
|
||||
driverExt, err := InitWDAClient()
|
||||
driverExt, err := InitWDAClient(nil)
|
||||
checkErr(t, err)
|
||||
|
||||
pathSearch := "/Users/hero/Documents/temp/2020-05/opencv/IMG_ft.png"
|
||||
|
||||
Reference in New Issue
Block a user