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
+1 -1
View File
@@ -1 +1 @@
v5.0.0-250709 v5.0.0-250717
+65 -146
View File
@@ -635,7 +635,7 @@ const htmlTemplate = `<!DOCTYPE html>
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HttpRunner Test Report</title> <title>Wings Test Report</title>
<style> <style>
* { * {
margin: 0; margin: 0;
@@ -1194,6 +1194,15 @@ const htmlTemplate = `<!DOCTYPE html>
padding: 12px; padding: 12px;
} }
.screenshot-display .screenshot-image {
min-height: 300px;
padding: 15px 0;
}
.screenshot-display .screenshot-image img {
max-height: 350px;
}
.screenshot-item-compact { .screenshot-item-compact {
text-align: center; text-align: center;
} }
@@ -1212,7 +1221,7 @@ const htmlTemplate = `<!DOCTYPE html>
.screenshot-item-compact .screenshot-image img { .screenshot-item-compact .screenshot-image img {
width: 100%; width: 100%;
height: auto; height: auto;
max-height: 500px; max-height: 350px;
border-radius: 4px; border-radius: 4px;
cursor: pointer; cursor: pointer;
transition: transform 0.2s; transition: transform 0.2s;
@@ -1223,7 +1232,7 @@ const htmlTemplate = `<!DOCTYPE html>
/* Handle very tall screenshots */ /* Handle very tall screenshots */
.screenshot-item-compact .screenshot-image img[style*="height"] { .screenshot-item-compact .screenshot-image img[style*="height"] {
max-height: 400px; max-height: 350px;
width: auto; width: auto;
max-width: 100%; max-width: 100%;
} }
@@ -1838,101 +1847,6 @@ const htmlTemplate = `<!DOCTYPE html>
word-wrap: break-word; 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) { @media screen and (max-width: 768px) {
.validator-ai-layout { .validator-ai-layout {
flex-direction: column; flex-direction: column;
@@ -2575,7 +2489,7 @@ const htmlTemplate = `<!DOCTYPE html>
<div class="header"> <div class="header">
<div class="header-content"> <div class="header-content">
<div class="header-left"> <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 class="subtitle">Start Time: {{.Time.StartAt.Format "2006-01-02 15:04:05"}}</div>
</div> </div>
<div class="header-right"> <div class="header-right">
@@ -2891,10 +2805,8 @@ const htmlTemplate = `<!DOCTYPE html>
<div class="screenshot-display"> <div class="screenshot-display">
{{$base64Image := encodeImageBase64 $action.AIResult.ImagePath}} {{$base64Image := encodeImageBase64 $action.AIResult.ImagePath}}
{{if $base64Image}} {{if $base64Image}}
<div class="screenshot-item-compact"> <div class="screenshot-image">
<div class="screenshot-image"> <img src="data:image/jpeg;base64,{{$base64Image}}" alt="AI {{title $action.AIResult.Type}} Screenshot" onclick="openImageModal(this.src)" />
<img src="data:image/jpeg;base64,{{$base64Image}}" alt="AI {{title $action.AIResult.Type}} Screenshot" onclick="openImageModal(this.src)" />
</div>
</div> </div>
{{end}} {{end}}
</div> </div>
@@ -3048,53 +2960,60 @@ const htmlTemplate = `<!DOCTYPE html>
<!-- AI Assertion Results --> <!-- AI Assertion Results -->
{{if $validator.ai_result}} {{if $validator.ai_result}}
<div class="ai-assertion-section"> <div class="validator-ai-content">
<h5>🤖 AI Assertion Details</h5> <!-- Display AI Thought -->
{{if $validator.ai_result.assertion_result.thought}}
<!-- AI Assertion Screenshot --> <div class="thought">{{$validator.ai_result.assertion_result.thought}}</div>
{{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>
{{end}} {{end}}
<!-- AI Model Analysis --> <!-- AI Assertion Layout: Screenshot left, Analysis right -->
<div class="ai-analysis-container"> <div class="validator-ai-layout">
<span class="step-name">🧠 AI Model Analysis</span> <!-- Left column: Screenshot -->
{{if $validator.ai_result.model_call_elapsed}} {{if $validator.ai_result.image_path}}
<span class="duration">{{formatDuration $validator.ai_result.model_call_elapsed}}</span> <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}} {{end}}
<div class="ai-analysis-content">
{{if $validator.ai_result.assertion_result.model_name}} <!-- Right column: AI Analysis -->
<div class="model-info">🤖 Model: {{$validator.ai_result.assertion_result.model_name}}</div> <div class="validator-column-analysis">
{{end}} <div class="validator-step-compact">
{{if $validator.ai_result.assertion_result.usage}} <div class="step-header-compact">
<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> <span class="step-name">🤖 AI Assertion Analysis</span>
{{end}} {{if $validator.ai_result.model_call_elapsed}}
{{if $validator.ai_result.resolution}} <span class="duration">{{formatDuration $validator.ai_result.model_call_elapsed}}</span>
<div class="model-info">📐 Resolution: {{$validator.ai_result.resolution.Width}}x{{$validator.ai_result.resolution.Height}}</div> {{end}}
{{end}} </div>
{{if $validator.ai_result.assertion_result.thought}} <div class="validator-ai-details">
<div class="ai-thought"> {{if $validator.ai_result.assertion_result.model_name}}
<strong>💭 AI Reasoning:</strong> <div class="model-info">🤖 Model: {{$validator.ai_result.assertion_result.model_name}}</div>
<div class="thought-content">{{$validator.ai_result.assertion_result.thought}}</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> </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> </div>
</div> </div>
+18 -23
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 // execute step with parameters iterator
tasks, err := r.generateExecutionTasks(step) tasks, err := r.generateExecutionTasks(step)
if err != nil { if err != nil {
@@ -899,30 +896,10 @@ func (r *SessionRunner) RunStep(step IStep) (stepResult *StepResult, err error)
r.sessionVariables[k] = v 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 // execute with loops as outer iteration
for _, task := range tasks { 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 // Check for interrupt signal before each parameter iteration
select { select {
case <-r.caseRunner.hrpRunner.interruptSignal: 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 // executeStepWithVariables executes a single step with given parameters
// parameters will override step variables with the same name // 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) { 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() stepConfig := step.Config()
// backup original variables // backup original variables
+1
View File
@@ -37,6 +37,7 @@ type StepConfig struct {
Validators []interface{} `json:"validate,omitempty" yaml:"validate,omitempty"` Validators []interface{} `json:"validate,omitempty" yaml:"validate,omitempty"`
StepExport []string `json:"export,omitempty" yaml:"export,omitempty"` StepExport []string `json:"export,omitempty" yaml:"export,omitempty"`
Loops int `json:"loops,omitempty" yaml:"loops,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 AutoPopupHandler bool `json:"auto_popup_handler,omitempty" yaml:"auto_popup_handler,omitempty"` // enable auto popup handler for this step
} }
+10 -3
View File
@@ -715,6 +715,7 @@ func runStepMobileUI(s *SessionRunner, step IStep) (stepResult *StepResult, err
var stepVariables map[string]interface{} var stepVariables map[string]interface{}
var stepValidators []interface{} var stepValidators []interface{}
var stepAutoPopupHandler bool var stepAutoPopupHandler bool
var stepIgnorePopup bool
var mobileStep *MobileUI var mobileStep *MobileUI
switch stepMobile := step.(type) { switch stepMobile := step.(type) {
@@ -722,11 +723,13 @@ func runStepMobileUI(s *SessionRunner, step IStep) (stepResult *StepResult, err
mobileStep = stepMobile.obj() mobileStep = stepMobile.obj()
stepVariables = stepMobile.Variables stepVariables = stepMobile.Variables
stepAutoPopupHandler = stepMobile.AutoPopupHandler stepAutoPopupHandler = stepMobile.AutoPopupHandler
stepIgnorePopup = stepMobile.IgnorePopup
case *StepMobileUIValidation: case *StepMobileUIValidation:
mobileStep = stepMobile.obj() mobileStep = stepMobile.obj()
stepVariables = stepMobile.Variables stepVariables = stepMobile.Variables
stepValidators = stepMobile.Validators stepValidators = stepMobile.Validators
stepAutoPopupHandler = stepMobile.StepMobile.AutoPopupHandler stepAutoPopupHandler = stepMobile.StepMobile.AutoPopupHandler
stepIgnorePopup = stepMobile.StepMobile.IgnorePopup
default: default:
return stepResult, errors.New("invalid mobile UI step type") 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 { if s.caseRunner != nil && s.caseRunner.Config != nil {
config = s.caseRunner.Config.Get() config = s.caseRunner.Config.Get()
} }
// automatic handling of pop-up windows on each step finished // automatic handling of pop-up windows on each step finished, default to disabled
// priority: testcase config > step config, default to disabled // priority: step ignore_popup > config auto_popup_handler > step auto_popup_handler
shouldHandlePopup := false 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 // testcase level config has higher priority
shouldHandlePopup = true shouldHandlePopup = true
} else if stepAutoPopupHandler { } else if stepAutoPopupHandler {
+93 -48
View File
@@ -138,7 +138,7 @@ func (wd *BrowserDriver) Drag(fromX, fromY, toX, toY float64, options ...option.
data["duration"] = 0.5 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 return
} }
@@ -146,7 +146,7 @@ func (wd *BrowserDriver) AppLaunch(packageName string) (err error) {
data := map[string]interface{}{ data := map[string]interface{}{
"url": packageName, "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 return err
} }
@@ -185,7 +185,7 @@ func (wd *BrowserDriver) Scroll(delta int) (err error) {
data := map[string]interface{}{ data := map[string]interface{}{
"delta": delta, "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 return err
} }
@@ -210,7 +210,7 @@ func (wd *BrowserDriver) CloseTab(pageIndex int) (err error) {
"page_index": pageIndex, "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 return err
} }
@@ -223,7 +223,7 @@ func (wd *BrowserDriver) HoverBySelector(selector string, options ...option.Acti
if actionOptions.Index > 0 { if actionOptions.Index > 0 {
data["element_index"] = actionOptions.Index 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 return err
} }
@@ -236,7 +236,7 @@ func (wd *BrowserDriver) TapBySelector(selector string, options ...option.Action
if actionOptions.Index > 0 { if actionOptions.Index > 0 {
data["element_index"] = actionOptions.Index 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 return err
} }
@@ -246,7 +246,7 @@ func (wd *BrowserDriver) SecondaryClick(x, y float64) (err error) {
"x": x, "x": x,
"y": y, "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 return err
} }
@@ -259,7 +259,7 @@ func (wd *BrowserDriver) SecondaryClickBySelector(selector string, options ...op
if actionOptions.Index > 0 { if actionOptions.Index > 0 {
data["element_index"] = actionOptions.Index 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 return err
} }
@@ -315,28 +315,21 @@ func (wd *BrowserDriver) GetPageUrl(options ...option.ActionOption) (text string
if actionOptions.Index > 0 { if actionOptions.Index > 0 {
uri = uri + "?page_index=" + fmt.Sprintf("%v", actionOptions.Index) 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 { if err != nil {
return "", err return "", err
} }
data, err := resp.ValueConvertToJsonObject() data := resp.Data.(map[string]interface{})
if err != nil {
return "", err
}
data = data["data"].(map[string]interface{})
return data["url"].(string), nil return data["url"].(string), nil
} }
func (wd *BrowserDriver) IsElementExistBySelector(selector string) (bool, error) { 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 { if err != nil {
return false, err return false, err
} }
data, err := resp.ValueConvertToJsonObject()
if err != nil { data := resp.Data.(map[string]interface{})
return false, err
}
data = data["data"].(map[string]interface{})
return data["exist"].(bool), nil return data["exist"].(bool), nil
} }
@@ -345,7 +338,7 @@ func (wd *BrowserDriver) LoginNoneUI(packageName, phoneNumber string, captcha, p
"url": packageName, "url": packageName,
"web_cookie": password, "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 { if err != nil {
return false, err return false, err
} }
@@ -357,7 +350,7 @@ func (wd *BrowserDriver) Hover(x, y float64) (err error) {
"x": x, "x": x,
"y": y, "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 return err
} }
@@ -365,37 +358,32 @@ func (wd *BrowserDriver) Input(text string, option ...option.ActionOption) (err
data := map[string]interface{}{ data := map[string]interface{}{
"text": text, "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 return err
} }
// Source Return application elements tree // Source Return application elements tree
func (wd *BrowserDriver) Source(srcOpt ...option.SourceOption) (string, error) { 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 { if err != nil {
return "", err 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) { 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 { if err != nil {
return nil, err 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{}) data := result.Data.(map[string]interface{})
screenshotBase64 := data["screenshot"].(string) screenshotBase64 := data["screenshot"].(string)
screenRaw, err := base64.StdEncoding.DecodeString(screenshotBase64) 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) { 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 { if err != nil {
return types.Size{}, err return types.Size{}, err
} }
data, err := resp.ValueConvertToJsonObject() data := resp.Data.(map[string]interface{})
if err != nil {
return types.Size{}, err
}
data = data["data"].(map[string]interface{})
width := data["width"] width := data["width"]
height := data["height"] height := data["height"]
return types.Size{ return types.Size{
@@ -532,7 +516,7 @@ func (wd *BrowserDriver) TapFloat(x, y float64, opts ...option.ActionOption) err
"duration": duration, "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 return err
} }
@@ -550,7 +534,7 @@ func (wd *BrowserDriver) DoubleTap(x, y float64, options ...option.ActionOption)
"y": y, "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 return err
} }
@@ -561,7 +545,7 @@ func (wd *BrowserDriver) UploadFile(x, y float64, FileUrl, FileFormat string) (e
"file_url": FileUrl, "file_url": FileUrl,
"file_format": FileFormat, "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 return err
} }
@@ -607,7 +591,7 @@ func (wd *BrowserDriver) ForegroundInfo() (app types.AppInfo, err error) {
// PressBack Presses the back button // PressBack Presses the back button
func (wd *BrowserDriver) PressBack(options ...option.ActionOption) error { 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 return err
} }
@@ -699,10 +683,71 @@ func (wd *BrowserDriver) TapXY(x, y float64, opts ...option.ActionOption) error
"x": x, "x": x,
"y": y, "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 return err
} }
func (wd *BrowserDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error { func (wd *BrowserDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
return wd.TapFloat(x, y, opts...) 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
}
+6 -20
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) { func (s *DriverSession) GET(urlStr string, opts ...option.ActionOption) (rawResp DriverRawResponse, err error) {
rawResp, err = s.RequestWithRetry(http.MethodGet, urlStr, nil, opts...) return s.RequestWithRetry(http.MethodGet, urlStr, nil, opts...)
if err != nil {
return nil, errors.Wrap(code.DeviceHTTPDriverError, err.Error())
}
return rawResp, nil
} }
func (s *DriverSession) POST(data interface{}, urlStr string, opts ...option.ActionOption) (rawResp DriverRawResponse, err error) { func (s *DriverSession) POST(data interface{}, urlStr string, opts ...option.ActionOption) (rawResp DriverRawResponse, err error) {
var bsJSON []byte = nil var bsJSON []byte = nil
if data != nil { if data != nil {
if bsJSON, err = json.Marshal(data); err != 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...) return s.RequestWithRetry(http.MethodPost, urlStr, bsJSON, opts...)
if err != nil {
return nil, errors.Wrap(code.DeviceHTTPDriverError, err.Error())
}
return rawResp, nil
} }
func (s *DriverSession) DELETE(urlStr string, opts ...option.ActionOption) (rawResp DriverRawResponse, err error) { func (s *DriverSession) DELETE(urlStr string, opts ...option.ActionOption) (rawResp DriverRawResponse, err error) {
rawResp, err = s.RequestWithRetry(http.MethodDelete, urlStr, nil, opts...) return s.RequestWithRetry(http.MethodDelete, urlStr, nil, opts...)
if err != nil {
return nil, errors.Wrap(code.DeviceHTTPDriverError, err.Error())
}
return rawResp, nil
} }
func (s *DriverSession) RequestWithRetry(method string, urlStr string, rawBody []byte, opts ...option.ActionOption) ( 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 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) log.Warn().Err(err).Msgf("request failed, attempt %d/%d", attempt, s.maxRetry)
// If this was the last attempt, break // 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 err = rawResp.CheckErr(); err != nil {
if resp.StatusCode == http.StatusOK {
return rawResp, nil
}
return nil, err return nil, err
} }
+1 -2
View File
@@ -243,8 +243,7 @@ func (dev *IOSDevice) NewDriver() (driver IDriver, err error) {
if dev.Options.ResetHomeOnStartup { if dev.Options.ResetHomeOnStartup {
log.Info().Msg("go back to home screen") log.Info().Msg("go back to home screen")
if err = wdaDriver.Home(); err != nil { if err = wdaDriver.Home(); err != nil {
return nil, errors.Wrap(code.DeviceHTTPDriverError, return nil, errors.Wrap(err, "go back to home screen failed")
fmt.Sprintf("go back to home screen failed: %v", err))
} }
} }
if dev.Options.LogOn { if dev.Options.LogOn {
+9 -11
View File
@@ -196,7 +196,7 @@ func (wd *WDADriver) DeviceInfo() (deviceInfo types.DeviceInfo, err error) {
// [[FBRoute GET:@"/wda/device/info"].withoutSession // [[FBRoute GET:@"/wda/device/info"].withoutSession
var rawResp DriverRawResponse var rawResp DriverRawResponse
if rawResp, err = wd.Session.GET("/wda/device/info"); err != nil { 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 } }) reply := new(struct{ Value struct{ types.DeviceInfo } })
if err = json.Unmarshal(rawResp, reply); err != nil { if err = json.Unmarshal(rawResp, reply); err != nil {
@@ -275,14 +275,14 @@ func (wd *WDADriver) Scale() (float64, error) {
} }
type Screen struct { type Screen struct {
StatusBarSize types.Size `json:"statusBarSize"` types.Size
Scale float64 `json:"scale"` Scale float64 `json:"scale"`
} }
func (wd *WDADriver) Screen() (screen Screen, err error) { 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 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 return Screen{}, err
} }
reply := new(struct{ Value struct{ Screen } }) 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:)] // [[FBRoute GET:@"/screenshot"].withoutSession respondWithTarget:self action:@selector(handleGetScreenshot:)]
rawResp, err := wd.Session.GET("/screenshot") rawResp, err := wd.Session.GET("/screenshot")
if err != nil { if err != nil {
return nil, errors.Wrap(code.DeviceHTTPDriverError, return nil, errors.Wrap(code.DeviceScreenShotError,
fmt.Sprintf("WDA screenshot failed %v", err)) fmt.Sprintf("WDA screenshot failed %v", err))
} }
raw, err = rawResp.ValueDecodeAsBase64() raw, err = rawResp.ValueDecodeAsBase64()
if err != nil { if err != nil {
return nil, errors.Wrap(code.DeviceHTTPDriverError, return nil, errors.Wrap(code.DeviceScreenShotError,
fmt.Sprintf("decode WDA screenshot data failed: %v", err)) fmt.Sprintf("decode WDA screenshot data failed: %v", err))
} }
return raw, nil return raw, nil
@@ -454,8 +454,7 @@ func (wd *WDADriver) AppLaunch(bundleId string) (err error) {
} }
_, err = wd.Session.POST(data, "/wings/apps/launch") _, err = wd.Session.POST(data, "/wings/apps/launch")
if err != nil { if err != nil {
return errors.Wrap(code.DeviceHTTPDriverError, return errors.Wrap(err, "wda app launch failed")
fmt.Sprintf("wda launch failed: %v", err))
} }
return nil return nil
} }
@@ -466,8 +465,7 @@ func (wd *WDADriver) AppLaunchUnattached(bundleId string) (err error) {
data := map[string]interface{}{"bundleId": bundleId} data := map[string]interface{}{"bundleId": bundleId}
_, err = wd.Session.POST(data, "/wda/apps/launchUnattached") _, err = wd.Session.POST(data, "/wda/apps/launchUnattached")
if err != nil { if err != nil {
return errors.Wrap(code.DeviceHTTPDriverError, return errors.Wrap(err, "wda app launchUnattached failed")
fmt.Sprintf("wda launchUnattached failed: %v", err))
} }
return nil return nil
} }
+22 -12
View File
@@ -151,7 +151,8 @@ func (dExt *XTDriver) ExecuteAction(ctx context.Context, action option.MobileAct
// Execute via MCP tool // Execute via MCP tool
result, err := dExt.client.CallTool(ctx, req) result, err := dExt.client.CallTool(ctx, req)
if err != nil { 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 // 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 // For regular actions, collect session data and return it directly
sessionData := dExt.GetSession().GetData(true) // reset after getting data sessionData := dExt.GetSession().GetData(true) // reset after getting data
log.Debug().Str("tool", string(tool.Name())). // Log execution result, but avoid printing base64 data for screenshot tools
Interface("result", result.Content). logger := log.Debug().Str("tool", string(tool.Name()))
Msg("executed action via MCP tool") if tool.Name() != option.ACTION_ScreenShot {
logger.Interface("result", result.Content)
}
logger.Msg("executed action via MCP tool")
return sessionData, nil return sessionData, nil
} }
@@ -254,22 +259,27 @@ func (dExt *XTDriver) CallMCPTool(ctx context.Context,
log.Debug().Err(err). log.Debug().Err(err).
Str("server", serverName). Str("server", serverName).
Str("tool", toolName). Str("tool", toolName).
Msg("MCP hook call failed") Msg("call MCP tool failed")
return nil, err return nil, errors.Wrap(err, "call MCP tool failed")
} }
if result.IsError { if result.IsError {
log.Debug(). logger := log.Debug().
Str("server", serverName). Str("server", serverName).
Str("tool", toolName). Str("tool", toolName)
Interface("content", result.Content).
Msg("MCP hook returned error") // Avoid printing base64 data for screenshot tools
return nil, fmt.Errorf("MCP hook returned error") 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(). log.Debug().
Str("server", serverName). Str("server", serverName).
Str("tool", toolName). Str("tool", toolName).
Msg("MCP hook called successfully") Msg("call MCP tool successfully")
return result, nil return result, nil
} }