mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-09 01:17:30 +08:00
Merge branch 'session_refactor' into 'master'
feat: 优化driver层错误码返回 See merge request iesqa/httprunner!127
This commit is contained in:
@@ -24,6 +24,7 @@ require (
|
|||||||
github.com/gin-gonic/gin v1.10.0
|
github.com/gin-gonic/gin v1.10.0
|
||||||
github.com/go-openapi/spec v0.20.7
|
github.com/go-openapi/spec v0.20.7
|
||||||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
|
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
|
||||||
|
github.com/google/uuid v1.6.0
|
||||||
github.com/gorilla/websocket v1.5.3
|
github.com/gorilla/websocket v1.5.3
|
||||||
github.com/httprunner/funplugin v0.5.5
|
github.com/httprunner/funplugin v0.5.5
|
||||||
github.com/jinzhu/copier v0.3.5
|
github.com/jinzhu/copier v0.3.5
|
||||||
@@ -82,7 +83,6 @@ require (
|
|||||||
github.com/golang/protobuf v1.5.4 // indirect
|
github.com/golang/protobuf v1.5.4 // indirect
|
||||||
github.com/google/btree v1.1.2 // indirect
|
github.com/google/btree v1.1.2 // indirect
|
||||||
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
|
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
|
||||||
github.com/goph/emperror v0.17.2 // indirect
|
github.com/goph/emperror v0.17.2 // indirect
|
||||||
github.com/gorilla/css v1.0.1 // indirect
|
github.com/gorilla/css v1.0.1 // indirect
|
||||||
github.com/grandcat/zeroconf v1.0.0 // indirect
|
github.com/grandcat/zeroconf v1.0.0 // indirect
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
v5.0.0-250711
|
v5.0.0-250716
|
||||||
|
|||||||
@@ -14,10 +14,12 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
|
"github.com/httprunner/httprunner/v5/code"
|
||||||
"github.com/httprunner/httprunner/v5/internal/json"
|
"github.com/httprunner/httprunner/v5/internal/json"
|
||||||
"github.com/httprunner/httprunner/v5/uixt/option"
|
"github.com/httprunner/httprunner/v5/uixt/option"
|
||||||
)
|
)
|
||||||
@@ -160,7 +162,7 @@ func (s *DriverSession) POST(data interface{}, urlStr string, opts ...option.Act
|
|||||||
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, err
|
return nil, errors.Wrap(code.InvalidParamError, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return s.RequestWithRetry(http.MethodPost, urlStr, bsJSON, opts...)
|
return s.RequestWithRetry(http.MethodPost, urlStr, bsJSON, opts...)
|
||||||
@@ -191,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
|
||||||
@@ -220,6 +223,7 @@ func (s *DriverSession) RequestWithRetry(method string, urlStr string, rawBody [
|
|||||||
func (s *DriverSession) Request(method string, urlStr string, rawBody []byte, opts ...option.ActionOption) (
|
func (s *DriverSession) Request(method string, urlStr string, rawBody []byte, opts ...option.ActionOption) (
|
||||||
rawResp DriverRawResponse, err error,
|
rawResp DriverRawResponse, err error,
|
||||||
) {
|
) {
|
||||||
|
logid := uuid.New().String()
|
||||||
timeout := s.timeout
|
timeout := s.timeout
|
||||||
options := option.NewActionOptions(opts...)
|
options := option.NewActionOptions(opts...)
|
||||||
if options.Timeout > 0 {
|
if options.Timeout > 0 {
|
||||||
@@ -251,7 +255,7 @@ func (s *DriverSession) Request(method string, urlStr string, rawBody []byte, op
|
|||||||
logger = log.Debug().Bool("success", true)
|
logger = log.Debug().Bool("success", true)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger = logger.Str("request_method", method).Str("request_url", rawURL).
|
logger = logger.Str("logid", logid).Str("request_method", method).Str("request_url", rawURL).
|
||||||
Str("request_body", string(rawBody))
|
Str("request_body", string(rawBody))
|
||||||
if !driverResult.RequestTime.IsZero() {
|
if !driverResult.RequestTime.IsZero() {
|
||||||
logger = logger.Int64("request_time", driverResult.RequestTime.UnixMilli())
|
logger = logger.Int64("request_time", driverResult.RequestTime.UnixMilli())
|
||||||
@@ -274,6 +278,8 @@ func (s *DriverSession) Request(method string, urlStr string, rawBody []byte, op
|
|||||||
}
|
}
|
||||||
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
|
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
|
||||||
req.Header.Set("Accept", "application/json")
|
req.Header.Set("Accept", "application/json")
|
||||||
|
req.Header.Set("X-HTTP-Request-ID", logid)
|
||||||
|
req.Header.Set("logid", logid)
|
||||||
|
|
||||||
driverResult.RequestTime = time.Now()
|
driverResult.RequestTime = time.Now()
|
||||||
var resp *http.Response
|
var resp *http.Response
|
||||||
|
|||||||
+1
-2
@@ -231,8 +231,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.MobileUIDriverError,
|
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 {
|
||||||
|
|||||||
@@ -268,7 +268,7 @@ func (wd *WDADriver) Scale() (float64, error) {
|
|||||||
}
|
}
|
||||||
screen, err := wd.Screen()
|
screen, err := wd.Screen()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.Wrap(code.MobileUIDriverError,
|
return 0, errors.Wrap(code.DeviceHTTPDriverError,
|
||||||
fmt.Sprintf("get screen info failed: %v", err))
|
fmt.Sprintf("get screen info failed: %v", err))
|
||||||
}
|
}
|
||||||
return screen.Scale, nil
|
return screen.Scale, 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.MobileUILaunchAppError,
|
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.MobileUILaunchAppError,
|
return errors.Wrap(err, "wda app launchUnattached failed")
|
||||||
fmt.Sprintf("wda launchUnattached failed: %v", err))
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -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
|
||||||
@@ -259,7 +260,7 @@ func (dExt *XTDriver) CallMCPTool(ctx context.Context,
|
|||||||
Str("server", serverName).
|
Str("server", serverName).
|
||||||
Str("tool", toolName).
|
Str("tool", toolName).
|
||||||
Msg("call MCP tool failed")
|
Msg("call MCP tool failed")
|
||||||
return nil, err
|
return nil, errors.Wrap(err, "call MCP tool failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
if result.IsError {
|
if result.IsError {
|
||||||
|
|||||||
Reference in New Issue
Block a user