Merge branch 'master' into session_refactor

This commit is contained in:
余泓铮
2025-07-17 16:28:25 +08:00
10 changed files with 226 additions and 266 deletions

View File

@@ -138,7 +138,7 @@ func (wd *BrowserDriver) Drag(fromX, fromY, toX, toY float64, options ...option.
data["duration"] = 0.5
}
_, err = wd.Session.POST(data, wd.concatURL(wd.Session.ID, "ui/drag"))
_, err = wd.CustomePost(data, wd.concatURL(wd.Session.ID, "ui/drag"))
return
}
@@ -146,7 +146,7 @@ func (wd *BrowserDriver) AppLaunch(packageName string) (err error) {
data := map[string]interface{}{
"url": packageName,
}
_, err = wd.Session.POST(data, wd.concatURL(wd.Session.ID, "ui/page_launch"))
_, err = wd.CustomePost(data, wd.concatURL(wd.Session.ID, "ui/page_launch"))
return err
}
@@ -185,7 +185,7 @@ func (wd *BrowserDriver) Scroll(delta int) (err error) {
data := map[string]interface{}{
"delta": delta,
}
_, err = wd.Session.POST(data, wd.concatURL(wd.Session.ID, "ui/scroll"))
_, err = wd.CustomePost(data, wd.concatURL(wd.Session.ID, "ui/scroll"))
return err
}
@@ -210,7 +210,7 @@ func (wd *BrowserDriver) CloseTab(pageIndex int) (err error) {
"page_index": pageIndex,
}
_, err = wd.Session.POST(data, wd.concatURL(wd.Session.ID, "ui/page_close"))
_, err = wd.CustomePost(data, wd.concatURL(wd.Session.ID, "ui/page_close"))
return err
}
@@ -223,7 +223,7 @@ func (wd *BrowserDriver) HoverBySelector(selector string, options ...option.Acti
if actionOptions.Index > 0 {
data["element_index"] = actionOptions.Index
}
_, err = wd.Session.POST(data, wd.concatURL(wd.Session.ID, "ui/hover"))
_, err = wd.CustomePost(data, wd.concatURL(wd.Session.ID, "ui/hover"))
return err
}
@@ -236,7 +236,7 @@ func (wd *BrowserDriver) TapBySelector(selector string, options ...option.Action
if actionOptions.Index > 0 {
data["element_index"] = actionOptions.Index
}
_, err = wd.Session.POST(data, wd.concatURL(wd.Session.ID, "ui/tap"))
_, err = wd.CustomePost(data, wd.concatURL(wd.Session.ID, "ui/tap"))
return err
}
@@ -246,7 +246,7 @@ func (wd *BrowserDriver) SecondaryClick(x, y float64) (err error) {
"x": x,
"y": y,
}
_, err = wd.Session.POST(data, wd.concatURL(wd.Session.ID, "ui/right_click"))
_, err = wd.CustomePost(data, wd.concatURL(wd.Session.ID, "ui/right_click"))
return err
}
@@ -259,7 +259,7 @@ func (wd *BrowserDriver) SecondaryClickBySelector(selector string, options ...op
if actionOptions.Index > 0 {
data["element_index"] = actionOptions.Index
}
_, err = wd.Session.POST(data, wd.concatURL(wd.Session.ID, "ui/right_click"))
_, err = wd.CustomePost(data, wd.concatURL(wd.Session.ID, "ui/right_click"))
return err
}
@@ -315,28 +315,21 @@ func (wd *BrowserDriver) GetPageUrl(options ...option.ActionOption) (text string
if actionOptions.Index > 0 {
uri = uri + "?page_index=" + fmt.Sprintf("%v", actionOptions.Index)
}
resp, err := wd.Session.GET(wd.concatURL(wd.Session.ID, uri))
resp, err := wd.CustomeGet(wd.concatURL(wd.Session.ID, uri))
if err != nil {
return "", err
}
data, err := resp.ValueConvertToJsonObject()
if err != nil {
return "", err
}
data = data["data"].(map[string]interface{})
data := resp.Data.(map[string]interface{})
return data["url"].(string), nil
}
func (wd *BrowserDriver) IsElementExistBySelector(selector string) (bool, error) {
resp, err := wd.Session.GET(wd.concatURL("ui/element_exist", "?selector=", selector))
resp, err := wd.CustomeGet(wd.concatURL("ui/element_exist", "?selector=", selector))
if err != nil {
return false, err
}
data, err := resp.ValueConvertToJsonObject()
if err != nil {
return false, err
}
data = data["data"].(map[string]interface{})
data := resp.Data.(map[string]interface{})
return data["exist"].(bool), nil
}
@@ -345,7 +338,7 @@ func (wd *BrowserDriver) LoginNoneUI(packageName, phoneNumber string, captcha, p
"url": packageName,
"web_cookie": password,
}
_, err = wd.Session.POST(data, wd.concatURL(wd.Session.ID, "stub/login"))
_, err = wd.CustomePost(data, wd.concatURL(wd.Session.ID, "stub/login"))
if err != nil {
return false, err
}
@@ -357,7 +350,7 @@ func (wd *BrowserDriver) Hover(x, y float64) (err error) {
"x": x,
"y": y,
}
_, err = wd.Session.POST(data, wd.concatURL(wd.Session.ID, "ui/hover"))
_, err = wd.CustomePost(data, wd.concatURL(wd.Session.ID, "ui/hover"))
return err
}
@@ -365,37 +358,32 @@ func (wd *BrowserDriver) Input(text string, option ...option.ActionOption) (err
data := map[string]interface{}{
"text": text,
}
_, err = wd.Session.POST(data, wd.concatURL(wd.Session.ID, "ui/input"))
_, err = wd.CustomePost(data, wd.concatURL(wd.Session.ID, "ui/input"))
return err
}
// Source Return application elements tree
func (wd *BrowserDriver) Source(srcOpt ...option.SourceOption) (string, error) {
resp, err := wd.Session.GET(wd.concatURL(wd.Session.ID, "stub/source"))
result, err := wd.CustomeGet(wd.concatURL(wd.Session.ID, "stub/source"))
if err != nil {
return "", err
}
return resp.ValueConvertToString()
jsonData, err := json.Marshal(result.Data)
if err != nil {
return "", err
}
return string(jsonData), err
}
func (wd *BrowserDriver) ScreenShot(options ...option.ActionOption) (*bytes.Buffer, error) {
resp, err := wd.Session.GET(wd.concatURL(wd.Session.ID, "screenshot"))
result, err := wd.CustomeGet(wd.concatURL(wd.Session.ID, "screenshot"))
if err != nil {
return nil, err
}
// 将结果解析为 JSON
var result WebAgentResponse
if err = json.Unmarshal(resp, &result); err != nil {
return nil, err
}
if result.Code != 0 {
log.Info().Msgf("%v", result.Message)
return nil, errors.New(result.Message)
}
data := result.Data.(map[string]interface{})
screenshotBase64 := data["screenshot"].(string)
screenRaw, err := base64.StdEncoding.DecodeString(screenshotBase64)
@@ -425,16 +413,12 @@ func (wd *BrowserDriver) BatteryInfo() (batteryInfo types.BatteryInfo, err error
}
func (wd *BrowserDriver) WindowSize() (types.Size, error) {
resp, err := wd.Session.GET(wd.concatURL(wd.Session.ID, "window_size"))
resp, err := wd.CustomeGet(wd.concatURL(wd.Session.ID, "window_size"))
if err != nil {
return types.Size{}, err
}
data, err := resp.ValueConvertToJsonObject()
if err != nil {
return types.Size{}, err
}
data = data["data"].(map[string]interface{})
data := resp.Data.(map[string]interface{})
width := data["width"]
height := data["height"]
return types.Size{
@@ -532,7 +516,7 @@ func (wd *BrowserDriver) TapFloat(x, y float64, opts ...option.ActionOption) err
"duration": duration,
}
_, err = wd.Session.POST(data, wd.concatURL(wd.Session.ID, "ui/tap"))
_, err = wd.CustomePost(data, wd.concatURL(wd.Session.ID, "ui/tap"))
return err
}
@@ -550,7 +534,7 @@ func (wd *BrowserDriver) DoubleTap(x, y float64, options ...option.ActionOption)
"y": y,
}
_, err = wd.Session.POST(data, wd.concatURL(wd.Session.ID, "ui/double_tap"))
_, err = wd.CustomePost(data, wd.concatURL(wd.Session.ID, "ui/double_tap"))
return err
}
@@ -561,7 +545,7 @@ func (wd *BrowserDriver) UploadFile(x, y float64, FileUrl, FileFormat string) (e
"file_url": FileUrl,
"file_format": FileFormat,
}
_, err = wd.Session.POST(data, wd.concatURL(wd.Session.ID, "ui/upload"))
_, err = wd.CustomePost(data, wd.concatURL(wd.Session.ID, "ui/upload"))
return err
}
@@ -607,7 +591,7 @@ func (wd *BrowserDriver) ForegroundInfo() (app types.AppInfo, err error) {
// PressBack Presses the back button
func (wd *BrowserDriver) PressBack(options ...option.ActionOption) error {
_, err := wd.Session.POST(nil, wd.concatURL(wd.Session.ID, "ui/back"))
_, err := wd.CustomePost(nil, wd.concatURL(wd.Session.ID, "ui/back"))
return err
}
@@ -699,10 +683,71 @@ func (wd *BrowserDriver) TapXY(x, y float64, opts ...option.ActionOption) error
"x": x,
"y": y,
}
_, err := wd.Session.POST(data, wd.concatURL(wd.Session.ID, "ui/double_tap"))
_, err := wd.CustomePost(data, wd.concatURL(wd.Session.ID, "ui/tap"))
return err
}
func (wd *BrowserDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
return wd.TapFloat(x, y, opts...)
}
func (wd *BrowserDriver) SetHeader(headers string) (err error) {
data := map[string]interface{}{
"headers": headers,
}
_, err = wd.CustomePost(data, wd.concatURL(wd.Session.ID, "set_headers"))
return err
}
func (wd *BrowserDriver) Keyboard(key string) (err error) {
data := map[string]interface{}{
"press": key,
}
_, err = wd.CustomePost(data, wd.concatURL(wd.Session.ID, "ui/keyboard"))
return err
}
func (wd *BrowserDriver) PageAction(action string) (err error) {
data := map[string]interface{}{
"action": action,
}
_, err = wd.CustomePost(data, wd.concatURL(wd.Session.ID, "ui/page/action"))
return err
}
func (wd *BrowserDriver) CustomePost(data interface{}, urlStr string) (response *WebAgentResponse, err error) {
rawResp, err := wd.Session.POST(data, urlStr, option.WithMaxRetryTimes(1), option.WithTimeout(3*60))
if err != nil {
return nil, err
}
var result WebAgentResponse
if err = json.Unmarshal(rawResp, &result); err != nil {
return nil, err
}
if result.Code != 0 {
log.Info().Msgf("%v", result.Message)
return nil, errors.New(result.Message)
}
return &result, err
}
func (wd *BrowserDriver) CustomeGet(urlStr string) (response *WebAgentResponse, err error) {
rawResp, err := wd.Session.GET(urlStr, option.WithMaxRetryTimes(1), option.WithTimeout(3*60))
if err != nil {
return nil, err
}
var webResp WebAgentResponse
if err = json.Unmarshal(rawResp, &webResp); err != nil {
return nil, err
}
if webResp.Code != 0 {
log.Info().Msgf("%v", webResp.Message)
return nil, errors.New(webResp.Message)
}
return &webResp, err
}

