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

@@ -1 +1 @@
v5.0.0-250709
v5.0.0-250717

211
report.go
View File

@@ -635,7 +635,7 @@ const htmlTemplate = `<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HttpRunner Test Report</title>
<title>Wings Test Report</title>
<style>
* {
margin: 0;
@@ -1194,6 +1194,15 @@ const htmlTemplate = `<!DOCTYPE html>
padding: 12px;
}
.screenshot-display .screenshot-image {
min-height: 300px;
padding: 15px 0;
}
.screenshot-display .screenshot-image img {
max-height: 350px;
}
.screenshot-item-compact {
text-align: center;
}
@@ -1212,7 +1221,7 @@ const htmlTemplate = `<!DOCTYPE html>
.screenshot-item-compact .screenshot-image img {
width: 100%;
height: auto;
max-height: 500px;
max-height: 350px;
border-radius: 4px;
cursor: pointer;
transition: transform 0.2s;
@@ -1223,7 +1232,7 @@ const htmlTemplate = `<!DOCTYPE html>
/* Handle very tall screenshots */
.screenshot-item-compact .screenshot-image img[style*="height"] {
max-height: 400px;
max-height: 350px;
width: auto;
max-width: 100%;
}
@@ -1838,101 +1847,6 @@ const htmlTemplate = `<!DOCTYPE html>
word-wrap: break-word;
}
/* AI Assertion Styles */
.ai-assertion-section {
margin-top: 15px;
padding: 15px;
background: linear-gradient(135deg, #f0f8ff 0%, #f5f5ff 100%);
border: 2px solid #4169e1;
border-radius: 12px;
box-shadow: 0 4px 8px rgba(65, 105, 225, 0.15);
}
.ai-assertion-section h5 {
margin: 0 0 15px 0;
color: #4169e1;
font-size: 1.1em;
font-weight: 600;
}
.ai-screenshot-container {
background: white;
border: 1px solid #dee2e6;
border-radius: 8px;
padding: 12px;
margin-bottom: 15px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.ai-screenshot {
text-align: center;
margin-top: 10px;
}
.ai-screenshot img {
max-width: 100%;
height: auto;
border-radius: 8px;
border: 1px solid #dee2e6;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
cursor: pointer;
transition: transform 0.2s;
}
.ai-screenshot img:hover {
transform: scale(1.02);
}
.ai-analysis-container {
background: white;
border: 1px solid #dee2e6;
border-radius: 8px;
padding: 12px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.ai-analysis-content {
margin-top: 10px;
}
.ai-thought {
background: linear-gradient(135deg, #e8f4fd 0%, #f0f8ff 100%);
border: 1px solid #4169e1;
border-radius: 8px;
padding: 12px;
margin: 10px 0;
color: #2c3e50;
}
.ai-thought .thought-content {
margin-top: 8px;
font-style: italic;
color: #34495e;
white-space: pre-wrap;
word-wrap: break-word;
}
.ai-raw-response {
background: #f8f9fa;
border: 1px solid #dee2e6;
border-radius: 8px;
padding: 12px;
margin: 10px 0;
color: #2c3e50;
}
.ai-raw-response .response-content {
margin-top: 8px;
font-family: monospace;
font-size: 0.9em;
background: white;
padding: 8px;
border-radius: 4px;
border: 1px solid #e9ecef;
white-space: pre-wrap;
word-wrap: break-word;
}
@media screen and (max-width: 768px) {
.validator-ai-layout {
flex-direction: column;
@@ -2575,7 +2489,7 @@ const htmlTemplate = `<!DOCTYPE html>
<div class="header">
<div class="header-content">
<div class="header-left">
<h1>🚀 HttpRunner Test Report</h1>
<h1>🚀 Wings Test Report</h1>
<div class="subtitle">Start Time: {{.Time.StartAt.Format "2006-01-02 15:04:05"}}</div>
</div>
<div class="header-right">
@@ -2891,10 +2805,8 @@ const htmlTemplate = `<!DOCTYPE html>
<div class="screenshot-display">
{{$base64Image := encodeImageBase64 $action.AIResult.ImagePath}}
{{if $base64Image}}
<div class="screenshot-item-compact">
<div class="screenshot-image">
<img src="data:image/jpeg;base64,{{$base64Image}}" alt="AI {{title $action.AIResult.Type}} Screenshot" onclick="openImageModal(this.src)" />
</div>
<div class="screenshot-image">
<img src="data:image/jpeg;base64,{{$base64Image}}" alt="AI {{title $action.AIResult.Type}} Screenshot" onclick="openImageModal(this.src)" />
</div>
{{end}}
</div>
@@ -3048,53 +2960,60 @@ const htmlTemplate = `<!DOCTYPE html>
<!-- AI Assertion Results -->
{{if $validator.ai_result}}
<div class="ai-assertion-section">
<h5>🤖 AI Assertion Details</h5>
<!-- AI Assertion Screenshot -->
{{if $validator.ai_result.image_path}}
<div class="ai-screenshot-container">
<span class="step-name">📸 AI Assertion Screenshot</span>
{{if $validator.ai_result.screenshot_elapsed}}
<span class="duration">{{formatDuration $validator.ai_result.screenshot_elapsed}}</span>
{{end}}
<div class="ai-screenshot">
{{$base64Image := encodeImageBase64 $validator.ai_result.image_path}}
{{if $base64Image}}
<img src="data:image/jpeg;base64,{{$base64Image}}" alt="AI Assertion Screenshot" onclick="openImageModal(this.src)" />
{{end}}
</div>
</div>
<div class="validator-ai-content">
<!-- Display AI Thought -->
{{if $validator.ai_result.assertion_result.thought}}
<div class="thought">{{$validator.ai_result.assertion_result.thought}}</div>
{{end}}
<!-- AI Model Analysis -->
<div class="ai-analysis-container">
<span class="step-name">🧠 AI Model Analysis</span>
{{if $validator.ai_result.model_call_elapsed}}
<span class="duration">{{formatDuration $validator.ai_result.model_call_elapsed}}</span>
<!-- AI Assertion Layout: Screenshot left, Analysis right -->
<div class="validator-ai-layout">
<!-- Left column: Screenshot -->
{{if $validator.ai_result.image_path}}
<div class="validator-column-screenshot">
<div class="validator-step-compact">
<div class="step-header-compact">
<span class="step-name">📸 AI Assertion Screenshot</span>
{{if $validator.ai_result.screenshot_elapsed}}
<span class="duration">{{formatDuration $validator.ai_result.screenshot_elapsed}}</span>
{{end}}
</div>
<div class="screenshot-display">
{{$base64Image := encodeImageBase64 $validator.ai_result.image_path}}
{{if $base64Image}}
<div class="screenshot-image">
<img src="data:image/jpeg;base64,{{$base64Image}}" alt="AI Assertion Screenshot" onclick="openImageModal(this.src)" />
</div>
{{end}}
</div>
</div>
</div>
{{end}}
<div class="ai-analysis-content">
{{if $validator.ai_result.assertion_result.model_name}}
<div class="model-info">🤖 Model: {{$validator.ai_result.assertion_result.model_name}}</div>
{{end}}
{{if $validator.ai_result.assertion_result.usage}}
<div class="usage-info">📊 Tokens: {{$validator.ai_result.assertion_result.usage.PromptTokens}} in / {{$validator.ai_result.assertion_result.usage.CompletionTokens}} out / {{$validator.ai_result.assertion_result.usage.TotalTokens}} total</div>
{{end}}
{{if $validator.ai_result.resolution}}
<div class="model-info">📐 Resolution: {{$validator.ai_result.resolution.Width}}x{{$validator.ai_result.resolution.Height}}</div>
{{end}}
{{if $validator.ai_result.assertion_result.thought}}
<div class="ai-thought">
<strong>💭 AI Reasoning:</strong>
<div class="thought-content">{{$validator.ai_result.assertion_result.thought}}</div>
<!-- Right column: AI Analysis -->
<div class="validator-column-analysis">
<div class="validator-step-compact">
<div class="step-header-compact">
<span class="step-name">🤖 AI Assertion Analysis</span>
{{if $validator.ai_result.model_call_elapsed}}
<span class="duration">{{formatDuration $validator.ai_result.model_call_elapsed}}</span>
{{end}}
</div>
<div class="validator-ai-details">
{{if $validator.ai_result.assertion_result.model_name}}
<div class="model-info">🤖 Model: {{$validator.ai_result.assertion_result.model_name}}</div>
{{end}}
{{if $validator.ai_result.assertion_result.usage}}
<div class="usage-info">📊 Tokens: {{$validator.ai_result.assertion_result.usage.PromptTokens}} in / {{$validator.ai_result.assertion_result.usage.CompletionTokens}} out / {{$validator.ai_result.assertion_result.usage.TotalTokens}} total</div>
{{end}}
{{if $validator.ai_result.resolution}}
<div class="model-info">📐 Resolution: {{$validator.ai_result.resolution.Width}}x{{$validator.ai_result.resolution.Height}}</div>
{{end}}
{{if $validator.ai_result.assertion_result.content}}
<div class="model-info">💬 Assertion Result: {{$validator.ai_result.assertion_result.content}}</div>
{{end}}
</div>
</div>
{{end}}
{{if $validator.ai_result.assertion_result.content}}
<div class="ai-raw-response">
<strong>📝 Raw Model Response:</strong>
<div class="response-content">{{$validator.ai_result.assertion_result.content}}</div>
</div>
{{end}}
</div>
</div>
</div>

