refactor: update URL handling in DriverSession and WDADriver, removing base URL dependency for HTTP requests

This commit is contained in:
lilong.129
2025-06-26 14:37:41 +08:00
parent 88d255bce1
commit 3e4858db44
4 changed files with 144 additions and 129 deletions

View File

@@ -1 +1 @@
v5.0.0-beta-2506261418 v5.0.0-beta-2506261437

View File

@@ -103,8 +103,11 @@ func (s *DriverSession) History() []*DriverRequests {
} }
func (s *DriverSession) buildURL(urlStr string) (string, error) { func (s *DriverSession) buildURL(urlStr string) (string, error) {
if urlStr == "" { if urlStr == "" || urlStr == "/" {
return "", fmt.Errorf("URL cannot be empty") if s.baseUrl == "" {
return "", fmt.Errorf("base URL is empty")
}
return s.baseUrl, nil
} }
// Handle full URLs // Handle full URLs
@@ -116,26 +119,40 @@ func (s *DriverSession) buildURL(urlStr string) (string, error) {
return u.String(), nil return u.String(), nil
} }
// For relative paths, return as-is (caller should provide full URL) // handle relative path using ResolveReference
return urlStr, nil if s.baseUrl == "" {
return "", fmt.Errorf("base URL is empty")
}
baseURL, err := url.Parse(s.baseUrl)
if err != nil {
return "", fmt.Errorf("failed to parse base URL: %w", err)
}
relativeURL, err := url.Parse(urlStr)
if err != nil {
return "", fmt.Errorf("failed to parse relative URL: %w", err)
}
finalURL := baseURL.ResolveReference(relativeURL)
return finalURL.String(), nil
} }
func (s *DriverSession) GET(fullURL string) (rawResp DriverRawResponse, err error) { func (s *DriverSession) GET(urlStr string) (rawResp DriverRawResponse, err error) {
return s.RequestWithRetry(http.MethodGet, fullURL, nil) return s.RequestWithRetry(http.MethodGet, urlStr, nil)
} }
func (s *DriverSession) POST(data interface{}, fullURL string) (rawResp DriverRawResponse, err error) { func (s *DriverSession) POST(data interface{}, urlStr string) (rawResp DriverRawResponse, err error) {
var bsJSON []byte = nil var bsJSON []byte = nil
if data != nil { if data != nil {
if bsJSON, err = json.Marshal(data); err != nil { if bsJSON, err = json.Marshal(data); err != nil {
return nil, err return nil, err
} }
} }
return s.RequestWithRetry(http.MethodPost, fullURL, bsJSON) return s.RequestWithRetry(http.MethodPost, urlStr, bsJSON)
} }
func (s *DriverSession) DELETE(fullURL string) (rawResp DriverRawResponse, err error) { func (s *DriverSession) DELETE(urlStr string) (rawResp DriverRawResponse, err error) {
return s.RequestWithRetry(http.MethodDelete, fullURL, nil) return s.RequestWithRetry(http.MethodDelete, urlStr, nil)
} }
func (s *DriverSession) RequestWithRetry(method string, urlStr string, rawBody []byte) ( func (s *DriverSession) RequestWithRetry(method string, urlStr string, rawBody []byte) (

View File

@@ -9,17 +9,33 @@ import (
func TestDriverSession_buildURL(t *testing.T) { func TestDriverSession_buildURL(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
baseURL string
urlStr string urlStr string
want string want string
wantErr bool wantErr bool
errMsg string errMsg string
}{ }{
// Error cases
{ {
name: "empty url", name: "empty url without baseUrl",
urlStr: "", urlStr: "",
wantErr: true, wantErr: true,
errMsg: "URL cannot be empty", errMsg: "base URL is empty",
}, },
{
name: "relative path without baseUrl",
urlStr: "/api/users",
wantErr: true,
errMsg: "base URL is empty",
},
{
name: "invalid absolute url",
urlStr: "http://[invalid-url",
wantErr: true,
errMsg: "failed to parse URL",
},
// Absolute URLs (no baseURL needed)
{ {
name: "absolute http url", name: "absolute http url",
urlStr: "http://example.com/api", urlStr: "http://example.com/api",
@@ -30,27 +46,63 @@ func TestDriverSession_buildURL(t *testing.T) {
urlStr: "https://example.com/api", urlStr: "https://example.com/api",
want: "https://example.com/api", want: "https://example.com/api",
}, },
// Empty/root path with baseURL
{ {
name: "invalid absolute url", name: "empty url with baseUrl",
urlStr: "http://[invalid-url", baseURL: "http://localhost:8080",
wantErr: true, urlStr: "",
errMsg: "failed to parse URL", want: "http://localhost:8080",
}, },
{ {
name: "relative path", name: "root path with baseUrl",
urlStr: "/api/users", baseURL: "http://localhost:8080",
want: "/api/users", urlStr: "/",
want: "http://localhost:8080",
},
// Relative paths with baseURL
{
name: "relative path with leading slash",
baseURL: "http://localhost:8080",
urlStr: "/api/users",
want: "http://localhost:8080/api/users",
}, },
{ {
name: "relative path with query params", name: "relative path without leading slash",
urlStr: "/api/users?id=1&name=test", baseURL: "http://localhost:8080/api",
want: "/api/users?id=1&name=test", urlStr: "users",
want: "http://localhost:8080/users",
},
{
name: "relative path with query params",
baseURL: "http://localhost:8080",
urlStr: "/api/users?id=1&name=test",
want: "http://localhost:8080/api/users?id=1&name=test",
},
// BaseURL with path scenarios
{
name: "baseUrl with path + relative path",
baseURL: "http://localhost:8080/api/v1",
urlStr: "users",
want: "http://localhost:8080/api/users",
},
{
name: "baseUrl with trailing slash + relative path",
baseURL: "http://localhost:8080/api/",
urlStr: "users",
want: "http://localhost:8080/api/users",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
s := NewDriverSession() s := NewDriverSession()
if tt.baseURL != "" {
s.SetBaseURL(tt.baseURL)
}
got, err := s.buildURL(tt.urlStr) got, err := s.buildURL(tt.urlStr)
if tt.wantErr { if tt.wantErr {
@@ -65,61 +117,8 @@ func TestDriverSession_buildURL(t *testing.T) {
} }
} }
func TestDriverSession_WithBaseURL(t *testing.T) {
tests := []struct {
name string
baseURL string
path string
want string
}{
{
name: "base url with root path",
baseURL: "http://localhost:8080",
path: "/",
want: "http://localhost:8080/",
},
{
name: "base url with api path",
baseURL: "http://localhost:8080",
path: "/api/users",
want: "http://localhost:8080/api/users",
},
{
name: "base url with path and query params",
baseURL: "http://localhost:8080",
path: "/api/users?id=1&name=test",
want: "http://localhost:8080/api/users?id=1&name=test",
},
{
name: "base url with trailing slash and path",
baseURL: "http://localhost:8080/",
path: "api/users",
want: "http://localhost:8080/api/users",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := NewDriverSession()
// Test that the URL concatenation works as expected
fullURL := tt.baseURL + tt.path
assert.Equal(t, tt.want, fullURL)
// Test that buildURL handles the resulting full URL correctly
if fullURL != "" {
got, err := s.buildURL(fullURL)
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
}
})
}
}
func TestDriverSession(t *testing.T) { func TestDriverSession(t *testing.T) {
session := NewDriverSession() session := NewDriverSession()
// Test backward compatibility with SetBaseURL (should not error)
session.SetBaseURL("https://postman-echo.com") session.SetBaseURL("https://postman-echo.com")
// Test GET with full URL // Test GET with full URL
@@ -132,21 +131,22 @@ func TestDriverSession(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
t.Log(resp) t.Log(resp)
// Test GETWithBaseURL // Test GET with relative path (using baseURL)
baseURL := "https://postman-echo.com" resp, err = session.GET("/get")
resp, err = session.GET(baseURL + "/get")
assert.Nil(t, err) assert.Nil(t, err)
t.Log(resp) t.Log(resp)
// Verify request history
driverRequests := session.History() driverRequests := session.History()
assert.Equal(t, 3, len(driverRequests)) assert.Equal(t, 3, len(driverRequests))
// Test reset functionality
session.Reset() session.Reset()
driverRequests = session.History() driverRequests = session.History()
assert.Equal(t, 0, len(driverRequests)) assert.Equal(t, 0, len(driverRequests))
// Test POST with base URL and path // Test POST with relative path
resp, err = session.POST(nil, baseURL+"/post") resp, err = session.POST(nil, "/post")
assert.Nil(t, err) assert.Nil(t, err)
t.Log(resp) t.Log(resp)

View File

@@ -59,7 +59,6 @@ type WDADriver struct {
mjpegClient *http.Client mjpegClient *http.Client
mjpegUrl string mjpegUrl string
baseURL string // store base URL for building full URLs
} }
func (wd *WDADriver) getLocalPort() (int, error) { func (wd *WDADriver) getLocalPort() (int, error) {
@@ -140,9 +139,8 @@ func (wd *WDADriver) Setup() error {
} }
// Store base URL for building full URLs // Store base URL for building full URLs
wd.baseURL = fmt.Sprintf("http://localhost:%d", localPort) baseURL := fmt.Sprintf("http://localhost:%d", localPort)
// Keep the old method call for backward compatibility wd.Session.SetBaseURL(baseURL)
wd.Session.SetBaseURL(wd.baseURL)
if err = wd.initMjpegClient(); err != nil { if err = wd.initMjpegClient(); err != nil {
return err return err
@@ -190,7 +188,7 @@ func (wd *WDADriver) Status() (deviceStatus types.DeviceStatus, err error) {
// [[FBRoute GET:@"/status"].withoutSession respondWithTarget:self action:@selector(handleGetStatus:)] // [[FBRoute GET:@"/status"].withoutSession respondWithTarget:self action:@selector(handleGetStatus:)]
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 = wd.Session.GET(wd.baseURL + "/status"); err != nil { if rawResp, err = wd.Session.GET("/status"); err != nil {
return types.DeviceStatus{}, err return types.DeviceStatus{}, err
} }
reply := new(struct{ Value struct{ types.DeviceStatus } }) reply := new(struct{ Value struct{ types.DeviceStatus } })
@@ -209,7 +207,7 @@ func (wd *WDADriver) DeviceInfo() (deviceInfo types.DeviceInfo, err error) {
// [[FBRoute GET:@"/wda/device/info"] respondWithTarget:self action:@selector(handleGetDeviceInfo:)] // [[FBRoute GET:@"/wda/device/info"] respondWithTarget:self action:@selector(handleGetDeviceInfo:)]
// [[FBRoute GET:@"/wda/device/info"].withoutSession // [[FBRoute GET:@"/wda/device/info"].withoutSession
var rawResp DriverRawResponse var rawResp DriverRawResponse
if rawResp, err = wd.Session.GET(wd.baseURL + "/wda/device/info"); err != nil { if rawResp, err = wd.Session.GET("/wda/device/info"); err != nil {
return types.DeviceInfo{}, err return types.DeviceInfo{}, err
} }
reply := new(struct{ Value struct{ types.DeviceInfo } }) reply := new(struct{ Value struct{ types.DeviceInfo } })
@@ -224,7 +222,7 @@ func (wd *WDADriver) Location() (location types.Location, err error) {
// [[FBRoute GET:@"/wda/device/location"] respondWithTarget:self action:@selector(handleGetLocation:)] // [[FBRoute GET:@"/wda/device/location"] respondWithTarget:self action:@selector(handleGetLocation:)]
// [[FBRoute GET:@"/wda/device/location"].withoutSession // [[FBRoute GET:@"/wda/device/location"].withoutSession
var rawResp DriverRawResponse var rawResp DriverRawResponse
if rawResp, err = wd.Session.GET(wd.baseURL + "/wda/device/location"); err != nil { if rawResp, err = wd.Session.GET("/wda/device/location"); err != nil {
return types.Location{}, err return types.Location{}, err
} }
reply := new(struct{ Value struct{ types.Location } }) reply := new(struct{ Value struct{ types.Location } })
@@ -238,7 +236,7 @@ func (wd *WDADriver) Location() (location types.Location, err error) {
func (wd *WDADriver) BatteryInfo() (batteryInfo types.BatteryInfo, err error) { func (wd *WDADriver) BatteryInfo() (batteryInfo types.BatteryInfo, err error) {
// [[FBRoute GET:@"/wda/batteryInfo"] respondWithTarget:self action:@selector(handleGetBatteryInfo:)] // [[FBRoute GET:@"/wda/batteryInfo"] respondWithTarget:self action:@selector(handleGetBatteryInfo:)]
var rawResp DriverRawResponse var rawResp DriverRawResponse
if rawResp, err = wd.Session.GET(wd.baseURL + "/wda/batteryInfo"); err != nil { if rawResp, err = wd.Session.GET("/wda/batteryInfo"); err != nil {
return types.BatteryInfo{}, err return types.BatteryInfo{}, err
} }
reply := new(struct{ Value struct{ types.BatteryInfo } }) reply := new(struct{ Value struct{ types.BatteryInfo } })
@@ -257,7 +255,7 @@ func (wd *WDADriver) WindowSize() (size types.Size, err error) {
} }
var rawResp DriverRawResponse var rawResp DriverRawResponse
if rawResp, err = wd.Session.GET(wd.baseURL + "/wings/window/size"); err != nil { if rawResp, err = wd.Session.GET("/wings/window/size"); err != nil {
return types.Size{}, errors.Wrap(err, "get window size failed by WDA request") return types.Size{}, errors.Wrap(err, "get window size failed by WDA request")
} }
reply := new(struct{ Value struct{ types.Size } }) reply := new(struct{ Value struct{ types.Size } })
@@ -296,7 +294,7 @@ type Screen struct {
func (wd *WDADriver) Screen() (screen Screen, err error) { func (wd *WDADriver) Screen() (screen Screen, err error) {
// [[FBRoute GET:@"/wda/screen"] respondWithTarget:self action:@selector(handleGetScreen:)] // [[FBRoute GET:@"/wda/screen"] respondWithTarget:self action:@selector(handleGetScreen:)]
var rawResp DriverRawResponse var rawResp DriverRawResponse
if rawResp, err = wd.Session.GET(wd.baseURL + "/wda/screen"); err != nil { if rawResp, err = wd.Session.GET("/wda/screen"); err != nil {
return Screen{}, err return Screen{}, err
} }
reply := new(struct{ Value struct{ Screen } }) reply := new(struct{ Value struct{ Screen } })
@@ -310,7 +308,7 @@ func (wd *WDADriver) Screen() (screen Screen, err error) {
func (wd *WDADriver) ScreenShot(opts ...option.ActionOption) (raw *bytes.Buffer, err error) { func (wd *WDADriver) ScreenShot(opts ...option.ActionOption) (raw *bytes.Buffer, err error) {
// [[FBRoute GET:@"/screenshot"] respondWithTarget:self action:@selector(handleGetScreenshot:)] // [[FBRoute GET:@"/screenshot"] respondWithTarget:self action:@selector(handleGetScreenshot:)]
// [[FBRoute GET:@"/screenshot"].withoutSession respondWithTarget:self action:@selector(handleGetScreenshot:)] // [[FBRoute GET:@"/screenshot"].withoutSession respondWithTarget:self action:@selector(handleGetScreenshot:)]
rawResp, err := wd.Session.GET(wd.baseURL + "/screenshot") rawResp, err := wd.Session.GET("/screenshot")
if err != nil { if err != nil {
return nil, errors.Wrap(code.DeviceScreenShotError, return nil, errors.Wrap(code.DeviceScreenShotError,
fmt.Sprintf("WDA screenshot failed %v", err)) fmt.Sprintf("WDA screenshot failed %v", err))
@@ -338,7 +336,7 @@ func (wd *WDADriver) ActiveAppInfo() (info types.AppInfo, err error) {
// [[FBRoute GET:@"/wda/activeAppInfo"] respondWithTarget:self action:@selector(handleActiveAppInfo:)] // [[FBRoute GET:@"/wda/activeAppInfo"] respondWithTarget:self action:@selector(handleActiveAppInfo:)]
// [[FBRoute GET:@"/wda/activeAppInfo"].withoutSession // [[FBRoute GET:@"/wda/activeAppInfo"].withoutSession
var rawResp DriverRawResponse var rawResp DriverRawResponse
if rawResp, err = wd.Session.GET(wd.baseURL + "/wda/activeAppInfo"); err != nil { if rawResp, err = wd.Session.GET("/wda/activeAppInfo"); err != nil {
return types.AppInfo{}, err return types.AppInfo{}, err
} }
reply := new(struct{ Value struct{ types.AppInfo } }) reply := new(struct{ Value struct{ types.AppInfo } })
@@ -352,7 +350,7 @@ func (wd *WDADriver) ActiveAppInfo() (info types.AppInfo, err error) {
func (wd *WDADriver) ActiveAppsList() (appsList []types.AppBaseInfo, err error) { func (wd *WDADriver) ActiveAppsList() (appsList []types.AppBaseInfo, err error) {
// [[FBRoute GET:@"/wda/apps/list"] respondWithTarget:self action:@selector(handleGetActiveAppsList:)] // [[FBRoute GET:@"/wda/apps/list"] respondWithTarget:self action:@selector(handleGetActiveAppsList:)]
var rawResp DriverRawResponse var rawResp DriverRawResponse
if rawResp, err = wd.Session.GET(wd.baseURL + "/wda/apps/list"); err != nil { if rawResp, err = wd.Session.GET("/wda/apps/list"); err != nil {
return nil, err return nil, err
} }
reply := new(struct{ Value []types.AppBaseInfo }) reply := new(struct{ Value []types.AppBaseInfo })
@@ -372,7 +370,7 @@ func (wd *WDADriver) IsLocked() (locked bool, err error) {
// [[FBRoute GET:@"/wda/locked"] respondWithTarget:self action:@selector(handleIsLocked:)] // [[FBRoute GET:@"/wda/locked"] respondWithTarget:self action:@selector(handleIsLocked:)]
// [[FBRoute GET:@"/wda/locked"].withoutSession // [[FBRoute GET:@"/wda/locked"].withoutSession
var rawResp DriverRawResponse var rawResp DriverRawResponse
if rawResp, err = wd.Session.GET(wd.baseURL + "/wda/locked"); err != nil { if rawResp, err = wd.Session.GET("/wda/locked"); err != nil {
return false, err return false, err
} }
if locked, err = rawResp.ValueConvertToBool(); err != nil { if locked, err = rawResp.ValueConvertToBool(); err != nil {
@@ -384,20 +382,20 @@ func (wd *WDADriver) IsLocked() (locked bool, err error) {
func (wd *WDADriver) Unlock() (err error) { func (wd *WDADriver) Unlock() (err error) {
// [[FBRoute POST:@"/wda/unlock"] respondWithTarget:self action:@selector(handleUnlock:)] // [[FBRoute POST:@"/wda/unlock"] respondWithTarget:self action:@selector(handleUnlock:)]
// [[FBRoute POST:@"/wda/unlock"].withoutSession // [[FBRoute POST:@"/wda/unlock"].withoutSession
_, err = wd.Session.POST(nil, wd.baseURL+"/wda/unlock") _, err = wd.Session.POST(nil, "/wda/unlock")
return return
} }
func (wd *WDADriver) Lock() (err error) { func (wd *WDADriver) Lock() (err error) {
// [[FBRoute POST:@"/wda/lock"] respondWithTarget:self action:@selector(handleLock:)] // [[FBRoute POST:@"/wda/lock"] respondWithTarget:self action:@selector(handleLock:)]
// [[FBRoute POST:@"/wda/lock"].withoutSession // [[FBRoute POST:@"/wda/lock"].withoutSession
_, err = wd.Session.POST(nil, wd.baseURL+"/wda/lock") _, err = wd.Session.POST(nil, "/wda/lock")
return return
} }
func (wd *WDADriver) Home() (err error) { func (wd *WDADriver) Home() (err error) {
// [[FBRoute POST:@"/wda/homescreen"].withoutSession respondWithTarget:self action:@selector(handleHomescreenCommand:)] // [[FBRoute POST:@"/wda/homescreen"].withoutSession respondWithTarget:self action:@selector(handleHomescreenCommand:)]
_, err = wd.Session.POST(nil, wd.baseURL+"/wda/homescreen") _, err = wd.Session.POST(nil, "/wda/homescreen")
return return
} }
@@ -405,7 +403,7 @@ func (wd *WDADriver) AlertText() (text string, err error) {
// [[FBRoute GET:@"/alert/text"] respondWithTarget:self action:@selector(handleAlertGetTextCommand:)] // [[FBRoute GET:@"/alert/text"] respondWithTarget:self action:@selector(handleAlertGetTextCommand:)]
// [[FBRoute GET:@"/alert/text"].withoutSession // [[FBRoute GET:@"/alert/text"].withoutSession
var rawResp DriverRawResponse var rawResp DriverRawResponse
if rawResp, err = wd.Session.GET(wd.baseURL + "/alert/text"); err != nil { if rawResp, err = wd.Session.GET("/alert/text"); err != nil {
return "", err return "", err
} }
if text, err = rawResp.ValueConvertToString(); err != nil { if text, err = rawResp.ValueConvertToString(); err != nil {
@@ -417,7 +415,7 @@ func (wd *WDADriver) AlertText() (text string, err error) {
func (wd *WDADriver) AlertButtons() (btnLabels []string, err error) { func (wd *WDADriver) AlertButtons() (btnLabels []string, err error) {
// [[FBRoute GET:@"/wda/alert/buttons"] respondWithTarget:self action:@selector(handleGetAlertButtonsCommand:)] // [[FBRoute GET:@"/wda/alert/buttons"] respondWithTarget:self action:@selector(handleGetAlertButtonsCommand:)]
var rawResp DriverRawResponse var rawResp DriverRawResponse
if rawResp, err = wd.Session.GET(wd.baseURL + "/wda/alert/buttons"); err != nil { if rawResp, err = wd.Session.GET("/wda/alert/buttons"); err != nil {
return nil, err return nil, err
} }
reply := new(struct{ Value []string }) reply := new(struct{ Value []string })
@@ -435,7 +433,7 @@ func (wd *WDADriver) AlertAccept(label ...string) (err error) {
if len(label) != 0 && label[0] != "" { if len(label) != 0 && label[0] != "" {
data["name"] = label[0] data["name"] = label[0]
} }
_, err = wd.Session.POST(data, wd.baseURL+"/alert/accept") _, err = wd.Session.POST(data, "/alert/accept")
return return
} }
@@ -446,14 +444,14 @@ func (wd *WDADriver) AlertDismiss(label ...string) (err error) {
if len(label) != 0 && label[0] != "" { if len(label) != 0 && label[0] != "" {
data["name"] = label[0] data["name"] = label[0]
} }
_, err = wd.Session.POST(data, wd.baseURL+"/alert/dismiss") _, err = wd.Session.POST(data, "/alert/dismiss")
return return
} }
func (wd *WDADriver) AlertSendKeys(text string) (err error) { func (wd *WDADriver) AlertSendKeys(text string) (err error) {
// [[FBRoute POST:@"/alert/text"] respondWithTarget:self action:@selector(handleAlertSetTextCommand:)] // [[FBRoute POST:@"/alert/text"] respondWithTarget:self action:@selector(handleAlertSetTextCommand:)]
data := map[string]interface{}{"value": strings.Split(text, "")} data := map[string]interface{}{"value": strings.Split(text, "")}
_, err = wd.Session.POST(data, wd.baseURL+"/alert/text") _, err = wd.Session.POST(data, "/alert/text")
return return
} }
@@ -465,7 +463,7 @@ func (wd *WDADriver) AppLaunch(bundleId string) (err error) {
data["environment"] = map[string]interface{}{ data["environment"] = map[string]interface{}{
"SHOW_EXPLORER": "NO", "SHOW_EXPLORER": "NO",
} }
_, err = wd.Session.POST(data, wd.baseURL+"/wings/apps/launch") _, err = wd.Session.POST(data, "/wings/apps/launch")
if err != nil { if err != nil {
return errors.Wrap(code.MobileUILaunchAppError, return errors.Wrap(code.MobileUILaunchAppError,
fmt.Sprintf("wda launch failed: %v", err)) fmt.Sprintf("wda launch failed: %v", err))
@@ -477,7 +475,7 @@ func (wd *WDADriver) AppLaunchUnattached(bundleId string) (err error) {
log.Info().Str("bundleId", bundleId).Msg("WDADriver.AppLaunchUnattached") log.Info().Str("bundleId", bundleId).Msg("WDADriver.AppLaunchUnattached")
// [[FBRoute POST:@"/wda/apps/launchUnattached"].withoutSession respondWithTarget:self action:@selector(handleLaunchUnattachedApp:)] // [[FBRoute POST:@"/wda/apps/launchUnattached"].withoutSession respondWithTarget:self action:@selector(handleLaunchUnattachedApp:)]
data := map[string]interface{}{"bundleId": bundleId} data := map[string]interface{}{"bundleId": bundleId}
_, err = wd.Session.POST(data, wd.baseURL+"/wda/apps/launchUnattached") _, err = wd.Session.POST(data, "/wda/apps/launchUnattached")
if err != nil { if err != nil {
return errors.Wrap(code.MobileUILaunchAppError, return errors.Wrap(code.MobileUILaunchAppError,
fmt.Sprintf("wda launchUnattached failed: %v", err)) fmt.Sprintf("wda launchUnattached failed: %v", err))
@@ -490,7 +488,7 @@ func (wd *WDADriver) AppTerminate(bundleId string) (successful bool, err error)
// [[FBRoute POST:@"/wda/apps/terminate"] respondWithTarget:self action:@selector(handleSessionAppTerminate:)] // [[FBRoute POST:@"/wda/apps/terminate"] respondWithTarget:self action:@selector(handleSessionAppTerminate:)]
data := map[string]interface{}{"bundleId": bundleId} data := map[string]interface{}{"bundleId": bundleId}
var rawResp DriverRawResponse var rawResp DriverRawResponse
if rawResp, err = wd.Session.POST(data, wd.baseURL+"/wings/apps/terminate"); err != nil { if rawResp, err = wd.Session.POST(data, "/wings/apps/terminate"); err != nil {
return false, err return false, err
} }
if successful, err = rawResp.ValueConvertToBool(); err != nil { if successful, err = rawResp.ValueConvertToBool(); err != nil {
@@ -503,7 +501,7 @@ func (wd *WDADriver) AppActivate(bundleId string) (err error) {
log.Info().Str("bundleId", bundleId).Msg("WDADriver.AppActivate") log.Info().Str("bundleId", bundleId).Msg("WDADriver.AppActivate")
// [[FBRoute POST:@"/wda/apps/activate"] respondWithTarget:self action:@selector(handleSessionAppActivate:)] // [[FBRoute POST:@"/wda/apps/activate"] respondWithTarget:self action:@selector(handleSessionAppActivate:)]
data := map[string]interface{}{"bundleId": bundleId} data := map[string]interface{}{"bundleId": bundleId}
_, err = wd.Session.POST(data, wd.baseURL+"/wings/apps/activate") _, err = wd.Session.POST(data, "/wings/apps/activate")
return return
} }
@@ -566,7 +564,7 @@ func (wd *WDADriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
} }
option.MergeOptions(data, opts...) option.MergeOptions(data, opts...)
_, err = wd.Session.POST(data, wd.baseURL+"/wings/interaction/tap") _, err = wd.Session.POST(data, "/wings/interaction/tap")
return err return err
} }
@@ -588,7 +586,7 @@ func (wd *WDADriver) DoubleTap(x, y float64, opts ...option.ActionOption) error
"x": x, "x": x,
"y": y, "y": y,
} }
_, err = wd.Session.POST(data, wd.baseURL+"/wings/interaction/doubleTap") _, err = wd.Session.POST(data, "/wings/interaction/doubleTap")
return err return err
} }
@@ -628,7 +626,7 @@ func (wd *WDADriver) Drag(fromX, fromY, toX, toY float64, opts ...option.ActionO
} }
option.MergeOptions(data, opts...) option.MergeOptions(data, opts...)
// wda 43 version // wda 43 version
_, err = wd.Session.POST(data, wd.baseURL+"/wings/interaction/drag") _, err = wd.Session.POST(data, "/wings/interaction/drag")
return err return err
} }
@@ -642,7 +640,7 @@ func (wd *WDADriver) SetPasteboard(contentType types.PasteboardType, content str
"contentType": contentType, "contentType": contentType,
"content": base64.StdEncoding.EncodeToString([]byte(content)), "content": base64.StdEncoding.EncodeToString([]byte(content)),
} }
_, err = wd.Session.POST(data, wd.baseURL+"/wda/setPasteboard") _, err = wd.Session.POST(data, "/wda/setPasteboard")
return return
} }
@@ -650,7 +648,7 @@ func (wd *WDADriver) GetPasteboard(contentType types.PasteboardType) (raw *bytes
// [[FBRoute POST:@"/wda/getPasteboard"] respondWithTarget:self action:@selector(handleGetPasteboard:)] // [[FBRoute POST:@"/wda/getPasteboard"] respondWithTarget:self action:@selector(handleGetPasteboard:)]
data := map[string]interface{}{"contentType": contentType} data := map[string]interface{}{"contentType": contentType}
var rawResp DriverRawResponse var rawResp DriverRawResponse
if rawResp, err = wd.Session.POST(data, wd.baseURL+"/wda/getPasteboard"); err != nil { if rawResp, err = wd.Session.POST(data, "/wda/getPasteboard"); err != nil {
return nil, err return nil, err
} }
if raw, err = rawResp.ValueDecodeAsBase64(); err != nil { if raw, err = rawResp.ValueDecodeAsBase64(); err != nil {
@@ -668,7 +666,7 @@ func (wd *WDADriver) Input(text string, opts ...option.ActionOption) (err error)
// [[FBRoute POST:@"/wda/keys"] respondWithTarget:self action:@selector(handleKeys:)] // [[FBRoute POST:@"/wda/keys"] respondWithTarget:self action:@selector(handleKeys:)]
data := map[string]interface{}{"value": strings.Split(text, "")} data := map[string]interface{}{"value": strings.Split(text, "")}
option.MergeOptions(data, opts...) option.MergeOptions(data, opts...)
_, err = wd.Session.POST(data, wd.baseURL+"/gtf/interaction/input") _, err = wd.Session.POST(data, "/gtf/interaction/input")
return return
} }
@@ -679,7 +677,7 @@ func (wd *WDADriver) Backspace(count int, opts ...option.ActionOption) (err erro
} }
data := map[string]interface{}{"count": count} data := map[string]interface{}{"count": count}
option.MergeOptions(data, opts...) option.MergeOptions(data, opts...)
_, err = wd.Session.POST(data, wd.baseURL+"/gtf/interaction/input/backspace") _, err = wd.Session.POST(data, "/gtf/interaction/input/backspace")
return return
} }
@@ -701,14 +699,14 @@ func (wd *WDADriver) PressButton(button types.DeviceButton) (err error) {
} }
data := map[string]interface{}{"name": button} data := map[string]interface{}{"name": button}
_, err = wd.Session.POST(data, wd.baseURL+"/wda/pressButton") _, err = wd.Session.POST(data, "/wda/pressButton")
return return
} }
func (wd *WDADriver) Orientation() (orientation types.Orientation, err error) { func (wd *WDADriver) 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
if rawResp, err = wd.Session.GET(wd.baseURL + "/orientation"); err != nil { if rawResp, err = wd.Session.GET("/orientation"); err != nil {
return "", err return "", err
} }
reply := new(struct{ Value types.Orientation }) reply := new(struct{ Value types.Orientation })
@@ -722,14 +720,14 @@ func (wd *WDADriver) Orientation() (orientation types.Orientation, err error) {
func (wd *WDADriver) SetOrientation(orientation types.Orientation) (err error) { func (wd *WDADriver) SetOrientation(orientation types.Orientation) (err error) {
// [[FBRoute POST:@"/orientation"] respondWithTarget:self action:@selector(handleSetOrientation:)] // [[FBRoute POST:@"/orientation"] respondWithTarget:self action:@selector(handleSetOrientation:)]
data := map[string]interface{}{"orientation": orientation} data := map[string]interface{}{"orientation": orientation}
_, err = wd.Session.POST(data, wd.baseURL+"/orientation") _, err = wd.Session.POST(data, "/orientation")
return return
} }
func (wd *WDADriver) Rotation() (rotation types.Rotation, err error) { func (wd *WDADriver) Rotation() (rotation types.Rotation, err error) {
// [[FBRoute GET:@"/rotation"] respondWithTarget:self action:@selector(handleGetRotation:)] // [[FBRoute GET:@"/rotation"] respondWithTarget:self action:@selector(handleGetRotation:)]
var rawResp DriverRawResponse var rawResp DriverRawResponse
if rawResp, err = wd.Session.GET(wd.baseURL + "/rotation"); err != nil { if rawResp, err = wd.Session.GET("/rotation"); err != nil {
return types.Rotation{}, err return types.Rotation{}, err
} }
reply := new(struct{ Value types.Rotation }) reply := new(struct{ Value types.Rotation })
@@ -742,7 +740,7 @@ func (wd *WDADriver) Rotation() (rotation types.Rotation, err error) {
func (wd *WDADriver) SetRotation(rotation types.Rotation) (err error) { func (wd *WDADriver) SetRotation(rotation types.Rotation) (err error) {
// [[FBRoute POST:@"/rotation"] respondWithTarget:self action:@selector(handleSetRotation:)] // [[FBRoute POST:@"/rotation"] respondWithTarget:self action:@selector(handleSetRotation:)]
_, err = wd.Session.POST(rotation, wd.baseURL+"/rotation") _, err = wd.Session.POST(rotation, "/rotation")
return return
} }
@@ -756,7 +754,7 @@ func (wd *WDADriver) Source(srcOpt ...option.SourceOption) (source string, err e
query = "?" + query query = "?" + query
} }
var rawResp DriverRawResponse var rawResp DriverRawResponse
if rawResp, err = wd.Session.GET(wd.baseURL + "/source" + query); err != nil { if rawResp, err = wd.Session.GET("/source" + query); err != nil {
return "", err return "", err
} }
// json format // json format
@@ -779,7 +777,7 @@ func (wd *WDADriver) AccessibleSource() (source string, err error) {
// [[FBRoute GET:@"/wda/accessibleSource"] respondWithTarget:self action:@selector(handleGetAccessibleSourceCommand:)] // [[FBRoute GET:@"/wda/accessibleSource"] respondWithTarget:self action:@selector(handleGetAccessibleSourceCommand:)]
// [[FBRoute GET:@"/wda/accessibleSource"].withoutSession // [[FBRoute GET:@"/wda/accessibleSource"].withoutSession
var rawResp DriverRawResponse var rawResp DriverRawResponse
if rawResp, err = wd.Session.GET(wd.baseURL + "/wda/accessibleSource"); err != nil { if rawResp, err = wd.Session.GET("/wda/accessibleSource"); err != nil {
return "", err return "", err
} }
var jr builtinJSON.RawMessage var jr builtinJSON.RawMessage
@@ -792,13 +790,13 @@ func (wd *WDADriver) AccessibleSource() (source string, err error) {
func (wd *WDADriver) HealthCheck() (err error) { func (wd *WDADriver) HealthCheck() (err error) {
// [[FBRoute GET:@"/wda/healthcheck"].withoutSession respondWithTarget:self action:@selector(handleGetHealthCheck:)] // [[FBRoute GET:@"/wda/healthcheck"].withoutSession respondWithTarget:self action:@selector(handleGetHealthCheck:)]
_, err = wd.Session.GET(wd.baseURL + "/wda/healthcheck") _, err = wd.Session.GET("/wda/healthcheck")
return return
} }
func (wd *WDADriver) IsHealthy() (healthy bool, err error) { func (wd *WDADriver) IsHealthy() (healthy bool, err error) {
var rawResp DriverRawResponse var rawResp DriverRawResponse
if rawResp, err = wd.Session.GET(wd.baseURL + "/health"); err != nil { if rawResp, err = wd.Session.GET("/health"); err != nil {
return false, err return false, err
} }
if string(rawResp) != "I-AM-ALIVE" { if string(rawResp) != "I-AM-ALIVE" {
@@ -810,7 +808,7 @@ func (wd *WDADriver) IsHealthy() (healthy bool, err error) {
func (wd *WDADriver) GetAppiumSettings() (settings map[string]interface{}, err error) { func (wd *WDADriver) GetAppiumSettings() (settings map[string]interface{}, err error) {
// [[FBRoute GET:@"/appium/settings"] respondWithTarget:self action:@selector(handleGetSettings:)] // [[FBRoute GET:@"/appium/settings"] respondWithTarget:self action:@selector(handleGetSettings:)]
var rawResp DriverRawResponse var rawResp DriverRawResponse
if rawResp, err = wd.Session.GET(wd.baseURL + "/appium/settings"); err != nil { if rawResp, err = wd.Session.GET("/appium/settings"); err != nil {
return nil, err return nil, err
} }
reply := new(struct{ Value map[string]interface{} }) reply := new(struct{ Value map[string]interface{} })
@@ -825,7 +823,7 @@ func (wd *WDADriver) SetAppiumSettings(settings map[string]interface{}) (ret map
// [[FBRoute POST:@"/appium/settings"] respondWithTarget:self action:@selector(handleSetSettings:)] // [[FBRoute POST:@"/appium/settings"] respondWithTarget:self action:@selector(handleSetSettings:)]
data := map[string]interface{}{"settings": settings} data := map[string]interface{}{"settings": settings}
var rawResp DriverRawResponse var rawResp DriverRawResponse
if rawResp, err = wd.Session.POST(data, wd.baseURL+"/appium/settings"); err != nil { if rawResp, err = wd.Session.POST(data, "/appium/settings"); err != nil {
return nil, err return nil, err
} }
reply := new(struct{ Value map[string]interface{} }) reply := new(struct{ Value map[string]interface{} })
@@ -837,13 +835,13 @@ func (wd *WDADriver) SetAppiumSettings(settings map[string]interface{}) (ret map
} }
func (wd *WDADriver) WdaShutdown() (err error) { func (wd *WDADriver) WdaShutdown() (err error) {
_, err = wd.Session.GET(wd.baseURL + "/wda/shutdown") _, err = wd.Session.GET("/wda/shutdown")
return return
} }
func (wd *WDADriver) triggerWDALog(data map[string]interface{}) (rawResp []byte, err error) { func (wd *WDADriver) triggerWDALog(data map[string]interface{}) (rawResp []byte, err error) {
// [[FBRoute POST:@"/gtf/automation/log"].withoutSession respondWithTarget:self action:@selector(handleAutomationLog:)] // [[FBRoute POST:@"/gtf/automation/log"].withoutSession respondWithTarget:self action:@selector(handleAutomationLog:)]
return wd.Session.POST(data, wd.baseURL+"/gtf/automation/log") return wd.Session.POST(data, "/gtf/automation/log")
} }
func (wd *WDADriver) ScreenRecord(opts ...option.ActionOption) (videoPath string, err error) { func (wd *WDADriver) ScreenRecord(opts ...option.ActionOption) (videoPath string, err error) {
@@ -939,7 +937,7 @@ func (wd *WDADriver) PushImage(localPath string) error {
return err return err
} }
_, err = wd.Session.POST(data, wd.baseURL+"/gtf/albums/add") _, err = wd.Session.POST(data, "/gtf/albums/add")
return err return err
} }
@@ -952,7 +950,7 @@ func (wd *WDADriver) ClearImages() error {
log.Info().Msg("WDADriver.ClearImages") log.Info().Msg("WDADriver.ClearImages")
data := map[string]interface{}{} data := map[string]interface{}{}
_, err := wd.Session.POST(data, wd.baseURL+"/gtf/albums/clear") _, err := wd.Session.POST(data, "/gtf/albums/clear")
return err return err
} }