From 1bd2b1ba5e5029f40edcfc966784cff5c50eab61 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Mon, 26 May 2025 16:08:27 +0800 Subject: [PATCH] change: move code --- internal/version/VERSION | 2 +- server/context.go | 2 +- uixt/browser_device.go | 4 +++ uixt/cache.go | 2 +- uixt/mcp_server.go | 61 ------------------------------------- uixt/sdk.go | 66 ++++++++++++++++++++++++++++++++++++++-- 6 files changed, 70 insertions(+), 67 deletions(-) diff --git a/internal/version/VERSION b/internal/version/VERSION index d32d071a..34009e02 100644 --- a/internal/version/VERSION +++ b/internal/version/VERSION @@ -1 +1 @@ -v5.0.0-beta-2505261530 +v5.0.0-beta-2505261608 diff --git a/server/context.go b/server/context.go index 3d4cd65a..b4f9fd8d 100644 --- a/server/context.go +++ b/server/context.go @@ -44,7 +44,7 @@ func (r *Router) GetDriver(c *gin.Context) (driverExt *uixt.XTDriver, err error) func (r *Router) GetDevice(c *gin.Context) (device uixt.IDevice, err error) { platform := c.Param("platform") serial := c.Param("serial") - device, err = uixt.NewDevice(platform, serial) + device, err = uixt.NewDeviceWithDefault(platform, serial) if err != nil { RenderErrorInitDriver(c, err) return diff --git a/uixt/browser_device.go b/uixt/browser_device.go index 533cd6da..8e0231a5 100644 --- a/uixt/browser_device.go +++ b/uixt/browser_device.go @@ -2,6 +2,7 @@ package uixt import ( "bytes" + "fmt" "github.com/pkg/errors" "github.com/rs/zerolog/log" @@ -30,6 +31,9 @@ func NewBrowserDevice(opts ...option.BrowserDeviceOption) (device *BrowserDevice } log.Info().Str("browserID", device.Options.BrowserID).Msg("init browser device") + if err := device.Setup(); err != nil { + return nil, fmt.Errorf("setup browser device failed: %w", err) + } return device, nil } diff --git a/uixt/cache.go b/uixt/cache.go index 6a1be54f..0d4bbb98 100644 --- a/uixt/cache.go +++ b/uixt/cache.go @@ -27,7 +27,7 @@ func setupXTDriver(_ context.Context, args map[string]any) (*XTDriver, error) { } } - driverExt, err := NewDriverExt(platform, serial) + driverExt, err := NewXTDriverWithDefault(platform, serial) if err != nil { return nil, err } diff --git a/uixt/mcp_server.go b/uixt/mcp_server.go index 265f183d..e338a885 100644 --- a/uixt/mcp_server.go +++ b/uixt/mcp_server.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "strings" "time" "github.com/danielpaulus/go-ios/ios" @@ -1190,66 +1189,6 @@ func (t *ToolDrag) ConvertActionToCallToolRequest(action MobileAction) (mcp.Call return mcp.CallToolRequest{}, fmt.Errorf("invalid drag params: %v", action.Params) } -func NewDriverExt(platform, serial string) (*XTDriver, error) { - device, err := NewDevice(platform, serial) - if err != nil { - return nil, err - } - - // init driver - driver, err := device.NewDriver() - if err != nil { - return nil, fmt.Errorf("init driver failed: %w", err) - } - if err := driver.Setup(); err != nil { - return nil, fmt.Errorf("setup driver failed: %w", err) - } - - // init XTDriver - driverExt, err := NewXTDriver(driver, - option.WithCVService(option.CVServiceTypeVEDEM)) - if err != nil { - return nil, fmt.Errorf("init XT driver failed: %w", err) - } - return driverExt, nil -} - -func NewDevice(platform, serial string) (device IDevice, err error) { - if serial == "" { - return nil, fmt.Errorf("serial is empty") - } - switch strings.ToLower(platform) { - case "android": - device, err = NewAndroidDevice( - option.WithSerialNumber(serial)) - if err != nil { - return - } - case "ios": - device, err = NewIOSDevice( - option.WithUDID(serial), - option.WithWDAPort(8700), - option.WithWDAMjpegPort(8800), - option.WithResetHomeOnStartup(false), - ) - if err != nil { - return - } - case "browser": - device, err = NewBrowserDevice(option.WithBrowserID(serial)) - if err != nil { - return - } - default: - return nil, fmt.Errorf("invalid platform: %s", platform) - } - err = device.Setup() - if err != nil { - log.Error().Err(err).Msg("setup device failed") - } - return device, nil -} - // mapToStruct convert map[string]any to target struct func mapToStruct(m map[string]any, out interface{}) error { b, err := json.Marshal(m) diff --git a/uixt/sdk.go b/uixt/sdk.go index 985706b7..caa6b209 100644 --- a/uixt/sdk.go +++ b/uixt/sdk.go @@ -3,6 +3,7 @@ package uixt import ( "context" "fmt" + "strings" "github.com/httprunner/httprunner/v5/uixt/ai" "github.com/httprunner/httprunner/v5/uixt/option" @@ -107,8 +108,67 @@ func (dExt *XTDriver) ExecuteAction(action MobileAction) (err error) { return fmt.Errorf("invoke tool %s failed", tool.Name()) } - log.Debug().Str("method", string(action.Method)). - Str("tool", string(tool.Name())). - Msg("executed action via MCP tool") + log.Debug().Str("tool", string(tool.Name())). + Msg("execute action via MCP tool") return nil } + +// NewXTDriverWithDefault is a helper function to create a XTDriver with default options +func NewXTDriverWithDefault(platform, serial string) (*XTDriver, error) { + device, err := NewDeviceWithDefault(platform, serial) + if err != nil { + return nil, err + } + + // init driver + driver, err := device.NewDriver() + if err != nil { + return nil, fmt.Errorf("init driver failed: %w", err) + } + if err := driver.Setup(); err != nil { + return nil, fmt.Errorf("setup driver failed: %w", err) + } + + // init XTDriver + driverExt, err := NewXTDriver(driver, + option.WithCVService(option.CVServiceTypeVEDEM)) + if err != nil { + return nil, fmt.Errorf("init XT driver failed: %w", err) + } + return driverExt, nil +} + +// NewDeviceWithDefault is a helper function to create a device with default options +func NewDeviceWithDefault(platform, serial string) (device IDevice, err error) { + if serial == "" { + return nil, fmt.Errorf("serial is empty") + } + + switch strings.ToLower(platform) { + case "android": + device, err = NewAndroidDevice( + option.WithSerialNumber(serial)) + if err != nil { + return + } + case "ios": + device, err = NewIOSDevice( + option.WithUDID(serial), + option.WithWDAPort(8700), + option.WithWDAMjpegPort(8800), + option.WithResetHomeOnStartup(false), + ) + if err != nil { + return + } + case "browser": + device, err = NewBrowserDevice(option.WithBrowserID(serial)) + if err != nil { + return + } + default: + return nil, fmt.Errorf("invalid platform: %s", platform) + } + + return device, nil +}