mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-06 16:07:45 +08:00
refactor: setup browser session
This commit is contained in:
+4
-3
@@ -14,13 +14,14 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
"github.com/httprunner/httprunner/v5/code"
|
"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/internal/version"
|
"github.com/httprunner/httprunner/v5/internal/version"
|
||||||
"github.com/httprunner/httprunner/v5/uixt"
|
"github.com/httprunner/httprunner/v5/uixt"
|
||||||
"github.com/httprunner/httprunner/v5/uixt/option"
|
"github.com/httprunner/httprunner/v5/uixt/option"
|
||||||
"github.com/pkg/errors"
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type UIXTRunner struct {
|
type UIXTRunner struct {
|
||||||
@@ -219,7 +220,7 @@ func (configs *UIXTConfig) getWDALocalPort(udid string) (string, error) {
|
|||||||
"device_id": udid,
|
"device_id": udid,
|
||||||
})
|
})
|
||||||
req, err := http.NewRequest("POST",
|
req, err := http.NewRequest("POST",
|
||||||
fmt.Sprintf("http://127.0.0.1:%d/get_device_port", configs.WDAMjpegPort),
|
fmt.Sprintf("http://localhost:%d/get_device_port", configs.WDAMjpegPort),
|
||||||
bytes.NewBuffer(payloadBytes))
|
bytes.NewBuffer(payloadBytes))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.Wrap(err, "create request failed")
|
return "", errors.Wrap(err, "create request failed")
|
||||||
|
|||||||
+15
-16
@@ -19,7 +19,7 @@ import (
|
|||||||
"github.com/httprunner/httprunner/v5/uixt/types"
|
"github.com/httprunner/httprunner/v5/uixt/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
const BROWSER_LOCAL_ADDRESS = "localhost:8093"
|
const BROWSER_BASE_URL = "http://localhost:8093"
|
||||||
|
|
||||||
type WebAgentResponse struct {
|
type WebAgentResponse struct {
|
||||||
Code int `json:"code"`
|
Code int `json:"code"`
|
||||||
@@ -35,9 +35,8 @@ type CreateBrowserResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type BrowserDriver struct {
|
type BrowserDriver struct {
|
||||||
urlPrefix *url.URL
|
Session *DriverSession
|
||||||
Session *DriverSession
|
Device *BrowserDevice
|
||||||
Device *BrowserDevice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type BrowserInfo struct {
|
type BrowserInfo struct {
|
||||||
@@ -61,7 +60,7 @@ func CreateBrowser(timeout int, width, height int) (browserInfo *BrowserInfo, er
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
rawURL := "http://" + BROWSER_LOCAL_ADDRESS + "/api/v1/create_browser"
|
rawURL := BROWSER_BASE_URL + "/api/v1/create_browser"
|
||||||
req, err := http.NewRequest(http.MethodPost, rawURL, bytes.NewBuffer(bsJSON))
|
req, err := http.NewRequest(http.MethodPost, rawURL, bytes.NewBuffer(bsJSON))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
@@ -98,20 +97,23 @@ func NewBrowserDriver(device *BrowserDevice) (driver *BrowserDriver, err error)
|
|||||||
log.Info().Msg("init NewBrowserDriver driver")
|
log.Info().Msg("init NewBrowserDriver driver")
|
||||||
driver = new(BrowserDriver)
|
driver = new(BrowserDriver)
|
||||||
driver.Device = device
|
driver.Device = device
|
||||||
driver.urlPrefix = &url.URL{}
|
|
||||||
driver.urlPrefix.Host = BROWSER_LOCAL_ADDRESS
|
err = driver.Setup()
|
||||||
driver.urlPrefix.Scheme = "http"
|
if err != nil {
|
||||||
driver.Session = NewDriverSession()
|
return nil, errors.Wrap(err, "setup browser driver failed")
|
||||||
driver.Session.ID = device.UUID()
|
}
|
||||||
|
|
||||||
return driver, nil
|
return driver, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wd *BrowserDriver) Setup() error {
|
func (wd *BrowserDriver) Setup() error {
|
||||||
|
wd.Session = NewDriverSession()
|
||||||
err := wd.Session.SetupPortForward(8093)
|
err := wd.Session.SetupPortForward(8093)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
wd.Session.SetBaseURL(BROWSER_LOCAL_ADDRESS)
|
wd.Session.SetBaseURL(BROWSER_BASE_URL)
|
||||||
|
wd.Session.ID = wd.Device.UUID()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -263,7 +265,7 @@ func (wd *BrowserDriver) SecondaryClickBySelector(selector string, options ...op
|
|||||||
|
|
||||||
func (wd *BrowserDriver) 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...)
|
||||||
baseURL := fmt.Sprintf("http://%s/api/v1/%s/element_text", BROWSER_LOCAL_ADDRESS, wd.Session.ID)
|
baseURL := fmt.Sprintf("%s/api/v1/%s/element_text", BROWSER_BASE_URL, wd.Session.ID)
|
||||||
|
|
||||||
// 使用 url.Values 构建查询参数
|
// 使用 url.Values 构建查询参数
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
@@ -404,10 +406,7 @@ func (wd *BrowserDriver) ScreenShot(options ...option.ActionOption) (*bytes.Buff
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (wd *BrowserDriver) concatURL(elem ...string) string {
|
func (wd *BrowserDriver) concatURL(elem ...string) string {
|
||||||
tmp, _ := url.Parse(wd.urlPrefix.String())
|
return path.Join(append([]string{"/api/v1"}, elem...)...)
|
||||||
commonPath := path.Join(append([]string{wd.urlPrefix.Path}, "api/v1/")...)
|
|
||||||
tmp.Path = path.Join(append([]string{commonPath}, elem...)...)
|
|
||||||
return tmp.String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wd *BrowserDriver) Status() (deviceStatus types.DeviceStatus, err error) {
|
func (wd *BrowserDriver) Status() (deviceStatus types.DeviceStatus, err error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user