refactor: BrowserDevice & BrowserDriver

This commit is contained in:
lilong.129
2025-03-03 22:03:42 +08:00
parent b687abb3c8
commit b69f5d5b25
7 changed files with 103 additions and 121 deletions
+1 -1
View File
@@ -1 +1 @@
v5.0.0+2503032140 v5.0.0+2503032203
+1 -1
View File
@@ -67,7 +67,7 @@ func (dev *BrowserDevice) GetPackageInfo(packageName string) (types.AppInfo, err
func (dev *BrowserDevice) NewDriver() (driver IDriver, err error) { func (dev *BrowserDevice) NewDriver() (driver IDriver, err error) {
// var driver WebDriver // var driver WebDriver
driver, err = NewBrowserWebDriver(dev.UUID()) driver, err = NewBrowserDriver(dev)
if err != nil { if err != nil {
return nil, err return nil, err
} }
+77 -81
View File
@@ -34,10 +34,9 @@ type CreateBrowserResponse struct {
Data BrowserInfo `json:"data"` Data BrowserInfo `json:"data"`
} }
type BrowserWebDriver struct { type BrowserDriver struct {
urlPrefix *url.URL urlPrefix *url.URL
sessionId string sessionId string
scale float64
} }
type BrowserInfo struct { type BrowserInfo struct {
@@ -50,11 +49,9 @@ func CreateBrowser(timeout int) (browserInfo *BrowserInfo, err error) {
} }
var bsJSON []byte = nil var bsJSON []byte = 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, err
} }
}
rawURL := "http://" + BROWSER_LOCAL_ADDRESS + "/api/v1/create_browser" rawURL := "http://" + BROWSER_LOCAL_ADDRESS + "/api/v1/create_browser"
req, err := http.NewRequest(http.MethodPost, rawURL, bytes.NewBuffer(bsJSON)) req, err := http.NewRequest(http.MethodPost, rawURL, bytes.NewBuffer(bsJSON))
@@ -89,18 +86,17 @@ func CreateBrowser(timeout int) (browserInfo *BrowserInfo, err error) {
return &result.Data, nil return &result.Data, nil
} }
func NewBrowserWebDriver(browserId string) (driver *BrowserWebDriver, err error) { func NewBrowserDriver(device *BrowserDevice) (driver *BrowserDriver, err error) {
log.Info().Msg("init NewBrowserWebDriver driver") log.Info().Msg("init NewBrowserDriver driver")
driver = new(BrowserWebDriver) driver = new(BrowserDriver)
driver.urlPrefix = &url.URL{} driver.urlPrefix = &url.URL{}
driver.urlPrefix.Host = BROWSER_LOCAL_ADDRESS driver.urlPrefix.Host = BROWSER_LOCAL_ADDRESS
driver.urlPrefix.Scheme = "http" driver.urlPrefix.Scheme = "http"
driver.scale = 1.0 driver.sessionId = device.UUID()
driver.sessionId = browserId
return driver, nil return driver, nil
} }
func (wd *BrowserWebDriver) Drag(fromX, fromY, toX, toY float64, options ...option.ActionOption) (err error) { func (wd *BrowserDriver) Drag(fromX, fromY, toX, toY float64, options ...option.ActionOption) (err error) {
data := map[string]interface{}{ data := map[string]interface{}{
"from_x": fromX, "from_x": fromX,
"from_y": fromY, "from_y": fromY,
@@ -118,7 +114,7 @@ func (wd *BrowserWebDriver) Drag(fromX, fromY, toX, toY float64, options ...opti
return return
} }
func (wd *BrowserWebDriver) AppLaunch(packageName string) (err error) { func (wd *BrowserDriver) AppLaunch(packageName string) (err error) {
data := map[string]interface{}{ data := map[string]interface{}{
"url": packageName, "url": packageName,
} }
@@ -127,7 +123,7 @@ func (wd *BrowserWebDriver) AppLaunch(packageName string) (err error) {
return return
} }
func (wd *BrowserWebDriver) DeleteSession() (err error) { func (wd *BrowserDriver) DeleteSession() (err error) {
url := wd.concatURL("context", wd.sessionId) url := wd.concatURL("context", wd.sessionId)
req, err := http.NewRequest("DELETE", url, nil) req, err := http.NewRequest("DELETE", url, nil)
@@ -158,7 +154,7 @@ func (wd *BrowserWebDriver) DeleteSession() (err error) {
return nil return nil
} }
func (wd *BrowserWebDriver) Scroll(delta int) (err error) { func (wd *BrowserDriver) Scroll(delta int) (err error) {
data := map[string]interface{}{ data := map[string]interface{}{
"delta": delta, "delta": delta,
} }
@@ -166,7 +162,7 @@ func (wd *BrowserWebDriver) Scroll(delta int) (err error) {
return err return err
} }
func (wd *BrowserWebDriver) CreateNetListener() (*websocket.Conn, error) { func (wd *BrowserDriver) CreateNetListener() (*websocket.Conn, error) {
webSocketUrl := "ws://localhost:8093/websocket_net_listen" webSocketUrl := "ws://localhost:8093/websocket_net_listen"
c, _, err := websocket.DefaultDialer.Dial(webSocketUrl, nil) c, _, err := websocket.DefaultDialer.Dial(webSocketUrl, nil)
if err != nil { if err != nil {
@@ -181,7 +177,7 @@ func (wd *BrowserWebDriver) CreateNetListener() (*websocket.Conn, error) {
return c, err return c, err
} }
func (wd *BrowserWebDriver) ClosePage(pageIndex int) (err error) { func (wd *BrowserDriver) ClosePage(pageIndex int) (err error) {
data := map[string]interface{}{ data := map[string]interface{}{
"page_index": pageIndex, "page_index": pageIndex,
} }
@@ -189,7 +185,7 @@ func (wd *BrowserWebDriver) ClosePage(pageIndex int) (err error) {
return err return err
} }
func (wd *BrowserWebDriver) HoverBySelector(selector string, options ...option.ActionOption) (err error) { func (wd *BrowserDriver) HoverBySelector(selector string, options ...option.ActionOption) (err error) {
data := map[string]interface{}{ data := map[string]interface{}{
"selector": selector, "selector": selector,
} }
@@ -201,7 +197,7 @@ func (wd *BrowserWebDriver) HoverBySelector(selector string, options ...option.A
return err return err
} }
func (wd *BrowserWebDriver) tapBySelector(selector string, options ...option.ActionOption) (err error) { func (wd *BrowserDriver) tapBySelector(selector string, options ...option.ActionOption) (err error) {
data := map[string]interface{}{ data := map[string]interface{}{
"selector": selector, "selector": selector,
} }
@@ -213,7 +209,7 @@ func (wd *BrowserWebDriver) tapBySelector(selector string, options ...option.Act
return err return err
} }
func (wd *BrowserWebDriver) RightClick(x, y float64) (err error) { func (wd *BrowserDriver) RightClick(x, y float64) (err error) {
data := map[string]interface{}{ data := map[string]interface{}{
"x": x, "x": x,
"y": y, "y": y,
@@ -222,7 +218,7 @@ func (wd *BrowserWebDriver) RightClick(x, y float64) (err error) {
return err return err
} }
func (wd *BrowserWebDriver) RightclickbySelector(selector string, options ...option.ActionOption) (err error) { func (wd *BrowserDriver) RightclickbySelector(selector string, options ...option.ActionOption) (err error) {
data := map[string]interface{}{ data := map[string]interface{}{
"selector": selector, "selector": selector,
} }
@@ -234,7 +230,7 @@ func (wd *BrowserWebDriver) RightclickbySelector(selector string, options ...opt
return err return err
} }
func (wd *BrowserWebDriver) GetElementTextBySelector(selector string, options ...option.ActionOption) (text string, err error) { func (wd *BrowserDriver) GetElementTextBySelector(selector string, options ...option.ActionOption) (text string, err error) {
actionOptions := option.NewActionOptions(options...) actionOptions := option.NewActionOptions(options...)
uri := "ui/element_text?selector=" + selector uri := "ui/element_text?selector=" + selector
if actionOptions.Index > 0 { if actionOptions.Index > 0 {
@@ -248,7 +244,7 @@ func (wd *BrowserWebDriver) GetElementTextBySelector(selector string, options ..
return data["text"].(string), nil return data["text"].(string), nil
} }
func (wd *BrowserWebDriver) GetPageUrl(options ...option.ActionOption) (text string, err error) { func (wd *BrowserDriver) GetPageUrl(options ...option.ActionOption) (text string, err error) {
uri := "ui/page_url" uri := "ui/page_url"
actionOptions := option.NewActionOptions(options...) actionOptions := option.NewActionOptions(options...)
if actionOptions.Index > 0 { if actionOptions.Index > 0 {
@@ -262,7 +258,7 @@ func (wd *BrowserWebDriver) GetPageUrl(options ...option.ActionOption) (text str
return data["url"].(string), nil return data["url"].(string), nil
} }
func (wd *BrowserWebDriver) IsElementExistBySelector(selector string) (bool, error) { func (wd *BrowserDriver) IsElementExistBySelector(selector string) (bool, error) {
resp, err := wd.HttpGet(wd.sessionId, "ui/element_exist", "?selector=", selector) resp, err := wd.HttpGet(wd.sessionId, "ui/element_exist", "?selector=", selector)
if err != nil { if err != nil {
return false, err return false, err
@@ -271,7 +267,7 @@ func (wd *BrowserWebDriver) IsElementExistBySelector(selector string) (bool, err
return data["exist"].(bool), nil return data["exist"].(bool), nil
} }
func (wd *BrowserWebDriver) Hover(x, y float64) (err error) { func (wd *BrowserDriver) Hover(x, y float64) (err error) {
data := map[string]interface{}{ data := map[string]interface{}{
"x": x, "x": x,
"y": y, "y": y,
@@ -280,7 +276,7 @@ func (wd *BrowserWebDriver) Hover(x, y float64) (err error) {
return err return err
} }
func (wd *BrowserWebDriver) Input(text string, option ...option.ActionOption) (err error) { func (wd *BrowserDriver) Input(text string, option ...option.ActionOption) (err error) {
data := map[string]interface{}{ data := map[string]interface{}{
"text": text, "text": text,
} }
@@ -289,7 +285,7 @@ func (wd *BrowserWebDriver) Input(text string, option ...option.ActionOption) (e
} }
// Source Return application elements tree // Source Return application elements tree
func (wd *BrowserWebDriver) Source(srcOpt ...option.SourceOption) (string, error) { func (wd *BrowserDriver) Source(srcOpt ...option.SourceOption) (string, error) {
resp, err := wd.HttpGet(http.MethodGet, wd.sessionId, "stub/source") resp, err := wd.HttpGet(http.MethodGet, wd.sessionId, "stub/source")
if err != nil { if err != nil {
return "", err return "", err
@@ -303,7 +299,7 @@ func (wd *BrowserWebDriver) Source(srcOpt ...option.SourceOption) (string, error
return string(jsonData), err return string(jsonData), err
} }
func (wd *BrowserWebDriver) ScreenShot(options ...option.ActionOption) (*bytes.Buffer, error) { func (wd *BrowserDriver) ScreenShot(options ...option.ActionOption) (*bytes.Buffer, error) {
resp, err := wd.HttpGet(http.MethodGet, wd.sessionId, "screenshot") resp, err := wd.HttpGet(http.MethodGet, wd.sessionId, "screenshot")
if err != nil { if err != nil {
return nil, err return nil, err
@@ -316,7 +312,7 @@ func (wd *BrowserWebDriver) ScreenShot(options ...option.ActionOption) (*bytes.B
return res, err return res, err
} }
func (wd *BrowserWebDriver) HttpPOST(data interface{}, pathElem ...string) (response *WebAgentResponse, err error) { func (wd *BrowserDriver) HttpPOST(data interface{}, pathElem ...string) (response *WebAgentResponse, 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 {
@@ -327,18 +323,18 @@ func (wd *BrowserWebDriver) HttpPOST(data interface{}, pathElem ...string) (resp
return wd.httpRequest(http.MethodPost, wd.concatURL(pathElem...), bsJSON) return wd.httpRequest(http.MethodPost, wd.concatURL(pathElem...), bsJSON)
} }
func (wd *BrowserWebDriver) HttpGet(data interface{}, pathElem ...string) (response *WebAgentResponse, err error) { func (wd *BrowserDriver) HttpGet(data interface{}, pathElem ...string) (response *WebAgentResponse, err error) {
return wd.httpRequest(http.MethodGet, wd.concatURL(pathElem...), nil) return wd.httpRequest(http.MethodGet, wd.concatURL(pathElem...), nil)
} }
func (wd *BrowserWebDriver) concatURL(elem ...string) string { func (wd *BrowserDriver) concatURL(elem ...string) string {
tmp, _ := url.Parse(wd.urlPrefix.String()) tmp, _ := url.Parse(wd.urlPrefix.String())
commonPath := path.Join(append([]string{wd.urlPrefix.Path}, "api/v1/")...) commonPath := path.Join(append([]string{wd.urlPrefix.Path}, "api/v1/")...)
tmp.Path = path.Join(append([]string{commonPath}, elem...)...) tmp.Path = path.Join(append([]string{commonPath}, elem...)...)
return tmp.String() return tmp.String()
} }
func (wd *BrowserWebDriver) httpRequest(method string, rawURL string, rawBody []byte, disableRetry ...bool) (response *WebAgentResponse, err error) { func (wd *BrowserDriver) httpRequest(method string, rawURL string, rawBody []byte, disableRetry ...bool) (response *WebAgentResponse, err error) {
req, err := http.NewRequest(method, rawURL, bytes.NewBuffer(rawBody)) req, err := http.NewRequest(method, rawURL, bytes.NewBuffer(rawBody))
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
@@ -377,22 +373,22 @@ func (wd *BrowserWebDriver) httpRequest(method string, rawURL string, rawBody []
return &result, err return &result, err
} }
func (wd *BrowserWebDriver) Status() (deviceStatus types.DeviceStatus, err error) { func (wd *BrowserDriver) Status() (deviceStatus types.DeviceStatus, err error) {
log.Warn().Msg("Status not implemented in ADBDriver") log.Warn().Msg("Status not implemented in ADBDriver")
return return
} }
func (wd *BrowserWebDriver) DeviceInfo() (deviceInfo types.DeviceInfo, err error) { func (wd *BrowserDriver) DeviceInfo() (deviceInfo types.DeviceInfo, err error) {
log.Warn().Msg("DeviceInfo not implemented in ADBDriver") log.Warn().Msg("DeviceInfo not implemented in ADBDriver")
return return
} }
func (wd *BrowserWebDriver) BatteryInfo() (batteryInfo types.BatteryInfo, err error) { func (wd *BrowserDriver) BatteryInfo() (batteryInfo types.BatteryInfo, err error) {
log.Warn().Msg("BatteryInfo not implemented in ADBDriver") log.Warn().Msg("BatteryInfo not implemented in ADBDriver")
return return
} }
func (wd *BrowserWebDriver) WindowSize() (types.Size, error) { func (wd *BrowserDriver) WindowSize() (types.Size, error) {
resp, err := wd.HttpGet(http.MethodGet, wd.sessionId, "window_size") resp, err := wd.HttpGet(http.MethodGet, wd.sessionId, "window_size")
if err != nil { if err != nil {
return types.Size{}, err return types.Size{}, err
@@ -406,66 +402,66 @@ func (wd *BrowserWebDriver) WindowSize() (types.Size, error) {
}, nil }, nil
} }
func (wd *BrowserWebDriver) Screen() (Screen, error) { func (wd *BrowserDriver) Screen() (Screen, error) {
return Screen{}, errors.New("not support") return Screen{}, errors.New("not support")
} }
func (wd *BrowserWebDriver) Scale() (float64, error) { func (wd *BrowserDriver) Scale() (float64, error) {
return 0, errors.New("not support") return 0, errors.New("not support")
} }
// GetTimestamp returns the timestamp of the mobile device // GetTimestamp returns the timestamp of the mobile device
func (wd *BrowserWebDriver) GetTimestamp() (timestamp int64, err error) { func (wd *BrowserDriver) GetTimestamp() (timestamp int64, err error) {
return 0, errors.New("not support") return 0, errors.New("not support")
} }
// Homescreen Forces the device under test to switch to the home screen // Homescreen Forces the device under test to switch to the home screen
func (wd *BrowserWebDriver) Homescreen() error { func (wd *BrowserDriver) Homescreen() error {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) Unlock() (err error) { func (wd *BrowserDriver) Unlock() (err error) {
return errors.New("not support") return errors.New("not support")
} }
// AppTerminate Terminate an application with the given package name. // AppTerminate Terminate an application with the given package name.
// Either `true` if the app has been successfully terminated or `false` if it was not running // Either `true` if the app has been successfully terminated or `false` if it was not running
func (wd *BrowserWebDriver) AppTerminate(packageName string) (bool, error) { func (wd *BrowserDriver) AppTerminate(packageName string) (bool, error) {
return false, errors.New("not support") return false, errors.New("not support")
} }
// AssertForegroundApp returns nil if the given package and activity are in foreground // AssertForegroundApp returns nil if the given package and activity are in foreground
func (wd *BrowserWebDriver) AssertForegroundApp(packageName string, activityType ...string) error { func (wd *BrowserDriver) AssertForegroundApp(packageName string, activityType ...string) error {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) Back() error { func (wd *BrowserDriver) Back() error {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) AppClear(packageName string) error { func (wd *BrowserDriver) AppClear(packageName string) error {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) ClearImages() error { func (wd *BrowserDriver) ClearImages() error {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) PushImage(localPath string) error { func (wd *BrowserDriver) PushImage(localPath string) error {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) Orientation() (orientation types.Orientation, err error) { func (wd *BrowserDriver) Orientation() (orientation types.Orientation, err error) {
log.Warn().Msg("Orientation not implemented in ADBDriver") log.Warn().Msg("Orientation not implemented in ADBDriver")
return return
} }
// Tap Sends a tap event at the coordinate. // Tap Sends a tap event at the coordinate.
func (wd *BrowserWebDriver) Tap(x, y int, options ...option.ActionOption) error { func (wd *BrowserDriver) Tap(x, y int, options ...option.ActionOption) error {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) TapFloat(x, y float64, options ...option.ActionOption) error { func (wd *BrowserDriver) TapFloat(x, y float64, options ...option.ActionOption) error {
actionOptions := option.NewActionOptions(options...) actionOptions := option.NewActionOptions(options...)
duration := 0.1 duration := 0.1
if actionOptions.Duration > 0 { if actionOptions.Duration > 0 {
@@ -481,7 +477,7 @@ func (wd *BrowserWebDriver) TapFloat(x, y float64, options ...option.ActionOptio
} }
// DoubleTap Sends a double tap event at the coordinate. // DoubleTap Sends a double tap event at the coordinate.
func (wd *BrowserWebDriver) DoubleTap(x, y float64, options ...option.ActionOption) error { func (wd *BrowserDriver) DoubleTap(x, y float64, options ...option.ActionOption) error {
data := map[string]interface{}{ data := map[string]interface{}{
"x": x, "x": x,
"y": y, "y": y,
@@ -490,7 +486,7 @@ func (wd *BrowserWebDriver) DoubleTap(x, y float64, options ...option.ActionOpti
return err return err
} }
func (wd *BrowserWebDriver) UploadFile(x, y float64, FileUrl, FileFormat string) (err error) { func (wd *BrowserDriver) UploadFile(x, y float64, FileUrl, FileFormat string) (err error) {
data := map[string]interface{}{ data := map[string]interface{}{
"x": x, "x": x,
"y": y, "y": y,
@@ -504,70 +500,70 @@ func (wd *BrowserWebDriver) UploadFile(x, y float64, FileUrl, FileFormat string)
// TouchAndHold Initiates a long-press gesture at the coordinate, holding for the specified duration. // TouchAndHold Initiates a long-press gesture at the coordinate, holding for the specified duration.
// //
// second: The default value is 1 // second: The default value is 1
func (wd *BrowserWebDriver) TouchAndHold(x, y float64, options ...option.ActionOption) error { func (wd *BrowserDriver) TouchAndHold(x, y float64, options ...option.ActionOption) error {
return errors.New("not support") return errors.New("not support")
} }
// Swipe works like Drag, but `pressForDuration` value is 0 // Swipe works like Drag, but `pressForDuration` value is 0
func (wd *BrowserWebDriver) Swipe(fromX, fromY, toX, toY float64, options ...option.ActionOption) error { func (wd *BrowserDriver) Swipe(fromX, fromY, toX, toY float64, options ...option.ActionOption) error {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) SwipeFloat(fromX, fromY, toX, toY float64, options ...option.ActionOption) error { func (wd *BrowserDriver) SwipeFloat(fromX, fromY, toX, toY float64, options ...option.ActionOption) error {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) SetIme(ime string) error { func (wd *BrowserDriver) SetIme(ime string) error {
return errors.New("not support") return errors.New("not support")
} }
// SendKeys Types a string into active element. There must be element with keyboard focus, // SendKeys Types a string into active element. There must be element with keyboard focus,
// otherwise an error is raised. // otherwise an error is raised.
// WithFrequency option can be used to set frequency of typing (letters per sec). The default value is 60 // WithFrequency option can be used to set frequency of typing (letters per sec). The default value is 60
func (wd *BrowserWebDriver) SendKeys(text string, options ...option.ActionOption) error { func (wd *BrowserDriver) SendKeys(text string, options ...option.ActionOption) error {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) Clear(packageName string) error { func (wd *BrowserDriver) Clear(packageName string) error {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) Setup() error { func (wd *BrowserDriver) Setup() error {
return nil return nil
} }
func (wd *BrowserWebDriver) GetDevice() IDevice { func (wd *BrowserDriver) GetDevice() IDevice {
return nil return nil
} }
func (wd *BrowserWebDriver) ForegroundInfo() (app types.AppInfo, err error) { func (wd *BrowserDriver) ForegroundInfo() (app types.AppInfo, err error) {
return return
} }
// PressBack Presses the back button // PressBack Presses the back button
func (wd *BrowserWebDriver) PressBack(options ...option.ActionOption) error { func (wd *BrowserDriver) PressBack(options ...option.ActionOption) error {
_, err := wd.HttpPOST(map[string]interface{}{}, wd.sessionId, "ui/back") _, err := wd.HttpPOST(map[string]interface{}{}, wd.sessionId, "ui/back")
return err return err
} }
func (wd *BrowserWebDriver) PressKeyCode(keyCode KeyCode) (err error) { func (wd *BrowserDriver) PressKeyCode(keyCode KeyCode) (err error) {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) Backspace(count int, options ...option.ActionOption) (err error) { func (wd *BrowserDriver) Backspace(count int, options ...option.ActionOption) (err error) {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) LogoutNoneUI(packageName string) error { func (wd *BrowserDriver) LogoutNoneUI(packageName string) error {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) TapByText(text string, options ...option.ActionOption) error { func (wd *BrowserDriver) TapByText(text string, options ...option.ActionOption) error {
return errors.New("not support") return errors.New("not support")
} }
// AccessibleSource Return application elements accessibility tree // AccessibleSource Return application elements accessibility tree
func (wd *BrowserWebDriver) AccessibleSource() (string, error) { func (wd *BrowserDriver) AccessibleSource() (string, error) {
return "", errors.New("not support") return "", errors.New("not support")
} }
@@ -576,67 +572,67 @@ func (wd *BrowserWebDriver) AccessibleSource() (string, error) {
// Checks health of XCTest by: // Checks health of XCTest by:
// 1) Querying application for some elements, // 1) Querying application for some elements,
// 2) Triggering some device events. // 2) Triggering some device events.
func (wd *BrowserWebDriver) HealthCheck() error { func (wd *BrowserDriver) HealthCheck() error {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) GetAppiumSettings() (map[string]interface{}, error) { func (wd *BrowserDriver) GetAppiumSettings() (map[string]interface{}, error) {
return nil, errors.New("not support") return nil, errors.New("not support")
} }
func (wd *BrowserWebDriver) SetAppiumSettings(settings map[string]interface{}) (map[string]interface{}, error) { func (wd *BrowserDriver) SetAppiumSettings(settings map[string]interface{}) (map[string]interface{}, error) {
return nil, errors.New("not support") return nil, errors.New("not support")
} }
func (wd *BrowserWebDriver) IsHealthy() (bool, error) { func (wd *BrowserDriver) IsHealthy() (bool, error) {
return false, errors.New("not support") return false, errors.New("not support")
} }
// triggers the log capture and returns the log entries // triggers the log capture and returns the log entries
func (wd *BrowserWebDriver) StartCaptureLog(identifier ...string) (err error) { func (wd *BrowserDriver) StartCaptureLog(identifier ...string) (err error) {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) StopCaptureLog() (result interface{}, err error) { func (wd *BrowserDriver) StopCaptureLog() (result interface{}, err error) {
return nil, errors.New("not support") return nil, errors.New("not support")
} }
func (wd *BrowserWebDriver) RecordScreen(folderPath string, duration time.Duration) (videoPath string, err error) { func (wd *BrowserDriver) RecordScreen(folderPath string, duration time.Duration) (videoPath string, err error) {
return "", errors.New("not support") return "", errors.New("not support")
} }
func (wd *BrowserWebDriver) TearDown() error { func (wd *BrowserDriver) TearDown() error {
return nil return nil
} }
func (wd *BrowserWebDriver) InitSession(capabilities option.Capabilities) error { func (wd *BrowserDriver) InitSession(capabilities option.Capabilities) error {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) GetSession() *DriverSession { func (wd *BrowserDriver) GetSession() *DriverSession {
return nil return nil
} }
func (wd *BrowserWebDriver) ScreenRecord(duration time.Duration) (videoPath string, err error) { func (wd *BrowserDriver) ScreenRecord(duration time.Duration) (videoPath string, err error) {
return return
} }
func (wd *BrowserWebDriver) Rotation() (rotation types.Rotation, err error) { func (wd *BrowserDriver) Rotation() (rotation types.Rotation, err error) {
return return
} }
func (wd *BrowserWebDriver) SetRotation(rotation types.Rotation) error { func (wd *BrowserDriver) SetRotation(rotation types.Rotation) error {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) Home() error { func (wd *BrowserDriver) Home() error {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) TapXY(x, y float64, opts ...option.ActionOption) error { func (wd *BrowserDriver) TapXY(x, y float64, opts ...option.ActionOption) error {
return errors.New("not support") return errors.New("not support")
} }
func (wd *BrowserWebDriver) 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...)
} }
+1 -1
View File
@@ -102,7 +102,7 @@ func (dExt *XTDriver) GetIDriver() IDriver {
} }
func (dExt *XTDriver) GetWebDriver() IBrowserWebDriver { func (dExt *XTDriver) GetWebDriver() IBrowserWebDriver {
return dExt.GetIDriver().(*BrowserWebDriver) return dExt.GetIDriver().(*BrowserDriver)
} }
type IXTDriver interface { type IXTDriver interface {
+6 -14
View File
@@ -3,7 +3,6 @@ package driver_ext
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
@@ -30,7 +29,7 @@ type CreateBrowserResponse struct {
} }
type StubBrowserDriver struct { type StubBrowserDriver struct {
*uixt.BrowserWebDriver *uixt.BrowserDriver
urlPrefix *url.URL urlPrefix *url.URL
sessionId string sessionId string
scale float64 scale float64
@@ -64,7 +63,6 @@ func CreateBrowser(timeout int) (browserInfo *BrowserInfo, err error) {
Timeout: 30 * time.Second, // 设置超时时间为5秒 Timeout: 30 * time.Second, // 设置超时时间为5秒
} }
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -87,31 +85,26 @@ func CreateBrowser(timeout int) (browserInfo *BrowserInfo, err error) {
return &result.Data, nil return &result.Data, nil
} }
func NewStubBrowserDriver(browserId string) (driver *StubBrowserDriver, err error) { func NewStubBrowserDriver(device *uixt.BrowserDevice) (driver *StubBrowserDriver, err error) {
BrowserWebDriver, err := uixt.NewBrowserWebDriver(browserId) BrowserWebDriver, err := uixt.NewBrowserDriver(device)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "create browser session failed") return nil, errors.Wrap(err, "create browser session failed")
} }
driver = &StubBrowserDriver{ driver = &StubBrowserDriver{
BrowserWebDriver: BrowserWebDriver, BrowserDriver: BrowserWebDriver,
}
driver.sessionId = browserId
if err != nil {
return nil, fmt.Errorf("adb forward: %w", err)
} }
driver.sessionId = device.UUID()
return driver, nil return driver, nil
} }
// Source Return application elements tree // Source Return application elements tree
func (wd *StubBrowserDriver) Source(srcOpt ...option.SourceOption) (string, error) { func (wd *StubBrowserDriver) Source(srcOpt ...option.SourceOption) (string, error) {
resp, err := wd.BrowserWebDriver.HttpGet(http.MethodGet, wd.sessionId, "stub/source") resp, err := wd.BrowserDriver.HttpGet(http.MethodGet, wd.sessionId, "stub/source")
if err != nil { if err != nil {
return "", err return "", err
} }
jsonData, err := json.Marshal(resp.Data) jsonData, err := json.Marshal(resp.Data)
if err != nil { if err != nil {
return "", err return "", err
} }
@@ -125,7 +118,6 @@ func (wd *StubBrowserDriver) LoginNoneUI(packageName, phoneNumber string, captch
"web_cookie": password, "web_cookie": password,
} }
resp, err := wd.HttpPOST(data, wd.sessionId, "stub/login") resp, err := wd.HttpPOST(data, wd.sessionId, "stub/login")
if err != nil { if err != nil {
return info, err return info, err
} }
+12 -18
View File
@@ -1,35 +1,29 @@
package driver_ext package driver_ext
import ( import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/httprunner/httprunner/v5/pkg/uixt" "github.com/httprunner/httprunner/v5/pkg/uixt"
"github.com/httprunner/httprunner/v5/pkg/uixt/option" "github.com/httprunner/httprunner/v5/pkg/uixt/option"
"testing"
) )
var ( var iOSStubDriver *StubIOSDriver
iOSStubDriver *StubIOSDriver
)
func checkErr(t *testing.T, err error, msg ...string) {
if err != nil {
if len(msg) == 0 {
t.Fatal(err)
} else {
t.Fatal(msg, err)
}
}
}
func setupIOSStubDriver(t *testing.T) { func setupIOSStubDriver(t *testing.T) {
iOSDevice, err := uixt.NewIOSDevice(option.WithWDAPort(8700), option.WithWDAMjpegPort(8800), option.WithResetHomeOnStartup(false)) iOSDevice, err := uixt.NewIOSDevice(
checkErr(t, err) option.WithWDAPort(8700),
option.WithWDAMjpegPort(8800),
option.WithResetHomeOnStartup(false))
assert.Nil(t, err)
iOSStubDriver, err = NewStubIOSDriver(iOSDevice) iOSStubDriver, err = NewStubIOSDriver(iOSDevice)
checkErr(t, err) assert.Nil(t, err)
} }
func TestIOSStubDriver_LoginNoneUI(t *testing.T) { func TestIOSStubDriver_LoginNoneUI(t *testing.T) {
setupIOSStubDriver(t) setupIOSStubDriver(t)
info, err := iOSStubDriver.LoginNoneUI("com.ss.iphone.ugc.AwemeInhouse", "12343418541", "", "im112233") info, err := iOSStubDriver.LoginNoneUI("com.ss.iphone.ugc.AwemeInhouse", "12343418541", "", "im112233")
checkErr(t, err) assert.Nil(t, err)
t.Logf("login info: %+v", info) t.Logf("login info: %+v", info)
} }
+3 -3
View File
@@ -1,9 +1,10 @@
package server_ext package server_ext
import ( import (
"github.com/gin-gonic/gin"
"strings" "strings"
"github.com/gin-gonic/gin"
"github.com/httprunner/httprunner/v5/pkg/uixt" "github.com/httprunner/httprunner/v5/pkg/uixt"
"github.com/httprunner/httprunner/v5/pkg/uixt/ai" "github.com/httprunner/httprunner/v5/pkg/uixt/ai"
"github.com/httprunner/httprunner/v5/pkg/uixt/driver_ext" "github.com/httprunner/httprunner/v5/pkg/uixt/driver_ext"
@@ -12,7 +13,6 @@ import (
func (p RouterBaseMethodExt) GetDriver(c *gin.Context) (driverExt uixt.IXTDriver, err error) { func (p RouterBaseMethodExt) GetDriver(c *gin.Context) (driverExt uixt.IXTDriver, err error) {
platform := c.Param("platform") platform := c.Param("platform")
serial := c.Param("serial")
deviceObj, exists := c.Get("device") deviceObj, exists := c.Get("device")
var device uixt.IDevice var device uixt.IDevice
var driver driver_ext.IStubDriver var driver driver_ext.IStubDriver
@@ -30,7 +30,7 @@ func (p RouterBaseMethodExt) GetDriver(c *gin.Context) (driverExt uixt.IXTDriver
case "ios": case "ios":
driver, err = driver_ext.NewStubIOSDriver(device.(*uixt.IOSDevice)) driver, err = driver_ext.NewStubIOSDriver(device.(*uixt.IOSDevice))
case "browser": case "browser":
driver, err = driver_ext.NewStubBrowserDriver(serial) driver, err = driver_ext.NewStubBrowserDriver(device.(*uixt.BrowserDevice))
} }
if err != nil { if err != nil {
server.RenderErrorInitDriver(c, err) server.RenderErrorInitDriver(c, err)