refactor: DriverSession

This commit is contained in:
lilong.129
2025-02-17 17:08:24 +08:00
parent c8d776009c
commit 29c0e1cbef
16 changed files with 377 additions and 396 deletions

View File

@@ -13,6 +13,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
@@ -33,22 +34,75 @@ func NewWDADriver(device *IOSDevice) (*WDADriver, error) {
log.Info().Interface("device", device).Msg("init ios WDA driver")
driver := &WDADriver{
Device: device,
Session: &Session{},
Session: NewDriverSession(),
}
err := driver.InitSession(nil)
if err != nil {
return nil, err
}
driver.InitSession(nil)
// init WDA scale
var err error
if driver.scale, err = driver.Scale(); err != nil {
return nil, err
}
// forward local port to device
var localPort int
localPort, err = strconv.Atoi(os.Getenv("WDA_LOCAL_PORT"))
if err != nil {
localPort, err = builtin.GetFreePort()
if err != nil {
return nil, errors.Wrap(code.DeviceHTTPDriverError,
fmt.Sprintf("get free port failed: %v", err))
}
if err = device.Forward(localPort, device.Options.WDAPort); err != nil {
return nil, errors.Wrap(code.DeviceHTTPDriverError,
fmt.Sprintf("forward tcp port failed: %v", err))
}
} else {
log.Info().Int("WDA_LOCAL_PORT", localPort).Msg("reuse WDA local port")
}
var localMjpegPort int
localMjpegPort, err = strconv.Atoi(os.Getenv("WDA_LOCAL_MJPEG_PORT"))
if err != nil {
localMjpegPort, err = builtin.GetFreePort()
if err != nil {
return nil, errors.Wrap(code.DeviceHTTPDriverError,
fmt.Sprintf("get free port failed: %v", err))
}
if err = device.Forward(localMjpegPort, device.Options.WDAMjpegPort); err != nil {
return nil, errors.Wrap(code.DeviceHTTPDriverError,
fmt.Sprintf("forward tcp port failed: %v", err))
}
} else {
log.Info().Int("WDA_LOCAL_MJPEG_PORT", localMjpegPort).
Msg("reuse WDA local mjpeg port")
}
host := "localhost"
driver.Session.SetBaseURL(fmt.Sprintf("http://%s:%d", host, localPort))
// create new session
if err = driver.InitSession(nil); err != nil {
return nil, errors.Wrap(code.DeviceHTTPDriverError, err.Error())
}
if driver.mjpegHTTPConn, err = net.Dial(
"tcp",
fmt.Sprintf("%s:%d", host, localMjpegPort),
); err != nil {
return nil, errors.Wrap(code.DeviceHTTPDriverError, err.Error())
}
driver.mjpegClient = NewHTTPClientWithConnection(driver.mjpegHTTPConn, 30*time.Second)
driver.mjpegUrl = fmt.Sprintf("%s:%d", host, localMjpegPort)
return driver, nil
}
type WDADriver struct {
Device *IOSDevice
Session *Session
Session *DriverSession
// cache to avoid repeated query
windowSize types.Size
@@ -59,17 +113,18 @@ type WDADriver struct {
mjpegUrl string
}
func (wd *WDADriver) resetSession() error {
capabilities := option.NewCapabilities()
capabilities.WithDefaultAlertAction(option.AlertActionAccept)
func (wd *WDADriver) GetMjpegClient() *http.Client {
return wd.mjpegClient
}
data := map[string]interface{}{
"capabilities": map[string]interface{}{
"alwaysMatch": capabilities,
},
func (wd *WDADriver) InitSession(capabilities option.Capabilities) error {
// [[FBRoute POST:@"/session"].withoutSession respondWithTarget:self action:@selector(handleCreateSession:)]
data := make(map[string]interface{})
if len(capabilities) == 0 {
data["capabilities"] = make(map[string]interface{})
} else {
data["capabilities"] = map[string]interface{}{"alwaysMatch": capabilities}
}
// Notice: use Driver.POST instead of httpPOST to avoid loop calling
rawResp, err := wd.Session.POST(data, "/session")
if err != nil {
return err
@@ -83,85 +138,6 @@ func (wd *WDADriver) resetSession() error {
return nil
}
func (wd *WDADriver) httpRequest(method string, rawURL string, rawBody []byte) (rawResp DriverRawResponse, err error) {
retryInterval := 3 * time.Second
for retryCount := 1; retryCount <= 3; retryCount++ {
rawResp, err = wd.Session.Request(method, rawURL, rawBody)
if err == nil {
return
}
// check WDA server status
status, err := wd.Status()
if err != nil {
log.Err(err).Msg("get WDA server status failed")
} else if status.State != "success" {
log.Warn().Interface("status", status).Msg("WDA server status is not success")
} else {
log.Info().Interface("status", status).Msg("get WDA server status")
}
retryInterval = retryInterval * 2
time.Sleep(retryInterval)
oldSessionID := wd.Session.ID
if err2 := wd.resetSession(); err2 != nil {
log.Err(err2).Msgf("failed to reset wda driver session, retry count: %v", retryCount)
continue
}
log.Debug().Str("new session", wd.Session.ID).Str("old session", oldSessionID).
Msgf("reset wda driver session successfully, retry count: %v", retryCount)
if oldSessionID != "" {
rawURL = strings.Replace(rawURL, oldSessionID, wd.Session.ID, 1)
}
}
return
}
func (wd *WDADriver) httpGET(pathElem ...string) (rawResp DriverRawResponse, err error) {
return wd.httpRequest(http.MethodGet, wd.Session.concatURL(nil, pathElem...), nil)
}
func (wd *WDADriver) httpPOST(data interface{}, pathElem ...string) (rawResp DriverRawResponse, err error) {
var bsJSON []byte = nil
if data != nil {
if bsJSON, err = json.Marshal(data); err != nil {
return nil, err
}
}
return wd.httpRequest(http.MethodPost, wd.Session.concatURL(nil, pathElem...), bsJSON)
}
func (wd *WDADriver) httpDELETE(pathElem ...string) (rawResp DriverRawResponse, err error) {
return wd.httpRequest(http.MethodDelete, wd.Session.concatURL(nil, pathElem...), nil)
}
func (wd *WDADriver) GetMjpegClient() *http.Client {
return wd.mjpegClient
}
func (wd *WDADriver) InitSession(capabilities option.Capabilities) error {
// [[FBRoute POST:@"/session"].withoutSession respondWithTarget:self action:@selector(handleCreateSession:)]
data := make(map[string]interface{})
if len(capabilities) == 0 {
data["capabilities"] = make(map[string]interface{})
} else {
data["capabilities"] = map[string]interface{}{
"alwaysMatch": capabilities,
}
}
rawResp, err := wd.httpPOST(data, "/session")
if err != nil {
return err
}
sessionInfo, err := rawResp.ValueConvertToSessionInfo()
if err != nil {
return err
}
wd.Session = &sessionInfo
return nil
}
func (wd *WDADriver) DeleteSession() (err error) {
if wd.mjpegClient != nil {
wd.mjpegClient.CloseIdleConnections()
@@ -171,7 +147,7 @@ func (wd *WDADriver) DeleteSession() (err error) {
}
// [[FBRoute DELETE:@""] respondWithTarget:self action:@selector(handleDeleteSession:)]
_, err = wd.httpDELETE("/session", wd.Session.ID)
_, err = wd.Session.DELETE("/session", wd.Session.ID)
return
}
@@ -198,7 +174,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"].withoutSession
var rawResp DriverRawResponse
if rawResp, err = wd.httpGET("/session", wd.Session.ID, "/wda/device/info"); err != nil {
if rawResp, err = wd.Session.GET("/session", wd.Session.ID, "/wda/device/info"); err != nil {
return types.DeviceInfo{}, err
}
reply := new(struct{ Value struct{ types.DeviceInfo } })
@@ -213,7 +189,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"].withoutSession
var rawResp DriverRawResponse
if rawResp, err = wd.httpGET("/session", wd.Session.ID, "/wda/device/location"); err != nil {
if rawResp, err = wd.Session.GET("/session", wd.Session.ID, "/wda/device/location"); err != nil {
return types.Location{}, err
}
reply := new(struct{ Value struct{ types.Location } })
@@ -227,7 +203,7 @@ func (wd *WDADriver) Location() (location types.Location, err error) {
func (wd *WDADriver) BatteryInfo() (batteryInfo types.BatteryInfo, err error) {
// [[FBRoute GET:@"/wda/batteryInfo"] respondWithTarget:self action:@selector(handleGetBatteryInfo:)]
var rawResp DriverRawResponse
if rawResp, err = wd.httpGET("/session", wd.Session.ID, "/wda/batteryInfo"); err != nil {
if rawResp, err = wd.Session.GET("/session", wd.Session.ID, "/wda/batteryInfo"); err != nil {
return types.BatteryInfo{}, err
}
reply := new(struct{ Value struct{ types.BatteryInfo } })
@@ -246,7 +222,7 @@ func (wd *WDADriver) WindowSize() (size types.Size, err error) {
}
var rawResp DriverRawResponse
if rawResp, err = wd.httpGET("/session", wd.Session.ID, "/window/size"); err != nil {
if rawResp, err = wd.Session.GET("/session", wd.Session.ID, "/window/size"); err != nil {
return types.Size{}, errors.Wrap(err, "get window size failed by WDA request")
}
reply := new(struct{ Value struct{ types.Size } })
@@ -280,7 +256,7 @@ func (wd *WDADriver) Scale() (float64, error) {
func (wd *WDADriver) Screen() (screen ai.Screen, err error) {
// [[FBRoute GET:@"/wda/screen"] respondWithTarget:self action:@selector(handleGetScreen:)]
var rawResp DriverRawResponse
if rawResp, err = wd.httpGET("/session", wd.Session.ID, "/wda/screen"); err != nil {
if rawResp, err = wd.Session.GET("/session", wd.Session.ID, "/wda/screen"); err != nil {
return ai.Screen{}, err
}
reply := new(struct{ Value struct{ ai.Screen } })
@@ -294,7 +270,7 @@ func (wd *WDADriver) Screen() (screen ai.Screen, 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"].withoutSession respondWithTarget:self action:@selector(handleGetScreenshot:)]
rawResp, err := wd.httpGET("/session", wd.Session.ID, "/screenshot")
rawResp, err := wd.Session.GET("/session", wd.Session.ID, "/screenshot")
if err != nil {
return nil, errors.Wrap(code.DeviceScreenShotError,
fmt.Sprintf("WDA screenshot failed %v", err))
@@ -327,7 +303,7 @@ func (wd *WDADriver) ActiveAppInfo() (info types.AppInfo, err error) {
// [[FBRoute GET:@"/wda/activeAppInfo"] respondWithTarget:self action:@selector(handleActiveAppInfo:)]
// [[FBRoute GET:@"/wda/activeAppInfo"].withoutSession
var rawResp DriverRawResponse
if rawResp, err = wd.httpGET("/session", wd.Session.ID, "/wda/activeAppInfo"); err != nil {
if rawResp, err = wd.Session.GET("/session", wd.Session.ID, "/wda/activeAppInfo"); err != nil {
return types.AppInfo{}, err
}
reply := new(struct{ Value struct{ types.AppInfo } })
@@ -341,7 +317,7 @@ func (wd *WDADriver) ActiveAppInfo() (info types.AppInfo, err error) {
func (wd *WDADriver) ActiveAppsList() (appsList []types.AppBaseInfo, err error) {
// [[FBRoute GET:@"/wda/apps/list"] respondWithTarget:self action:@selector(handleGetActiveAppsList:)]
var rawResp DriverRawResponse
if rawResp, err = wd.httpGET("/session", wd.Session.ID, "/wda/apps/list"); err != nil {
if rawResp, err = wd.Session.GET("/session", wd.Session.ID, "/wda/apps/list"); err != nil {
return nil, err
}
reply := new(struct{ Value []types.AppBaseInfo })
@@ -356,7 +332,7 @@ func (wd *WDADriver) AppState(bundleId string) (runState types.AppState, err err
// [[FBRoute POST:@"/wda/apps/state"] respondWithTarget:self action:@selector(handleSessionAppState:)]
data := map[string]interface{}{"bundleId": bundleId}
var rawResp DriverRawResponse
if rawResp, err = wd.httpPOST(data, "/session", wd.Session.ID, "/wda/apps/state"); err != nil {
if rawResp, err = wd.Session.POST(data, "/session", wd.Session.ID, "/wda/apps/state"); err != nil {
return 0, err
}
reply := new(struct{ Value types.AppState })
@@ -372,7 +348,7 @@ func (wd *WDADriver) IsLocked() (locked bool, err error) {
// [[FBRoute GET:@"/wda/locked"] respondWithTarget:self action:@selector(handleIsLocked:)]
// [[FBRoute GET:@"/wda/locked"].withoutSession
var rawResp DriverRawResponse
if rawResp, err = wd.httpGET("/session", wd.Session.ID, "/wda/locked"); err != nil {
if rawResp, err = wd.Session.GET("/session", wd.Session.ID, "/wda/locked"); err != nil {
return false, err
}
if locked, err = rawResp.ValueConvertToBool(); err != nil {
@@ -384,20 +360,20 @@ func (wd *WDADriver) IsLocked() (locked bool, err error) {
func (wd *WDADriver) Unlock() (err error) {
// [[FBRoute POST:@"/wda/unlock"] respondWithTarget:self action:@selector(handleUnlock:)]
// [[FBRoute POST:@"/wda/unlock"].withoutSession
_, err = wd.httpPOST(nil, "/session", wd.Session.ID, "/wda/unlock")
_, err = wd.Session.POST(nil, "/session", wd.Session.ID, "/wda/unlock")
return
}
func (wd *WDADriver) Lock() (err error) {
// [[FBRoute POST:@"/wda/lock"] respondWithTarget:self action:@selector(handleLock:)]
// [[FBRoute POST:@"/wda/lock"].withoutSession
_, err = wd.httpPOST(nil, "/session", wd.Session.ID, "/wda/lock")
_, err = wd.Session.POST(nil, "/session", wd.Session.ID, "/wda/lock")
return
}
func (wd *WDADriver) Home() (err error) {
// [[FBRoute POST:@"/wda/homescreen"].withoutSession respondWithTarget:self action:@selector(handleHomescreenCommand:)]
_, err = wd.httpPOST(nil, "/wda/homescreen")
_, err = wd.Session.POST(nil, "/wda/homescreen")
return
}
@@ -405,7 +381,7 @@ func (wd *WDADriver) AlertText() (text string, err error) {
// [[FBRoute GET:@"/alert/text"] respondWithTarget:self action:@selector(handleAlertGetTextCommand:)]
// [[FBRoute GET:@"/alert/text"].withoutSession
var rawResp DriverRawResponse
if rawResp, err = wd.httpGET("/session", wd.Session.ID, "/alert/text"); err != nil {
if rawResp, err = wd.Session.GET("/session", wd.Session.ID, "/alert/text"); err != nil {
return "", err
}
if text, err = rawResp.ValueConvertToString(); err != nil {
@@ -417,7 +393,7 @@ func (wd *WDADriver) AlertText() (text string, err error) {
func (wd *WDADriver) AlertButtons() (btnLabels []string, err error) {
// [[FBRoute GET:@"/wda/alert/buttons"] respondWithTarget:self action:@selector(handleGetAlertButtonsCommand:)]
var rawResp DriverRawResponse
if rawResp, err = wd.httpGET("/session", wd.Session.ID, "/wda/alert/buttons"); err != nil {
if rawResp, err = wd.Session.GET("/session", wd.Session.ID, "/wda/alert/buttons"); err != nil {
return nil, err
}
reply := new(struct{ Value []string })
@@ -435,7 +411,7 @@ func (wd *WDADriver) AlertAccept(label ...string) (err error) {
if len(label) != 0 && label[0] != "" {
data["name"] = label[0]
}
_, err = wd.httpPOST(data, "/alert/accept")
_, err = wd.Session.POST(data, "/alert/accept")
return
}
@@ -446,14 +422,14 @@ func (wd *WDADriver) AlertDismiss(label ...string) (err error) {
if len(label) != 0 && label[0] != "" {
data["name"] = label[0]
}
_, err = wd.httpPOST(data, "/alert/dismiss")
_, err = wd.Session.POST(data, "/alert/dismiss")
return
}
func (wd *WDADriver) AlertSendKeys(text string) (err error) {
// [[FBRoute POST:@"/alert/text"] respondWithTarget:self action:@selector(handleAlertSetTextCommand:)]
data := map[string]interface{}{"value": strings.Split(text, "")}
_, err = wd.httpPOST(data, "/session", wd.Session.ID, "/alert/text")
_, err = wd.Session.POST(data, "/session", wd.Session.ID, "/alert/text")
return
}
@@ -464,7 +440,7 @@ func (wd *WDADriver) AppLaunch(bundleId string) (err error) {
data["environment"] = map[string]interface{}{
"SHOW_EXPLORER": "NO",
}
_, err = wd.httpPOST(data, "/session", wd.Session.ID, "/wda/apps/launch")
_, err = wd.Session.POST(data, "/session", wd.Session.ID, "/wda/apps/launch")
if err != nil {
return errors.Wrap(code.MobileUILaunchAppError,
fmt.Sprintf("wda launch failed: %v", err))
@@ -475,7 +451,7 @@ func (wd *WDADriver) AppLaunch(bundleId string) (err error) {
func (wd *WDADriver) AppLaunchUnattached(bundleId string) (err error) {
// [[FBRoute POST:@"/wda/apps/launchUnattached"].withoutSession respondWithTarget:self action:@selector(handleLaunchUnattachedApp:)]
data := map[string]interface{}{"bundleId": bundleId}
_, err = wd.httpPOST(data, "/wda/apps/launchUnattached")
_, err = wd.Session.POST(data, "/wda/apps/launchUnattached")
if err != nil {
return errors.Wrap(code.MobileUILaunchAppError,
fmt.Sprintf("wda launchUnattached failed: %v", err))
@@ -487,7 +463,7 @@ func (wd *WDADriver) AppTerminate(bundleId string) (successful bool, err error)
// [[FBRoute POST:@"/wda/apps/terminate"] respondWithTarget:self action:@selector(handleSessionAppTerminate:)]
data := map[string]interface{}{"bundleId": bundleId}
var rawResp DriverRawResponse
if rawResp, err = wd.httpPOST(data, "/session", wd.Session.ID, "/wda/apps/terminate"); err != nil {
if rawResp, err = wd.Session.POST(data, "/session", wd.Session.ID, "/wda/apps/terminate"); err != nil {
return false, err
}
if successful, err = rawResp.ValueConvertToBool(); err != nil {
@@ -499,7 +475,7 @@ func (wd *WDADriver) AppTerminate(bundleId string) (successful bool, err error)
func (wd *WDADriver) AppActivate(bundleId string) (err error) {
// [[FBRoute POST:@"/wda/apps/activate"] respondWithTarget:self action:@selector(handleSessionAppActivate:)]
data := map[string]interface{}{"bundleId": bundleId}
_, err = wd.httpPOST(data, "/session", wd.Session.ID, "/wda/apps/activate")
_, err = wd.Session.POST(data, "/session", wd.Session.ID, "/wda/apps/activate")
return
}
@@ -509,7 +485,7 @@ func (wd *WDADriver) AppDeactivate(second float64) (err error) {
second = 3.0
}
data := map[string]interface{}{"duration": second}
_, err = wd.httpPOST(data, "/session", wd.Session.ID, "/wda/deactivateApp")
_, err = wd.Session.POST(data, "/session", wd.Session.ID, "/wda/deactivateApp")
return
}
@@ -552,7 +528,7 @@ func (wd *WDADriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
"x": wd.toScale(x),
"y": wd.toScale(y),
}
_, err := wd.httpPOST(data, "/session", wd.Session.ID, "/wda/tap/0")
_, err := wd.Session.POST(data, "/session", wd.Session.ID, "/wda/tap/0")
return err
}
@@ -569,7 +545,7 @@ func (wd *WDADriver) DoubleTapXY(x, y float64, opts ...option.ActionOption) erro
"x": x,
"y": y,
}
_, err = wd.httpPOST(data, "/session", wd.Session.ID, "/wda/doubleTap")
_, err = wd.Session.POST(data, "/session", wd.Session.ID, "/wda/doubleTap")
return err
}
@@ -608,8 +584,8 @@ func (wd *WDADriver) Drag(fromX, fromY, toX, toY float64, opts ...option.ActionO
// update data options in post data for extra WDA configurations
actionOptions.UpdateData(data)
// wda 43 version
_, err = wd.httpPOST(data, "/session", wd.Session.ID, "/wda/dragfromtoforduration")
// _, err = wd.httpPOST(data, "/session", wd.Session.ID, "/wda/drag")
_, err = wd.Session.POST(data, "/session", wd.Session.ID, "/wda/dragfromtoforduration")
// _, err = wd.Session.POST(data, "/session", wd.Session.ID, "/wda/drag")
return err
}
@@ -623,7 +599,7 @@ func (wd *WDADriver) SetPasteboard(contentType types.PasteboardType, content str
"contentType": contentType,
"content": base64.StdEncoding.EncodeToString([]byte(content)),
}
_, err = wd.httpPOST(data, "/session", wd.Session.ID, "/wda/setPasteboard")
_, err = wd.Session.POST(data, "/session", wd.Session.ID, "/wda/setPasteboard")
return
}
@@ -631,7 +607,7 @@ func (wd *WDADriver) GetPasteboard(contentType types.PasteboardType) (raw *bytes
// [[FBRoute POST:@"/wda/getPasteboard"] respondWithTarget:self action:@selector(handleGetPasteboard:)]
data := map[string]interface{}{"contentType": contentType}
var rawResp DriverRawResponse
if rawResp, err = wd.httpPOST(data, "/session", wd.Session.ID, "/wda/getPasteboard"); err != nil {
if rawResp, err = wd.Session.POST(data, "/session", wd.Session.ID, "/wda/getPasteboard"); err != nil {
return nil, err
}
if raw, err = rawResp.ValueDecodeAsBase64(); err != nil {
@@ -652,7 +628,7 @@ func (wd *WDADriver) Input(text string, opts ...option.ActionOption) (err error)
// new data options in post data for extra WDA configurations
actionOptions.UpdateData(data)
_, err = wd.httpPOST(data, "/session", wd.Session.ID, "/wda/keys")
_, err = wd.Session.POST(data, "/session", wd.Session.ID, "/wda/keys")
return
}
@@ -666,7 +642,7 @@ func (wd *WDADriver) Backspace(count int, opts ...option.ActionOption) (err erro
// new data options in post data for extra WDA configurations
actionOptions.UpdateData(data)
_, err = wd.httpPOST(data, "/gtf/interaction/input/backspace")
_, err = wd.Session.POST(data, "/gtf/interaction/input/backspace")
return
}
@@ -692,21 +668,21 @@ func (wd *WDADriver) Back() (err error) {
"toY": toY,
}
_, err = wd.httpPOST(data, "/session", wd.Session.ID, "/wda/dragfromtoforduration")
_, err = wd.Session.POST(data, "/session", wd.Session.ID, "/wda/dragfromtoforduration")
return
}
func (wd *WDADriver) PressButton(devBtn types.DeviceButton) (err error) {
// [[FBRoute POST:@"/wda/pressButton"] respondWithTarget:self action:@selector(handlePressButtonCommand:)]
data := map[string]interface{}{"name": devBtn}
_, err = wd.httpPOST(data, "/session", wd.Session.ID, "/wda/pressButton")
_, err = wd.Session.POST(data, "/session", wd.Session.ID, "/wda/pressButton")
return
}
func (wd *WDADriver) Orientation() (orientation types.Orientation, err error) {
// [[FBRoute GET:@"/orientation"] respondWithTarget:self action:@selector(handleGetOrientation:)]
var rawResp DriverRawResponse
if rawResp, err = wd.httpGET("/session", wd.Session.ID, "/orientation"); err != nil {
if rawResp, err = wd.Session.GET("/session", wd.Session.ID, "/orientation"); err != nil {
return "", err
}
reply := new(struct{ Value types.Orientation })
@@ -720,14 +696,14 @@ func (wd *WDADriver) Orientation() (orientation types.Orientation, err error) {
func (wd *WDADriver) SetOrientation(orientation types.Orientation) (err error) {
// [[FBRoute POST:@"/orientation"] respondWithTarget:self action:@selector(handleSetOrientation:)]
data := map[string]interface{}{"orientation": orientation}
_, err = wd.httpPOST(data, "/session", wd.Session.ID, "/orientation")
_, err = wd.Session.POST(data, "/session", wd.Session.ID, "/orientation")
return
}
func (wd *WDADriver) Rotation() (rotation types.Rotation, err error) {
// [[FBRoute GET:@"/rotation"] respondWithTarget:self action:@selector(handleGetRotation:)]
var rawResp DriverRawResponse
if rawResp, err = wd.httpGET("/session", wd.Session.ID, "/rotation"); err != nil {
if rawResp, err = wd.Session.GET("/session", wd.Session.ID, "/rotation"); err != nil {
return types.Rotation{}, err
}
reply := new(struct{ Value types.Rotation })
@@ -740,14 +716,18 @@ func (wd *WDADriver) Rotation() (rotation types.Rotation, err error) {
func (wd *WDADriver) SetRotation(rotation types.Rotation) (err error) {
// [[FBRoute POST:@"/rotation"] respondWithTarget:self action:@selector(handleSetRotation:)]
_, err = wd.httpPOST(rotation, "/session", wd.Session.ID, "/rotation")
_, err = wd.Session.POST(rotation, "/session", wd.Session.ID, "/rotation")
return
}
func (wd *WDADriver) Source(srcOpt ...option.SourceOption) (source string, err error) {
// [[FBRoute GET:@"/source"] respondWithTarget:self action:@selector(handleGetSourceCommand:)]
// [[FBRoute GET:@"/source"].withoutSession
tmp, _ := url.Parse(wd.Session.concatURL(nil, "/session", wd.Session.ID))
urlStr, err := wd.Session.concatURL("/session", wd.Session.ID)
if err != nil {
return "", err
}
tmp, _ := url.Parse(urlStr)
toJsonRaw := false
if len(srcOpt) != 0 {
q := tmp.Query()
@@ -761,8 +741,12 @@ func (wd *WDADriver) Source(srcOpt ...option.SourceOption) (source string, err e
tmp.RawQuery = q.Encode()
}
urlStr, err = wd.Session.concatURL(tmp.Path, "/source")
if err != nil {
return "", err
}
var rawResp DriverRawResponse
if rawResp, err = wd.httpRequest(http.MethodGet, wd.Session.concatURL(tmp, "/source"), nil); err != nil {
if rawResp, err = wd.Session.GET(http.MethodGet, urlStr); err != nil {
return "", nil
}
if toJsonRaw {
@@ -782,7 +766,7 @@ func (wd *WDADriver) AccessibleSource() (source string, err error) {
// [[FBRoute GET:@"/wda/accessibleSource"] respondWithTarget:self action:@selector(handleGetAccessibleSourceCommand:)]
// [[FBRoute GET:@"/wda/accessibleSource"].withoutSession
var rawResp DriverRawResponse
if rawResp, err = wd.httpGET("/session", wd.Session.ID, "/wda/accessibleSource"); err != nil {
if rawResp, err = wd.Session.GET("/session", wd.Session.ID, "/wda/accessibleSource"); err != nil {
return "", err
}
var jr builtinJSON.RawMessage
@@ -795,13 +779,13 @@ func (wd *WDADriver) AccessibleSource() (source string, err error) {
func (wd *WDADriver) HealthCheck() (err error) {
// [[FBRoute GET:@"/wda/healthcheck"].withoutSession respondWithTarget:self action:@selector(handleGetHealthCheck:)]
_, err = wd.httpGET("/wda/healthcheck")
_, err = wd.Session.GET("/wda/healthcheck")
return
}
func (wd *WDADriver) IsHealthy() (healthy bool, err error) {
var rawResp DriverRawResponse
if rawResp, err = wd.httpGET("/health"); err != nil {
if rawResp, err = wd.Session.GET("/health"); err != nil {
return false, err
}
if string(rawResp) != "I-AM-ALIVE" {
@@ -813,7 +797,7 @@ func (wd *WDADriver) IsHealthy() (healthy bool, err error) {
func (wd *WDADriver) GetAppiumSettings() (settings map[string]interface{}, err error) {
// [[FBRoute GET:@"/appium/settings"] respondWithTarget:self action:@selector(handleGetSettings:)]
var rawResp DriverRawResponse
if rawResp, err = wd.httpGET("/session", wd.Session.ID, "/appium/settings"); err != nil {
if rawResp, err = wd.Session.GET("/session", wd.Session.ID, "/appium/settings"); err != nil {
return nil, err
}
reply := new(struct{ Value map[string]interface{} })
@@ -828,7 +812,7 @@ func (wd *WDADriver) SetAppiumSettings(settings map[string]interface{}) (ret map
// [[FBRoute POST:@"/appium/settings"] respondWithTarget:self action:@selector(handleSetSettings:)]
data := map[string]interface{}{"settings": settings}
var rawResp DriverRawResponse
if rawResp, err = wd.httpPOST(data, "/session", wd.Session.ID, "/appium/settings"); err != nil {
if rawResp, err = wd.Session.POST(data, "/session", wd.Session.ID, "/appium/settings"); err != nil {
return nil, err
}
reply := new(struct{ Value map[string]interface{} })
@@ -840,13 +824,13 @@ func (wd *WDADriver) SetAppiumSettings(settings map[string]interface{}) (ret map
}
func (wd *WDADriver) WdaShutdown() (err error) {
_, err = wd.httpGET("/wda/shutdown")
_, err = wd.Session.GET("/wda/shutdown")
return
}
func (wd *WDADriver) triggerWDALog(data map[string]interface{}) (rawResp []byte, err error) {
// [[FBRoute POST:@"/gtf/automation/log"].withoutSession respondWithTarget:self action:@selector(handleAutomationLog:)]
return wd.httpPOST(data, "/gtf/automation/log")
return wd.Session.POST(data, "/gtf/automation/log")
}
func (wd *WDADriver) ScreenRecord(duration time.Duration) (videoPath string, err error) {
@@ -945,7 +929,7 @@ func (wd *WDADriver) StopCaptureLog() (result interface{}, err error) {
return reply.Value, nil
}
func (wd *WDADriver) GetSession() *Session {
func (wd *WDADriver) GetSession() *DriverSession {
return wd.Session
}