mirror of
https://github.com/httprunner/httprunner.git
synced 2026-08-25 01:59:52 +08:00
feat: 优化driver层错误码返回
This commit is contained in:
@@ -18,6 +18,7 @@ import (
|
|||||||
"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"
|
||||||
)
|
)
|
||||||
@@ -153,21 +154,33 @@ 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) {
|
||||||
return s.RequestWithRetry(http.MethodGet, urlStr, nil, opts...)
|
rawResp, err = 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, err
|
return nil, errors.Wrap(code.DeviceHTTPDriverError, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return s.RequestWithRetry(http.MethodPost, urlStr, bsJSON, opts...)
|
rawResp, err = 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) {
|
||||||
return s.RequestWithRetry(http.MethodDelete, urlStr, nil, opts...)
|
rawResp, err = 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) (
|
||||||
|
|||||||
@@ -231,7 +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(code.DeviceHTTPDriverError,
|
||||||
fmt.Sprintf("go back to home screen failed: %v", err))
|
fmt.Sprintf("go back to home screen failed: %v", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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{}, err
|
return types.DeviceInfo{}, errors.Wrap(code.DeviceHTTPDriverError, err.Error())
|
||||||
}
|
}
|
||||||
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 {
|
||||||
@@ -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
|
||||||
@@ -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.DeviceScreenShotError,
|
return nil, errors.Wrap(code.DeviceHTTPDriverError,
|
||||||
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.DeviceScreenShotError,
|
return nil, errors.Wrap(code.DeviceHTTPDriverError,
|
||||||
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,7 +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(code.DeviceHTTPDriverError,
|
||||||
fmt.Sprintf("wda launch failed: %v", err))
|
fmt.Sprintf("wda launch failed: %v", err))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -466,7 +466,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(code.DeviceHTTPDriverError,
|
||||||
fmt.Sprintf("wda launchUnattached failed: %v", err))
|
fmt.Sprintf("wda launchUnattached failed: %v", err))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user