mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 07:26:55 +08:00
add simulation
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
"github.com/mark3labs/mcp-go/server"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/internal/builtin"
|
||||
"github.com/httprunner/httprunner/v5/uixt/option"
|
||||
@@ -341,3 +342,95 @@ func (t *ToolDoubleTapXY) ConvertActionToCallToolRequest(action option.MobileAct
|
||||
}
|
||||
return mcp.CallToolRequest{}, fmt.Errorf("invalid double tap params: %v", action.Params)
|
||||
}
|
||||
|
||||
// ToolSIMClickAtPoint implements the sim_click_at_point tool call.
|
||||
type ToolSIMClickAtPoint struct {
|
||||
// Return data fields - these define the structure of data returned by this tool
|
||||
X float64 `json:"x" desc:"X coordinate where simulated click was performed"`
|
||||
Y float64 `json:"y" desc:"Y coordinate where simulated click was performed"`
|
||||
}
|
||||
|
||||
func (t *ToolSIMClickAtPoint) Name() option.ActionName {
|
||||
return option.ACTION_SIMClickAtPoint
|
||||
}
|
||||
|
||||
func (t *ToolSIMClickAtPoint) Description() string {
|
||||
return "Perform simulated click at specified point with human-like touch patterns"
|
||||
}
|
||||
|
||||
func (t *ToolSIMClickAtPoint) Options() []mcp.ToolOption {
|
||||
unifiedReq := &option.ActionOptions{}
|
||||
return unifiedReq.GetMCPOptions(option.ACTION_SIMClickAtPoint)
|
||||
}
|
||||
|
||||
func (t *ToolSIMClickAtPoint) Implement() server.ToolHandlerFunc {
|
||||
return func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
arguments := request.GetArguments()
|
||||
driverExt, err := setupXTDriver(ctx, arguments)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("setup driver failed: %w", err)
|
||||
}
|
||||
|
||||
unifiedReq, err := parseActionOptions(arguments)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Validate required parameters
|
||||
if unifiedReq.X == 0 || unifiedReq.Y == 0 {
|
||||
return nil, fmt.Errorf("x and y coordinates are required")
|
||||
}
|
||||
|
||||
x := unifiedReq.X
|
||||
y := unifiedReq.Y
|
||||
|
||||
log.Info().
|
||||
Float64("x", x).
|
||||
Float64("y", y).
|
||||
Msg("performing simulated click at point")
|
||||
|
||||
// Build all options from request arguments
|
||||
opts := unifiedReq.Options()
|
||||
|
||||
// Call the underlying SIMClickAtPoint method (Android UIA2 specific)
|
||||
if uia2Driver, ok := driverExt.IDriver.(*UIA2Driver); ok {
|
||||
err = uia2Driver.SIMClickAtPoint(x, y, opts...)
|
||||
if err != nil {
|
||||
return NewMCPErrorResponse(fmt.Sprintf("Simulated click failed: %s", err.Error())), err
|
||||
}
|
||||
} else {
|
||||
return NewMCPErrorResponse("SIMClickAtPoint is only supported on Android UIA2 driver"), fmt.Errorf("unsupported driver type for SIMClickAtPoint")
|
||||
}
|
||||
|
||||
message := fmt.Sprintf("Successfully performed simulated click at (%.2f, %.2f)", x, y)
|
||||
returnData := ToolSIMClickAtPoint{
|
||||
X: x,
|
||||
Y: y,
|
||||
}
|
||||
|
||||
return NewMCPSuccessResponse(message, &returnData), nil
|
||||
}
|
||||
}
|
||||
|
||||
func (t *ToolSIMClickAtPoint) ConvertActionToCallToolRequest(action option.MobileAction) (mcp.CallToolRequest, error) {
|
||||
// Handle params as map[string]interface{}
|
||||
if paramsMap, ok := action.Params.(map[string]interface{}); ok {
|
||||
arguments := map[string]any{}
|
||||
|
||||
// Extract coordinates
|
||||
if x, exists := paramsMap["x"]; exists {
|
||||
arguments["x"] = x
|
||||
}
|
||||
if y, exists := paramsMap["y"]; exists {
|
||||
arguments["y"] = y
|
||||
}
|
||||
|
||||
// Add duration from options
|
||||
if duration := action.ActionOptions.Duration; duration > 0 {
|
||||
arguments["duration"] = duration
|
||||
}
|
||||
|
||||
return BuildMCPCallToolRequest(t.Name(), arguments, action), nil
|
||||
}
|
||||
return mcp.CallToolRequest{}, fmt.Errorf("invalid SIM click at point params: %v", action.Params)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user