mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 07:26:55 +08:00
fix: 解决ui2不兼容的问题
This commit is contained in:
+27
-23
@@ -43,6 +43,7 @@ type UIA2Driver struct {
|
|||||||
|
|
||||||
// cache to avoid repeated query
|
// cache to avoid repeated query
|
||||||
windowSize types.Size
|
windowSize types.Size
|
||||||
|
baseURL string // store base URL for building full URLs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ud *UIA2Driver) Setup() error {
|
func (ud *UIA2Driver) Setup() error {
|
||||||
@@ -56,9 +57,12 @@ func (ud *UIA2Driver) Setup() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ud.Session.SetBaseURL(
|
|
||||||
fmt.Sprintf("http://forward-to-%d:%d/wd/hub",
|
// Store base URL for building full URLs
|
||||||
localPort, ud.Device.Options.UIA2Port))
|
ud.baseURL = fmt.Sprintf("http://forward-to-%d:%d/wd/hub",
|
||||||
|
localPort, ud.Device.Options.UIA2Port)
|
||||||
|
// Keep the old method call for backward compatibility
|
||||||
|
ud.Session.SetBaseURL(ud.baseURL)
|
||||||
|
|
||||||
// uiautomator2 server must be started before
|
// uiautomator2 server must be started before
|
||||||
|
|
||||||
@@ -108,7 +112,7 @@ func (ud *UIA2Driver) InitSession(capabilities option.Capabilities) (err error)
|
|||||||
} else {
|
} else {
|
||||||
data["capabilities"] = map[string]interface{}{"alwaysMatch": capabilities}
|
data["capabilities"] = map[string]interface{}{"alwaysMatch": capabilities}
|
||||||
}
|
}
|
||||||
if rawResp, err = ud.Session.POST(data, "/session"); err != nil {
|
if rawResp, err = ud.Session.POSTWithBaseURL(data, ud.baseURL, "/session"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
reply := new(struct{ Value struct{ SessionId string } })
|
reply := new(struct{ Value struct{ SessionId string } })
|
||||||
@@ -124,7 +128,7 @@ func (ud *UIA2Driver) DeleteSession() (err error) {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
urlStr := fmt.Sprintf("/session/%s", ud.Session.ID)
|
urlStr := fmt.Sprintf("/session/%s", ud.Session.ID)
|
||||||
if _, err = ud.Session.DELETE(urlStr); err == nil {
|
if _, err = ud.Session.DELETEWithBaseURL(ud.baseURL, urlStr); err == nil {
|
||||||
ud.Session.ID = ""
|
ud.Session.ID = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,7 +139,7 @@ func (ud *UIA2Driver) Status() (deviceStatus types.DeviceStatus, err error) {
|
|||||||
// register(getHandler, new Status("/wd/hub/status"))
|
// register(getHandler, new Status("/wd/hub/status"))
|
||||||
var rawResp DriverRawResponse
|
var rawResp DriverRawResponse
|
||||||
// Notice: use Driver.GET instead of httpGET to avoid loop calling
|
// Notice: use Driver.GET instead of httpGET to avoid loop calling
|
||||||
if rawResp, err = ud.Session.GET("/status"); err != nil {
|
if rawResp, err = ud.Session.GETWithBaseURL(ud.baseURL, "/status"); err != nil {
|
||||||
return types.DeviceStatus{Ready: false}, err
|
return types.DeviceStatus{Ready: false}, err
|
||||||
}
|
}
|
||||||
reply := new(struct {
|
reply := new(struct {
|
||||||
@@ -154,7 +158,7 @@ func (ud *UIA2Driver) DeviceInfo() (deviceInfo types.DeviceInfo, err error) {
|
|||||||
// register(getHandler, new GetDeviceInfo("/wd/hub/session/:sessionId/appium/device/info"))
|
// register(getHandler, new GetDeviceInfo("/wd/hub/session/:sessionId/appium/device/info"))
|
||||||
var rawResp DriverRawResponse
|
var rawResp DriverRawResponse
|
||||||
urlStr := fmt.Sprintf("/session/%s/appium/device/info", ud.Session.ID)
|
urlStr := fmt.Sprintf("/session/%s/appium/device/info", ud.Session.ID)
|
||||||
if rawResp, err = ud.Session.GET(urlStr); err != nil {
|
if rawResp, err = ud.Session.GETWithBaseURL(ud.baseURL, urlStr); err != nil {
|
||||||
return types.DeviceInfo{}, err
|
return types.DeviceInfo{}, err
|
||||||
}
|
}
|
||||||
reply := new(struct{ Value struct{ types.DeviceInfo } })
|
reply := new(struct{ Value struct{ types.DeviceInfo } })
|
||||||
@@ -169,7 +173,7 @@ func (ud *UIA2Driver) BatteryInfo() (batteryInfo types.BatteryInfo, err error) {
|
|||||||
// register(getHandler, new GetBatteryInfo("/wd/hub/session/:sessionId/appium/device/battery_info"))
|
// register(getHandler, new GetBatteryInfo("/wd/hub/session/:sessionId/appium/device/battery_info"))
|
||||||
var rawResp DriverRawResponse
|
var rawResp DriverRawResponse
|
||||||
urlStr := fmt.Sprintf("/session/%s/appium/device/battery_info", ud.Session.ID)
|
urlStr := fmt.Sprintf("/session/%s/appium/device/battery_info", ud.Session.ID)
|
||||||
if rawResp, err = ud.Session.GET(urlStr); err != nil {
|
if rawResp, err = ud.Session.GETWithBaseURL(ud.baseURL, urlStr); err != nil {
|
||||||
return types.BatteryInfo{}, err
|
return types.BatteryInfo{}, err
|
||||||
}
|
}
|
||||||
reply := new(struct{ Value struct{ types.BatteryInfo } })
|
reply := new(struct{ Value struct{ types.BatteryInfo } })
|
||||||
@@ -192,7 +196,7 @@ func (ud *UIA2Driver) WindowSize() (size types.Size, err error) {
|
|||||||
|
|
||||||
var rawResp DriverRawResponse
|
var rawResp DriverRawResponse
|
||||||
urlStr := fmt.Sprintf("/session/%s/window/:windowHandle/size", ud.Session.ID)
|
urlStr := fmt.Sprintf("/session/%s/window/:windowHandle/size", ud.Session.ID)
|
||||||
if rawResp, err = ud.Session.GET(urlStr); err != nil {
|
if rawResp, err = ud.Session.GETWithBaseURL(ud.baseURL, urlStr); err != nil {
|
||||||
return types.Size{}, errors.Wrap(err, "get window size failed by UIA2 request")
|
return types.Size{}, errors.Wrap(err, "get window size failed by UIA2 request")
|
||||||
}
|
}
|
||||||
reply := new(struct{ Value struct{ types.Size } })
|
reply := new(struct{ Value struct{ types.Size } })
|
||||||
@@ -220,7 +224,7 @@ func (ud *UIA2Driver) Back() (err error) {
|
|||||||
log.Info().Msg("UIA2Driver.Back")
|
log.Info().Msg("UIA2Driver.Back")
|
||||||
// register(postHandler, new PressBack("/wd/hub/session/:sessionId/back"))
|
// register(postHandler, new PressBack("/wd/hub/session/:sessionId/back"))
|
||||||
urlStr := fmt.Sprintf("/session/%s/back", ud.Session.ID)
|
urlStr := fmt.Sprintf("/session/%s/back", ud.Session.ID)
|
||||||
_, err = ud.Session.POST(nil, urlStr)
|
_, err = ud.Session.POSTWithBaseURL(nil, ud.baseURL, urlStr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,7 +240,7 @@ func (ud *UIA2Driver) PressKeyCodes(keyCode KeyCode, metaState KeyMeta, flags ..
|
|||||||
data["flags"] = flags[0]
|
data["flags"] = flags[0]
|
||||||
}
|
}
|
||||||
urlStr := fmt.Sprintf("/session/%s/appium/device/press_keycode", ud.Session.ID)
|
urlStr := fmt.Sprintf("/session/%s/appium/device/press_keycode", ud.Session.ID)
|
||||||
_, err = ud.Session.POST(data, urlStr)
|
_, err = ud.Session.POSTWithBaseURL(data, ud.baseURL, urlStr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,7 +248,7 @@ func (ud *UIA2Driver) Orientation() (orientation types.Orientation, err error) {
|
|||||||
// [[FBRoute GET:@"/orientation"] respondWithTarget:self action:@selector(handleGetOrientation:)]
|
// [[FBRoute GET:@"/orientation"] respondWithTarget:self action:@selector(handleGetOrientation:)]
|
||||||
var rawResp DriverRawResponse
|
var rawResp DriverRawResponse
|
||||||
urlStr := fmt.Sprintf("/session/%s/orientation", ud.Session.ID)
|
urlStr := fmt.Sprintf("/session/%s/orientation", ud.Session.ID)
|
||||||
if rawResp, err = ud.Session.GET(urlStr); err != nil {
|
if rawResp, err = ud.Session.GETWithBaseURL(ud.baseURL, urlStr); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
reply := new(struct{ Value types.Orientation })
|
reply := new(struct{ Value types.Orientation })
|
||||||
@@ -282,7 +286,7 @@ func (ud *UIA2Driver) DoubleTap(x, y float64, opts ...option.ActionOption) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
urlStr := fmt.Sprintf("/session/%s/actions/tap", ud.Session.ID)
|
urlStr := fmt.Sprintf("/session/%s/actions/tap", ud.Session.ID)
|
||||||
_, err = ud.Session.POST(data, urlStr)
|
_, err = ud.Session.POSTWithBaseURL(data, ud.baseURL, urlStr)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -329,7 +333,7 @@ func (ud *UIA2Driver) TapAbsXY(x, y float64, opts ...option.ActionOption) error
|
|||||||
option.MergeOptions(data, opts...)
|
option.MergeOptions(data, opts...)
|
||||||
|
|
||||||
urlStr := fmt.Sprintf("/session/%s/actions/tap", ud.Session.ID)
|
urlStr := fmt.Sprintf("/session/%s/actions/tap", ud.Session.ID)
|
||||||
_, err = ud.Session.POST(data, urlStr)
|
_, err = ud.Session.POSTWithBaseURL(data, ud.baseURL, urlStr)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -350,7 +354,7 @@ func (ud *UIA2Driver) TouchAndHold(x, y float64, opts ...option.ActionOption) (e
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
urlStr := fmt.Sprintf("/session/%s/touch/longclick", ud.Session.ID)
|
urlStr := fmt.Sprintf("/session/%s/touch/longclick", ud.Session.ID)
|
||||||
_, err = ud.Session.POST(data, urlStr)
|
_, err = ud.Session.POSTWithBaseURL(data, ud.baseURL, urlStr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -379,7 +383,7 @@ func (ud *UIA2Driver) Drag(fromX, fromY, toX, toY float64, opts ...option.Action
|
|||||||
|
|
||||||
// register(postHandler, new Drag("/wd/hub/session/:sessionId/touch/drag"))
|
// register(postHandler, new Drag("/wd/hub/session/:sessionId/touch/drag"))
|
||||||
urlStr := fmt.Sprintf("/session/%s/touch/drag", ud.Session.ID)
|
urlStr := fmt.Sprintf("/session/%s/touch/drag", ud.Session.ID)
|
||||||
_, err = ud.Session.POST(data, urlStr)
|
_, err = ud.Session.POSTWithBaseURL(data, ud.baseURL, urlStr)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -422,7 +426,7 @@ func (ud *UIA2Driver) Swipe(fromX, fromY, toX, toY float64, opts ...option.Actio
|
|||||||
option.MergeOptions(data, opts...)
|
option.MergeOptions(data, opts...)
|
||||||
|
|
||||||
urlStr := fmt.Sprintf("/session/%s/actions/swipe", ud.Session.ID)
|
urlStr := fmt.Sprintf("/session/%s/actions/swipe", ud.Session.ID)
|
||||||
_, err = ud.Session.POST(data, urlStr)
|
_, err = ud.Session.POSTWithBaseURL(data, ud.baseURL, urlStr)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -443,7 +447,7 @@ func (ud *UIA2Driver) SetPasteboard(contentType types.PasteboardType, content st
|
|||||||
}
|
}
|
||||||
// register(postHandler, new SetClipboard("/wd/hub/session/:sessionId/appium/device/set_clipboard"))
|
// register(postHandler, new SetClipboard("/wd/hub/session/:sessionId/appium/device/set_clipboard"))
|
||||||
urlStr := fmt.Sprintf("/session/%s/appium/device/set_clipboard", ud.Session.ID)
|
urlStr := fmt.Sprintf("/session/%s/appium/device/set_clipboard", ud.Session.ID)
|
||||||
_, err = ud.Session.POST(data, urlStr)
|
_, err = ud.Session.POSTWithBaseURL(data, ud.baseURL, urlStr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -457,7 +461,7 @@ func (ud *UIA2Driver) GetPasteboard(contentType types.PasteboardType) (raw *byte
|
|||||||
}
|
}
|
||||||
var rawResp DriverRawResponse
|
var rawResp DriverRawResponse
|
||||||
urlStr := fmt.Sprintf("/session/%s/appium/device/get_clipboard", ud.Session.ID)
|
urlStr := fmt.Sprintf("/session/%s/appium/device/get_clipboard", ud.Session.ID)
|
||||||
if rawResp, err = ud.Session.POST(data, urlStr); err != nil {
|
if rawResp, err = ud.Session.POSTWithBaseURL(data, ud.baseURL, urlStr); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
reply := new(struct{ Value string })
|
reply := new(struct{ Value string })
|
||||||
@@ -488,7 +492,7 @@ func (ud *UIA2Driver) Input(text string, opts ...option.ActionOption) (err error
|
|||||||
}
|
}
|
||||||
option.MergeOptions(data, opts...)
|
option.MergeOptions(data, opts...)
|
||||||
urlStr := fmt.Sprintf("/session/%s/keys", ud.Session.ID)
|
urlStr := fmt.Sprintf("/session/%s/keys", ud.Session.ID)
|
||||||
_, err = ud.Session.POST(data, urlStr)
|
_, err = ud.Session.POSTWithBaseURL(data, ud.baseURL, urlStr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -546,7 +550,7 @@ func (ud *UIA2Driver) SendActionKey(text string, opts ...option.ActionOption) (e
|
|||||||
option.MergeOptions(data, opts...)
|
option.MergeOptions(data, opts...)
|
||||||
|
|
||||||
urlStr := fmt.Sprintf("/session/%s/actions/keys", ud.Session.ID)
|
urlStr := fmt.Sprintf("/session/%s/actions/keys", ud.Session.ID)
|
||||||
_, err = ud.Session.POST(data, urlStr)
|
_, err = ud.Session.POSTWithBaseURL(data, ud.baseURL, urlStr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -554,7 +558,7 @@ func (ud *UIA2Driver) Rotation() (rotation types.Rotation, err error) {
|
|||||||
// register(getHandler, new GetRotation("/wd/hub/session/:sessionId/rotation"))
|
// register(getHandler, new GetRotation("/wd/hub/session/:sessionId/rotation"))
|
||||||
var rawResp DriverRawResponse
|
var rawResp DriverRawResponse
|
||||||
urlStr := fmt.Sprintf("/session/%s/rotation", ud.Session.ID)
|
urlStr := fmt.Sprintf("/session/%s/rotation", ud.Session.ID)
|
||||||
if rawResp, err = ud.Session.GET(urlStr); err != nil {
|
if rawResp, err = ud.Session.GETWithBaseURL(ud.baseURL, urlStr); err != nil {
|
||||||
return types.Rotation{}, err
|
return types.Rotation{}, err
|
||||||
}
|
}
|
||||||
reply := new(struct{ Value types.Rotation })
|
reply := new(struct{ Value types.Rotation })
|
||||||
@@ -576,7 +580,7 @@ func (ud *UIA2Driver) Source(srcOpt ...option.SourceOption) (source string, err
|
|||||||
// register(getHandler, new Source("/wd/hub/session/:sessionId/source"))
|
// register(getHandler, new Source("/wd/hub/session/:sessionId/source"))
|
||||||
var rawResp DriverRawResponse
|
var rawResp DriverRawResponse
|
||||||
urlStr := fmt.Sprintf("/session/%s/source", ud.Session.ID)
|
urlStr := fmt.Sprintf("/session/%s/source", ud.Session.ID)
|
||||||
if rawResp, err = ud.Session.GET(urlStr); err != nil {
|
if rawResp, err = ud.Session.GETWithBaseURL(ud.baseURL, urlStr); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
reply := new(struct{ Value string })
|
reply := new(struct{ Value string })
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ func NewDriverSession() *DriverSession {
|
|||||||
timeout := 30 * time.Second
|
timeout := 30 * time.Second
|
||||||
session := &DriverSession{
|
session := &DriverSession{
|
||||||
ctx: context.Background(),
|
ctx: context.Background(),
|
||||||
|
ID: "<SessionNotInit>",
|
||||||
timeout: timeout,
|
timeout: timeout,
|
||||||
client: &http.Client{
|
client: &http.Client{
|
||||||
Timeout: timeout,
|
Timeout: timeout,
|
||||||
@@ -53,6 +54,7 @@ func NewDriverSession() *DriverSession {
|
|||||||
|
|
||||||
type DriverSession struct {
|
type DriverSession struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
|
ID string
|
||||||
client *http.Client
|
client *http.Client
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
maxRetry int
|
maxRetry int
|
||||||
|
|||||||
Reference in New Issue
Block a user