mirror of
https://github.com/httprunner/httprunner.git
synced 2026-08-13 09:34:28 +08:00
fix: unittest
This commit is contained in:
39
hrp/internal/env/env.go
vendored
39
hrp/internal/env/env.go
vendored
@@ -7,25 +7,26 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
WDA_USB_DRIVER = os.Getenv("WDA_USB_DRIVER")
|
||||
WDA_LOCAL_PORT = os.Getenv("WDA_LOCAL_PORT")
|
||||
WDA_LOCAL_MJPEG_PORT = os.Getenv("WDA_LOCAL_MJPEG_PORT")
|
||||
VEDEM_OCR_URL = os.Getenv("VEDEM_OCR_URL")
|
||||
VEDEM_OCR_AK = os.Getenv("VEDEM_OCR_AK")
|
||||
VEDEM_OCR_SK = os.Getenv("VEDEM_OCR_SK")
|
||||
VEDEM_IM_URL = os.Getenv("VEDEM_IM_URL")
|
||||
VEDEM_IM_AK = os.Getenv("VEDEM_IM_AK")
|
||||
VEDEM_IM_SK = os.Getenv("VEDEM_IM_SK")
|
||||
VEDEM_CP_URL = os.Getenv("VEDEM_CP_URL")
|
||||
VEDEM_CP_AK = os.Getenv("VEDEM_CP_AK")
|
||||
VEDEM_CP_SK = os.Getenv("VEDEM_CP_SK")
|
||||
VEDEM_SD_URL = os.Getenv("VEDEM_SD_URL")
|
||||
VEDEM_SD_AK = os.Getenv("VEDEM_SD_AK")
|
||||
VEDEM_SD_SK = os.Getenv("VEDEM_SD_SK")
|
||||
DISABLE_GA = os.Getenv("DISABLE_GA")
|
||||
DISABLE_SENTRY = os.Getenv("DISABLE_SENTRY")
|
||||
PYPI_INDEX_URL = os.Getenv("PYPI_INDEX_URL")
|
||||
PATH = os.Getenv("PATH")
|
||||
WDA_USB_DRIVER = os.Getenv("WDA_USB_DRIVER")
|
||||
WDA_LOCAL_PORT = os.Getenv("WDA_LOCAL_PORT")
|
||||
WDA_LOCAL_MJPEG_PORT = os.Getenv("WDA_LOCAL_MJPEG_PORT")
|
||||
VEDEM_OCR_URL = os.Getenv("VEDEM_OCR_URL")
|
||||
VEDEM_OCR_AK = os.Getenv("VEDEM_OCR_AK")
|
||||
VEDEM_OCR_SK = os.Getenv("VEDEM_OCR_SK")
|
||||
VEDEM_IM_URL = os.Getenv("VEDEM_IM_URL")
|
||||
VEDEM_IM_AK = os.Getenv("VEDEM_IM_AK")
|
||||
VEDEM_IM_SK = os.Getenv("VEDEM_IM_SK")
|
||||
VEDEM_CP_URL = os.Getenv("VEDEM_CP_URL")
|
||||
VEDEM_CP_AK = os.Getenv("VEDEM_CP_AK")
|
||||
VEDEM_CP_SK = os.Getenv("VEDEM_CP_SK")
|
||||
VEDEM_SD_URL = os.Getenv("VEDEM_SD_URL")
|
||||
VEDEM_SD_AK = os.Getenv("VEDEM_SD_AK")
|
||||
VEDEM_SD_SK = os.Getenv("VEDEM_SD_SK")
|
||||
DISABLE_GA = os.Getenv("DISABLE_GA")
|
||||
DISABLE_SENTRY = os.Getenv("DISABLE_SENTRY")
|
||||
PYPI_INDEX_URL = os.Getenv("PYPI_INDEX_URL")
|
||||
PATH = os.Getenv("PATH")
|
||||
DISABLE_UIAUTOMATOR_SERVER = os.Getenv("DISABLE_UIAUTOMATOR_SERVER")
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
@@ -234,7 +234,7 @@ func (dExt *DriverExt) ClosePopupHandler() {
|
||||
x, y, width, height := dExt.MappingToRectInUIKit(rect)
|
||||
pointX := x + width*0.5
|
||||
pointY := y + height*0.5
|
||||
err = dExt.TapAbsXY(pointX, pointY)
|
||||
err = dExt.Driver.TapFloat(pointX, pointY)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/env"
|
||||
"net"
|
||||
"os/exec"
|
||||
"strings"
|
||||
@@ -167,10 +168,11 @@ func (dev *AndroidDevice) LogEnabled() bool {
|
||||
|
||||
func (dev *AndroidDevice) NewDriver(capabilities Capabilities) (driverExt *DriverExt, err error) {
|
||||
var driver WebDriver
|
||||
if dev.UIA2 {
|
||||
driver, err = dev.NewUSBDriver(capabilities)
|
||||
} else {
|
||||
disableUIA2 := env.DISABLE_UIAUTOMATOR_SERVER == "true"
|
||||
if disableUIA2 {
|
||||
driver, err = dev.NewAdbDriver()
|
||||
} else {
|
||||
driver, err = dev.NewUSBDriver(capabilities)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to init UIA driver")
|
||||
|
||||
@@ -87,7 +87,7 @@ func (ud *uiaDriver) NewSession(capabilities Capabilities) (sessionInfo SessionI
|
||||
// register(postHandler, new NewSession("/wd/hub/session"))
|
||||
var rawResp rawResponse
|
||||
data := map[string]interface{}{"capabilities": capabilities}
|
||||
if rawResp, err = ud.httpPOST(data, "/session"); err != nil {
|
||||
if rawResp, err = ud.tempHttpPOST(data, "/session"); err != nil {
|
||||
return SessionInfo{SessionId: ""}, err
|
||||
}
|
||||
reply := new(struct{ Value struct{ SessionId string } })
|
||||
@@ -103,7 +103,7 @@ func (ud *uiaDriver) DeleteSession() (err error) {
|
||||
if ud.sessionId == "" {
|
||||
return nil
|
||||
}
|
||||
if _, err = ud.httpDELETE("/session", ud.sessionId); err == nil {
|
||||
if _, err = ud.tempHttpDELETE("/session", ud.sessionId); err == nil {
|
||||
ud.sessionId = ""
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ func (ud *uiaDriver) DeleteSession() (err error) {
|
||||
func (ud *uiaDriver) Status() (deviceStatus DeviceStatus, err error) {
|
||||
// register(getHandler, new Status("/wd/hub/status"))
|
||||
var rawResp rawResponse
|
||||
if rawResp, err = ud.httpGET("/status"); err != nil {
|
||||
if rawResp, err = ud.tempHttpGET("/status"); err != nil {
|
||||
return DeviceStatus{Ready: false}, err
|
||||
}
|
||||
reply := new(struct {
|
||||
@@ -131,7 +131,7 @@ func (ud *uiaDriver) Status() (deviceStatus DeviceStatus, err error) {
|
||||
func (ud *uiaDriver) DeviceInfo() (deviceInfo DeviceInfo, err error) {
|
||||
// register(getHandler, new GetDeviceInfo("/wd/hub/session/:sessionId/appium/device/info"))
|
||||
var rawResp rawResponse
|
||||
if rawResp, err = ud.httpGET("/session", ud.sessionId, "appium/device/info"); err != nil {
|
||||
if rawResp, err = ud.tempHttpGET("/session", ud.sessionId, "appium/device/info"); err != nil {
|
||||
return DeviceInfo{}, err
|
||||
}
|
||||
reply := new(struct{ Value struct{ DeviceInfo } })
|
||||
@@ -145,7 +145,7 @@ func (ud *uiaDriver) DeviceInfo() (deviceInfo DeviceInfo, err error) {
|
||||
func (ud *uiaDriver) BatteryInfo() (batteryInfo BatteryInfo, err error) {
|
||||
// register(getHandler, new GetBatteryInfo("/wd/hub/session/:sessionId/appium/device/battery_info"))
|
||||
var rawResp rawResponse
|
||||
if rawResp, err = ud.httpGET("/session", ud.sessionId, "appium/device/battery_info"); err != nil {
|
||||
if rawResp, err = ud.tempHttpGET("/session", ud.sessionId, "appium/device/battery_info"); err != nil {
|
||||
return BatteryInfo{}, err
|
||||
}
|
||||
reply := new(struct{ Value struct{ BatteryInfo } })
|
||||
@@ -162,7 +162,7 @@ func (ud *uiaDriver) BatteryInfo() (batteryInfo BatteryInfo, err error) {
|
||||
func (ud *uiaDriver) WindowSize() (size Size, err error) {
|
||||
// register(getHandler, new GetDeviceSize("/wd/hub/session/:sessionId/window/:windowHandle/size"))
|
||||
var rawResp rawResponse
|
||||
if rawResp, err = ud.httpGET("/session", ud.sessionId, "window/:windowHandle/size"); err != nil {
|
||||
if rawResp, err = ud.tempHttpGET("/session", ud.sessionId, "window/:windowHandle/size"); err != nil {
|
||||
return Size{}, err
|
||||
}
|
||||
reply := new(struct{ Value struct{ Size } })
|
||||
@@ -176,7 +176,7 @@ func (ud *uiaDriver) WindowSize() (size Size, err error) {
|
||||
// PressBack simulates a short press on the BACK button.
|
||||
func (ud *uiaDriver) PressBack(options ...DataOption) (err error) {
|
||||
// register(postHandler, new PressBack("/wd/hub/session/:sessionId/back"))
|
||||
_, err = ud.httpPOST(nil, "/session", ud.sessionId, "back")
|
||||
_, err = ud.tempHttpPOST(nil, "/session", ud.sessionId, "back")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -195,7 +195,7 @@ func (ud *uiaDriver) PressKeyCode(keyCode KeyCode, metaState KeyMeta, flags ...K
|
||||
if len(flags) != 0 {
|
||||
data["flags"] = flags[0]
|
||||
}
|
||||
_, err = ud.httpPOST(data, "/session", ud.sessionId, "appium/device/press_keycode")
|
||||
_, err = ud.tempHttpPOST(data, "/session", ud.sessionId, "appium/device/press_keycode")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -212,7 +212,7 @@ func (ud *uiaDriver) TapFloat(x, y float64, options ...DataOption) (err error) {
|
||||
// new data options in post data for extra uiautomator configurations
|
||||
newData := NewData(data, options...)
|
||||
|
||||
_, err = ud.httpPOST(newData, "/session", ud.sessionId, "appium/tap")
|
||||
_, err = ud.tempHttpPOST(newData, "/session", ud.sessionId, "appium/tap")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -232,7 +232,7 @@ func (ud *uiaDriver) TouchAndHoldFloat(x, y float64, second ...float64) (err err
|
||||
"duration": int(second[0] * 1000),
|
||||
},
|
||||
}
|
||||
_, err = ud.httpPOST(data, "/session", ud.sessionId, "touch/longclick")
|
||||
_, err = ud.tempHttpPOST(data, "/session", ud.sessionId, "touch/longclick")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -256,7 +256,7 @@ func (ud *uiaDriver) DragFloat(fromX, fromY, toX, toY float64, options ...DataOp
|
||||
newData := NewData(data, options...)
|
||||
|
||||
// register(postHandler, new Drag("/wd/hub/session/:sessionId/touch/drag"))
|
||||
_, err = ud.httpPOST(newData, "/session", ud.sessionId, "touch/drag")
|
||||
_, err = ud.tempHttpPOST(newData, "/session", ud.sessionId, "touch/drag")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -280,7 +280,7 @@ func (ud *uiaDriver) SwipeFloat(fromX, fromY, toX, toY float64, options ...DataO
|
||||
// new data options in post data for extra uiautomator configurations
|
||||
newData := NewData(data, options...)
|
||||
|
||||
_, err := ud.httpPOST(newData, "/session", ud.sessionId, "touch/perform")
|
||||
_, err := ud.tempHttpPOST(newData, "/session", ud.sessionId, "touch/perform")
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -298,7 +298,7 @@ func (ud *uiaDriver) SetPasteboard(contentType PasteboardType, content string) (
|
||||
"content": base64.StdEncoding.EncodeToString([]byte(content)),
|
||||
}
|
||||
// register(postHandler, new SetClipboard("/wd/hub/session/:sessionId/appium/device/set_clipboard"))
|
||||
_, err = ud.httpPOST(data, "/session", ud.sessionId, "appium/device/set_clipboard")
|
||||
_, err = ud.tempHttpPOST(data, "/session", ud.sessionId, "appium/device/set_clipboard")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -311,7 +311,7 @@ func (ud *uiaDriver) GetPasteboard(contentType PasteboardType) (raw *bytes.Buffe
|
||||
"contentType": contentType[0],
|
||||
}
|
||||
var rawResp rawResponse
|
||||
if rawResp, err = ud.httpPOST(data, "/session", ud.sessionId, "appium/device/get_clipboard"); err != nil {
|
||||
if rawResp, err = ud.tempHttpPOST(data, "/session", ud.sessionId, "appium/device/get_clipboard"); err != nil {
|
||||
return
|
||||
}
|
||||
reply := new(struct{ Value string })
|
||||
@@ -336,7 +336,7 @@ func (ud *uiaDriver) SendKeys(text string, options ...DataOption) (err error) {
|
||||
// new data options in post data for extra uiautomator configurations
|
||||
newData := NewData(data, options...)
|
||||
|
||||
_, err = ud.httpPOST(newData, "/session", ud.sessionId, "keys")
|
||||
_, err = ud.tempHttpPOST(newData, "/session", ud.sessionId, "keys")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -347,7 +347,7 @@ func (ud *uiaDriver) Input(text string, options ...DataOption) (err error) {
|
||||
func (ud *uiaDriver) Rotation() (rotation Rotation, err error) {
|
||||
// register(getHandler, new GetRotation("/wd/hub/session/:sessionId/rotation"))
|
||||
var rawResp rawResponse
|
||||
if rawResp, err = ud.httpGET("/session", ud.sessionId, "rotation"); err != nil {
|
||||
if rawResp, err = ud.tempHttpGET("/session", ud.sessionId, "rotation"); err != nil {
|
||||
return Rotation{}, err
|
||||
}
|
||||
reply := new(struct{ Value Rotation })
|
||||
@@ -362,7 +362,7 @@ func (ud *uiaDriver) Rotation() (rotation Rotation, err error) {
|
||||
func (ud *uiaDriver) Screenshot() (raw *bytes.Buffer, err error) {
|
||||
// register(getHandler, new CaptureScreenshot("/wd/hub/session/:sessionId/screenshot"))
|
||||
var rawResp rawResponse
|
||||
if rawResp, err = ud.httpGET("/session", ud.sessionId, "screenshot"); err != nil {
|
||||
if rawResp, err = ud.tempHttpGET("/session", ud.sessionId, "screenshot"); err != nil {
|
||||
return nil, errors.Wrap(code.AndroidScreenShotError,
|
||||
fmt.Sprintf("get UIA screenshot data failed: %v", err))
|
||||
}
|
||||
@@ -384,7 +384,7 @@ func (ud *uiaDriver) Screenshot() (raw *bytes.Buffer, err error) {
|
||||
func (ud *uiaDriver) Source(srcOpt ...SourceOption) (source string, err error) {
|
||||
// register(getHandler, new Source("/wd/hub/session/:sessionId/source"))
|
||||
var rawResp rawResponse
|
||||
if rawResp, err = ud.httpGET("/session", ud.sessionId, "source"); err != nil {
|
||||
if rawResp, err = ud.tempHttpGET("/session", ud.sessionId, "source"); err != nil {
|
||||
return "", err
|
||||
}
|
||||
reply := new(struct{ Value string })
|
||||
|
||||
@@ -65,24 +65,6 @@ func (wd *Driver) httpDELETE(pathElem ...string) (rawResp rawResponse, err error
|
||||
return wd.httpRequest(http.MethodDelete, wd.concatURL(nil, pathElem...), nil)
|
||||
}
|
||||
|
||||
func (wd *Driver) tempHttpGET(pathElem ...string) (rawResp rawResponse, err error) {
|
||||
return wd.tempHttpRequest(http.MethodGet, wd.concatURL(nil, pathElem...), nil)
|
||||
}
|
||||
|
||||
func (wd *Driver) tempHttpPOST(data interface{}, pathElem ...string) (rawResp rawResponse, err error) {
|
||||
var bsJSON []byte = nil
|
||||
if data != nil {
|
||||
if bsJSON, err = json.Marshal(data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return wd.tempHttpRequest(http.MethodPost, wd.concatURL(nil, pathElem...), bsJSON)
|
||||
}
|
||||
|
||||
func (wd *Driver) tempHttpDELETE(pathElem ...string) (rawResp rawResponse, err error) {
|
||||
return wd.tempHttpRequest(http.MethodDelete, wd.concatURL(nil, pathElem...), nil)
|
||||
}
|
||||
|
||||
func (wd *Driver) httpRequest(method string, rawURL string, rawBody []byte) (rawResp rawResponse, err error) {
|
||||
log.Debug().Str("method", method).Str("url", rawURL).Str("body", string(rawBody)).Msg("request driver agent")
|
||||
|
||||
@@ -125,6 +107,24 @@ func (wd *Driver) httpRequest(method string, rawURL string, rawBody []byte) (raw
|
||||
return
|
||||
}
|
||||
|
||||
func (wd *Driver) tempHttpGET(pathElem ...string) (rawResp rawResponse, err error) {
|
||||
return wd.tempHttpRequest(http.MethodGet, wd.concatURL(nil, pathElem...), nil)
|
||||
}
|
||||
|
||||
func (wd *Driver) tempHttpPOST(data interface{}, pathElem ...string) (rawResp rawResponse, err error) {
|
||||
var bsJSON []byte = nil
|
||||
if data != nil {
|
||||
if bsJSON, err = json.Marshal(data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return wd.tempHttpRequest(http.MethodPost, wd.concatURL(nil, pathElem...), bsJSON)
|
||||
}
|
||||
|
||||
func (wd *Driver) tempHttpDELETE(pathElem ...string) (rawResp rawResponse, err error) {
|
||||
return wd.tempHttpRequest(http.MethodDelete, wd.concatURL(nil, pathElem...), nil)
|
||||
}
|
||||
|
||||
func (wd *Driver) tempHttpRequest(method string, rawURL string, rawBody []byte) (rawResp rawResponse, err error) {
|
||||
var localPort int
|
||||
{
|
||||
@@ -141,7 +141,7 @@ func (wd *Driver) tempHttpRequest(method string, rawURL string, rawBody []byte)
|
||||
tmpHTTPClient := HTTPClient
|
||||
|
||||
var resp *http.Response
|
||||
retryCount := 3
|
||||
retryCount := 5
|
||||
for retryCount > 0 {
|
||||
log.Info().Str("url", rawURL).Msg("request url")
|
||||
if req, err = http.NewRequest(method, rawURL, bytes.NewBuffer(rawBody)); err != nil {
|
||||
|
||||
@@ -11,25 +11,6 @@ import (
|
||||
"github.com/httprunner/httprunner/v4/hrp/pkg/uixt"
|
||||
)
|
||||
|
||||
// ios setting options
|
||||
var (
|
||||
WithUDID = uixt.WithUDID
|
||||
WithWDAPort = uixt.WithWDAPort
|
||||
WithWDAMjpegPort = uixt.WithWDAMjpegPort
|
||||
WithResetHomeOnStartup = uixt.WithResetHomeOnStartup
|
||||
WithSnapshotMaxDepth = uixt.WithSnapshotMaxDepth
|
||||
WithAcceptAlertButtonSelector = uixt.WithAcceptAlertButtonSelector
|
||||
WithDismissAlertButtonSelector = uixt.WithDismissAlertButtonSelector
|
||||
WithClosePopup = uixt.WithClosePopup
|
||||
)
|
||||
|
||||
// android setting options
|
||||
var (
|
||||
WithSerialNumber = uixt.WithSerialNumber
|
||||
WithAdbLogOn = uixt.WithAdbLogOn
|
||||
WithAdbClosePopup = uixt.WithAdbClosePopup
|
||||
)
|
||||
|
||||
type MobileStep struct {
|
||||
Serial string `json:"serial,omitempty" yaml:"serial,omitempty"`
|
||||
uixt.MobileAction `yaml:",inline"`
|
||||
|
||||
Reference in New Issue
Block a user