View File

@@ -878,9 +878,6 @@ func (r *SessionRunner) RunStep(step IStep) (stepResult *StepResult, err error)
}
}
stepName := step.Name()
stepType := string(step.Type())
// execute step with parameters iterator
tasks, err := r.generateExecutionTasks(step)
if err != nil {
@@ -899,30 +896,10 @@ func (r *SessionRunner) RunStep(step IStep) (stepResult *StepResult, err error)
r.sessionVariables[k] = v
}
}
// log final result
if err == nil && stepResult.Success {
log.Info().Str("step", stepName).
Str("type", stepType).
Bool("success", true).
Int64("elapsed(ms)", stepResult.Elapsed).
Interface("exportVars", stepResult.ExportVars).
Msg(RUN_STEP_END)
} else if stepResult != nil {
log.Error().Str("step", stepName).
Str("type", stepType).
Bool("success", false).
Int64("elapsed(ms)", stepResult.Elapsed).
Int("completed_tasks", len(stepResults)).
Int("total_tasks", len(tasks)).
Msg(RUN_STEP_END)
}
}()
// execute with loops as outer iteration
for _, task := range tasks {
log.Info().Str("step", task.stepName).Str("type", stepType).Msg(RUN_STEP_START)
// Check for interrupt signal before each parameter iteration
select {
case <-r.caseRunner.hrpRunner.interruptSignal:
@@ -967,6 +944,24 @@ func (r *SessionRunner) RunStep(step IStep) (stepResult *StepResult, err error)
// executeStepWithVariables executes a single step with given parameters
// parameters will override step variables with the same name
func (r *SessionRunner) executeStepWithVariables(step IStep, stepName string, parameters map[string]interface{}) (stepResult *StepResult, err error) {
stepType := string(step.Type())
log.Info().Str("step", stepName).Str("type", stepType).Msg(RUN_STEP_START)
defer func() {
if err == nil && stepResult.Success {
log.Info().Str("step", stepName).
Str("type", stepType).
Bool("success", true).
Int64("elapsed(ms)", stepResult.Elapsed).
Msg(RUN_STEP_END)
} else {
log.Error().Str("step", stepName).
Str("type", stepType).
Bool("success", false).
Int64("elapsed(ms)", stepResult.Elapsed).
Msg(RUN_STEP_END)
}
}()
stepConfig := step.Config()
// backup original variables

View File

@@ -37,6 +37,7 @@ type StepConfig struct {
Validators []interface{} `json:"validate,omitempty" yaml:"validate,omitempty"`
StepExport []string `json:"export,omitempty" yaml:"export,omitempty"`
Loops int `json:"loops,omitempty" yaml:"loops,omitempty"`
IgnorePopup bool `json:"ignore_popup,omitempty" yaml:"ignore_popup,omitempty"` // ignore popup for this step, keep for compatibility
AutoPopupHandler bool `json:"auto_popup_handler,omitempty" yaml:"auto_popup_handler,omitempty"` // enable auto popup handler for this step
}

View File

@@ -715,6 +715,7 @@ func runStepMobileUI(s *SessionRunner, step IStep) (stepResult *StepResult, err
var stepVariables map[string]interface{}
var stepValidators []interface{}
var stepAutoPopupHandler bool
var stepIgnorePopup bool
var mobileStep *MobileUI
switch stepMobile := step.(type) {
@@ -722,11 +723,13 @@ func runStepMobileUI(s *SessionRunner, step IStep) (stepResult *StepResult, err
mobileStep = stepMobile.obj()
stepVariables = stepMobile.Variables
stepAutoPopupHandler = stepMobile.AutoPopupHandler
stepIgnorePopup = stepMobile.IgnorePopup
case *StepMobileUIValidation:
mobileStep = stepMobile.obj()
stepVariables = stepMobile.Variables
stepValidators = stepMobile.Validators
stepAutoPopupHandler = stepMobile.StepMobile.AutoPopupHandler
stepIgnorePopup = stepMobile.StepMobile.IgnorePopup
default:
return stepResult, errors.New("invalid mobile UI step type")
}
@@ -794,10 +797,14 @@ func runStepMobileUI(s *SessionRunner, step IStep) (stepResult *StepResult, err
if s.caseRunner != nil && s.caseRunner.Config != nil {
config = s.caseRunner.Config.Get()
}
// automatic handling of pop-up windows on each step finished
// priority: testcase config > step config, default to disabled
// automatic handling of pop-up windows on each step finished, default to disabled
// priority: step ignore_popup > config auto_popup_handler > step auto_popup_handler
shouldHandlePopup := false
if config != nil && config.AutoPopupHandler {
if stepIgnorePopup {
// step level config, keep for compatibility
shouldHandlePopup = false
} else if config != nil && config.AutoPopupHandler {
// testcase level config has higher priority
shouldHandlePopup = true
} else if stepAutoPopupHandler {

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
}