View File

@@ -155,33 +155,21 @@ func (s *DriverSession) buildURL(urlStr string) (string, error) {
}
func (s *DriverSession) GET(urlStr string, opts ...option.ActionOption) (rawResp DriverRawResponse, err error) {
rawResp, err = s.RequestWithRetry(http.MethodGet, urlStr, nil, opts...)
if err != nil {
return nil, errors.Wrap(code.DeviceHTTPDriverError, err.Error())
}
return rawResp, nil
return s.RequestWithRetry(http.MethodGet, urlStr, nil, opts...)
}
func (s *DriverSession) POST(data interface{}, urlStr string, opts ...option.ActionOption) (rawResp DriverRawResponse, err error) {
var bsJSON []byte = nil
if data != nil {
if bsJSON, err = json.Marshal(data); err != nil {
return nil, errors.Wrap(code.DeviceHTTPDriverError, err.Error())
return nil, errors.Wrap(code.InvalidParamError, err.Error())
}
}
rawResp, err = s.RequestWithRetry(http.MethodPost, urlStr, bsJSON, opts...)
if err != nil {
return nil, errors.Wrap(code.DeviceHTTPDriverError, err.Error())
}
return rawResp, nil
return s.RequestWithRetry(http.MethodPost, urlStr, bsJSON, opts...)
}
func (s *DriverSession) DELETE(urlStr string, opts ...option.ActionOption) (rawResp DriverRawResponse, err error) {
rawResp, err = s.RequestWithRetry(http.MethodDelete, urlStr, nil, opts...)
if err != nil {
return nil, errors.Wrap(code.DeviceHTTPDriverError, err.Error())
}
return rawResp, nil
return s.RequestWithRetry(http.MethodDelete, urlStr, nil, opts...)
}
func (s *DriverSession) RequestWithRetry(method string, urlStr string, rawBody []byte, opts ...option.ActionOption) (
@@ -205,7 +193,8 @@ func (s *DriverSession) RequestWithRetry(method string, urlStr string, rawBody [
return rawResp, nil
}
lastError = err
// Notice: use DeviceHTTPDriverError when request driver failed
lastError = errors.Wrap(code.DeviceHTTPDriverError, err.Error())
log.Warn().Err(err).Msgf("request failed, attempt %d/%d", attempt, s.maxRetry)
// If this was the last attempt, break
@@ -319,9 +308,6 @@ func (s *DriverSession) Request(method string, urlStr string, rawBody []byte, op
}
if err = rawResp.CheckErr(); err != nil {
if resp.StatusCode == http.StatusOK {
return rawResp, nil
}
return nil, err
}

View File

@@ -243,8 +243,7 @@ func (dev *IOSDevice) NewDriver() (driver IDriver, err error) {
if dev.Options.ResetHomeOnStartup {
log.Info().Msg("go back to home screen")
if err = wdaDriver.Home(); err != nil {
return nil, errors.Wrap(code.DeviceHTTPDriverError,
fmt.Sprintf("go back to home screen failed: %v", err))
return nil, errors.Wrap(err, "go back to home screen failed")
}
}
if dev.Options.LogOn {

View File

@@ -196,7 +196,7 @@ func (wd *WDADriver) DeviceInfo() (deviceInfo types.DeviceInfo, err error) {
// [[FBRoute GET:@"/wda/device/info"].withoutSession
var rawResp DriverRawResponse
if rawResp, err = wd.Session.GET("/wda/device/info"); err != nil {
return types.DeviceInfo{}, errors.Wrap(code.DeviceHTTPDriverError, err.Error())
return types.DeviceInfo{}, err
}
reply := new(struct{ Value struct{ types.DeviceInfo } })
if err = json.Unmarshal(rawResp, reply); err != nil {
@@ -275,14 +275,14 @@ func (wd *WDADriver) Scale() (float64, error) {
}
type Screen struct {
StatusBarSize types.Size `json:"statusBarSize"`
Scale float64 `json:"scale"`
types.Size
Scale float64 `json:"scale"`
}
func (wd *WDADriver) Screen() (screen Screen, err error) {
// [[FBRoute GET:@"/wda/screen"] respondWithTarget:self action:@selector(handleGetScreen:)]
// [[FBRoute GET:@"/wings/window/size"] respondWithTarget:self action:@selector(handleGetScreen:)]
var rawResp DriverRawResponse
if rawResp, err = wd.Session.GET("/wda/screen"); err != nil {
if rawResp, err = wd.Session.GET("/wings/window/size"); err != nil {
return Screen{}, err
}
reply := new(struct{ Value struct{ Screen } })
@@ -298,12 +298,12 @@ func (wd *WDADriver) ScreenShot(opts ...option.ActionOption) (raw *bytes.Buffer,
// [[FBRoute GET:@"/screenshot"].withoutSession respondWithTarget:self action:@selector(handleGetScreenshot:)]
rawResp, err := wd.Session.GET("/screenshot")
if err != nil {
return nil, errors.Wrap(code.DeviceHTTPDriverError,
return nil, errors.Wrap(code.DeviceScreenShotError,
fmt.Sprintf("WDA screenshot failed %v", err))
}
raw, err = rawResp.ValueDecodeAsBase64()
if err != nil {
return nil, errors.Wrap(code.DeviceHTTPDriverError,
return nil, errors.Wrap(code.DeviceScreenShotError,
fmt.Sprintf("decode WDA screenshot data failed: %v", err))
}
return raw, nil
@@ -454,8 +454,7 @@ func (wd *WDADriver) AppLaunch(bundleId string) (err error) {
}
_, err = wd.Session.POST(data, "/wings/apps/launch")
if err != nil {
return errors.Wrap(code.DeviceHTTPDriverError,
fmt.Sprintf("wda launch failed: %v", err))
return errors.Wrap(err, "wda app launch failed")
}
return nil
}
@@ -466,8 +465,7 @@ func (wd *WDADriver) AppLaunchUnattached(bundleId string) (err error) {
data := map[string]interface{}{"bundleId": bundleId}
_, err = wd.Session.POST(data, "/wda/apps/launchUnattached")
if err != nil {
return errors.Wrap(code.DeviceHTTPDriverError,
fmt.Sprintf("wda launchUnattached failed: %v", err))
return errors.Wrap(err, "wda app launchUnattached failed")
}
return nil
}

View File

@@ -151,7 +151,8 @@ func (dExt *XTDriver) ExecuteAction(ctx context.Context, action option.MobileAct
// Execute via MCP tool
result, err := dExt.client.CallTool(ctx, req)
if err != nil {
return SessionData{}, fmt.Errorf("MCP tool call failed: %w", err)
// Notice: preserve the original error code
return SessionData{}, errors.Wrap(err, "call MCP tool failed")
}
// Check if the tool execution had business logic errors
@@ -169,9 +170,13 @@ func (dExt *XTDriver) ExecuteAction(ctx context.Context, action option.MobileAct
// For regular actions, collect session data and return it directly
sessionData := dExt.GetSession().GetData(true) // reset after getting data
log.Debug().Str("tool", string(tool.Name())).
Interface("result", result.Content).
Msg("executed action via MCP tool")
// Log execution result, but avoid printing base64 data for screenshot tools
logger := log.Debug().Str("tool", string(tool.Name()))
if tool.Name() != option.ACTION_ScreenShot {
logger.Interface("result", result.Content)
}
logger.Msg("executed action via MCP tool")
return sessionData, nil
}
@@ -254,22 +259,27 @@ func (dExt *XTDriver) CallMCPTool(ctx context.Context,
log.Debug().Err(err).
Str("server", serverName).
Str("tool", toolName).
Msg("MCP hook call failed")
return nil, err
Msg("call MCP tool failed")
return nil, errors.Wrap(err, "call MCP tool failed")
}
if result.IsError {
log.Debug().
logger := log.Debug().
Str("server", serverName).
Str("tool", toolName).
Interface("content", result.Content).
Msg("MCP hook returned error")
return nil, fmt.Errorf("MCP hook returned error")
Str("tool", toolName)
// Avoid printing base64 data for screenshot tools
if toolName != string(option.ACTION_ScreenShot) {
logger.Interface("content", result.Content)
}
logger.Msg("call MCP tool failed")
return nil, fmt.Errorf("call MCP tool %s failed", toolName)
}
log.Debug().
Str("server", serverName).
Str("tool", toolName).
Msg("MCP hook called successfully")
Msg("call MCP tool successfully")
return result, nil
}