fix: 解决ui2不兼容的问题

This commit is contained in:
余泓铮
2025-06-26 11:04:09 +08:00
parent a31a8c67f0
commit 552aa516b9
2 changed files with 29 additions and 23 deletions

View File

@@ -43,6 +43,7 @@ type UIA2Driver struct {
// cache to avoid repeated query
windowSize types.Size
baseURL string // store base URL for building full URLs
}
func (ud *UIA2Driver) Setup() error {
@@ -56,9 +57,12 @@ func (ud *UIA2Driver) Setup() error {
if err != nil {
return err
}
ud.Session.SetBaseURL(
fmt.Sprintf("http://forward-to-%d:%d/wd/hub",
localPort, ud.Device.Options.UIA2Port))
// Store base URL for building full URLs
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
@@ -108,7 +112,7 @@ func (ud *UIA2Driver) InitSession(capabilities option.Capabilities) (err error)
} else {
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
}
reply := new(struct{ Value struct{ SessionId string } })
@@ -124,7 +128,7 @@ func (ud *UIA2Driver) DeleteSession() (err error) {
return nil
}
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 = ""
}
@@ -135,7 +139,7 @@ func (ud *UIA2Driver) Status() (deviceStatus types.DeviceStatus, err error) {
// register(getHandler, new Status("/wd/hub/status"))
var rawResp DriverRawResponse
// 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
}
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"))
var rawResp DriverRawResponse
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
}
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"))
var rawResp DriverRawResponse
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
}
reply := new(struct{ Value struct{ types.BatteryInfo } })
@@ -192,7 +196,7 @@ func (ud *UIA2Driver) WindowSize() (size types.Size, err error) {
var rawResp DriverRawResponse
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")
}
reply := new(struct{ Value struct{ types.Size } })
@@ -220,7 +224,7 @@ func (ud *UIA2Driver) Back() (err error) {
log.Info().Msg("UIA2Driver.Back")
// register(postHandler, new PressBack("/wd/hub/session/:sessionId/back"))
urlStr := fmt.Sprintf("/session/%s/back", ud.Session.ID)
_, err = ud.Session.POST(nil, urlStr)
_, err = ud.Session.POSTWithBaseURL(nil, ud.baseURL, urlStr)
return
}
@@ -236,7 +240,7 @@ func (ud *UIA2Driver) PressKeyCodes(keyCode KeyCode, metaState KeyMeta, flags ..
data["flags"] = flags[0]
}
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
}
@@ -244,7 +248,7 @@ func (ud *UIA2Driver) Orientation() (orientation types.Orientation, err error) {
// [[FBRoute GET:@"/orientation"] respondWithTarget:self action:@selector(handleGetOrientation:)]
var rawResp DriverRawResponse
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
}
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)
_, err = ud.Session.POST(data, urlStr)
_, err = ud.Session.POSTWithBaseURL(data, ud.baseURL, urlStr)
return err
}
@@ -329,7 +333,7 @@ func (ud *UIA2Driver) TapAbsXY(x, y float64, opts ...option.ActionOption) error
option.MergeOptions(data, opts...)
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
}
@@ -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)
_, err = ud.Session.POST(data, urlStr)
_, err = ud.Session.POSTWithBaseURL(data, ud.baseURL, urlStr)
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"))
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
}
@@ -422,7 +426,7 @@ func (ud *UIA2Driver) Swipe(fromX, fromY, toX, toY float64, opts ...option.Actio
option.MergeOptions(data, opts...)
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
}
@@ -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"))
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
}
@@ -457,7 +461,7 @@ func (ud *UIA2Driver) GetPasteboard(contentType types.PasteboardType) (raw *byte
}
var rawResp DriverRawResponse
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
}
reply := new(struct{ Value string })
@@ -488,7 +492,7 @@ func (ud *UIA2Driver) Input(text string, opts ...option.ActionOption) (err error
}
option.MergeOptions(data, opts...)
urlStr := fmt.Sprintf("/session/%s/keys", ud.Session.ID)
_, err = ud.Session.POST(data, urlStr)
_, err = ud.Session.POSTWithBaseURL(data, ud.baseURL, urlStr)
return
}
@@ -546,7 +550,7 @@ func (ud *UIA2Driver) SendActionKey(text string, opts ...option.ActionOption) (e
option.MergeOptions(data, opts...)
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
}
@@ -554,7 +558,7 @@ func (ud *UIA2Driver) Rotation() (rotation types.Rotation, err error) {
// register(getHandler, new GetRotation("/wd/hub/session/:sessionId/rotation"))
var rawResp DriverRawResponse
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
}
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"))
var rawResp DriverRawResponse
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
}
reply := new(struct{ Value string })

View File

@@ -41,6 +41,7 @@ func NewDriverSession() *DriverSession {
timeout := 30 * time.Second
session := &DriverSession{
ctx: context.Background(),
ID: "<SessionNotInit>",
timeout: timeout,
client: &http.Client{
Timeout: timeout,
@@ -53,6 +54,7 @@ func NewDriverSession() *DriverSession {
type DriverSession struct {
ctx context.Context
ID string
client *http.Client
timeout time.Duration
maxRetry int