mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-10 17:43:00 +08:00
refactor: StubBrowserDriver
This commit is contained in:
@@ -1 +1 @@
|
||||
v5.0.0+2503041034
|
||||
v5.0.0+2503041108
|
||||
|
||||
@@ -62,7 +62,7 @@ func CreateBrowser(timeout int) (browserInfo *BrowserInfo, err error) {
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
Timeout: 30 * time.Second, // 设置超时时间为5秒
|
||||
Timeout: 30 * time.Second,
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
|
||||
@@ -166,7 +166,7 @@ func (sad *StubAndroidDriver) Source(srcOpt ...option.SourceOption) (source stri
|
||||
return res.(string), nil
|
||||
}
|
||||
|
||||
func (sad *StubAndroidDriver) LoginNoneUI(packageName, phoneNumber string, captcha, password string) (
|
||||
func (sad *StubAndroidDriver) LoginNoneUI(packageName, phoneNumber, captcha, password string) (
|
||||
info AppLoginInfo, err error) {
|
||||
app, err := sad.ForegroundInfo()
|
||||
if err != nil {
|
||||
@@ -182,7 +182,7 @@ func (sad *StubAndroidDriver) LoginNoneUI(packageName, phoneNumber string, captc
|
||||
}
|
||||
}
|
||||
|
||||
func (sad *StubAndroidDriver) LoginXigua(packageName, phoneNumber string, captcha, password string) (
|
||||
func (sad *StubAndroidDriver) LoginXigua(packageName, phoneNumber, captcha, password string) (
|
||||
info AppLoginInfo, err error) {
|
||||
loginSchema := ""
|
||||
if captcha != "" {
|
||||
@@ -198,7 +198,7 @@ func (sad *StubAndroidDriver) LoginXigua(packageName, phoneNumber string, captch
|
||||
return info, sad.OpenUrl(loginSchema)
|
||||
}
|
||||
|
||||
func (sad *StubAndroidDriver) LoginDouyin(packageName, phoneNumber string, captcha, password string) (
|
||||
func (sad *StubAndroidDriver) LoginDouyin(packageName, phoneNumber, captcha, password string) (
|
||||
info AppLoginInfo, err error) {
|
||||
|
||||
params := map[string]interface{}{
|
||||
|
||||
27
pkg/uixt/driver_ext/android_stub_driver_test.go
Normal file
27
pkg/uixt/driver_ext/android_stub_driver_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package driver_ext
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt"
|
||||
)
|
||||
|
||||
func setupAndroidStubDriver(t *testing.T) *StubAndroidDriver {
|
||||
device, err := uixt.NewAndroidDevice()
|
||||
require.Nil(t, err)
|
||||
device.Options.UIA2 = false
|
||||
device.Options.LogOn = false
|
||||
driver, err := NewStubAndroidDriver(device)
|
||||
require.Nil(t, err)
|
||||
return driver
|
||||
}
|
||||
|
||||
func TestAndroidStubDriver_LoginNoneUI(t *testing.T) {
|
||||
androidStubDriver := setupAndroidStubDriver(t)
|
||||
info, err := androidStubDriver.LoginNoneUI("com.ss.android.ugc.aweme", "12343418541", "", "im112233")
|
||||
assert.Nil(t, err)
|
||||
t.Logf("login info: %+v", info)
|
||||
}
|
||||
@@ -1,97 +1,27 @@
|
||||
package driver_ext
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt"
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/option"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const BROWSER_LOCAL_ADDRESS = "localhost:8093"
|
||||
|
||||
type WebAgentResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
Result interface{} `json:"result"`
|
||||
}
|
||||
|
||||
type CreateBrowserResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"msg"`
|
||||
Data BrowserInfo `json:"data"`
|
||||
}
|
||||
|
||||
type StubBrowserDriver struct {
|
||||
*uixt.BrowserDriver
|
||||
urlPrefix *url.URL
|
||||
|
||||
sessionId string
|
||||
scale float64
|
||||
}
|
||||
|
||||
type BrowserInfo struct {
|
||||
ContextId string `json:"context_id"`
|
||||
}
|
||||
|
||||
func CreateBrowser(timeout int) (browserInfo *BrowserInfo, err error) {
|
||||
data := map[string]interface{}{
|
||||
"timeout": timeout,
|
||||
}
|
||||
|
||||
var bsJSON []byte = nil
|
||||
if data != nil {
|
||||
if bsJSON, err = json.Marshal(data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
rawURL := "http://" + BROWSER_LOCAL_ADDRESS + "/api/v1/create_browser"
|
||||
req, err := http.NewRequest(http.MethodPost, rawURL, bytes.NewBuffer(bsJSON))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
Timeout: 30 * time.Second, // 设置超时时间为5秒
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rawResp, err := io.ReadAll(resp.Body)
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, errors.New(resp.Status)
|
||||
}
|
||||
|
||||
var result CreateBrowserResponse
|
||||
if err = json.Unmarshal(rawResp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if result.Code != 0 {
|
||||
return nil, errors.New(result.Message)
|
||||
}
|
||||
|
||||
return &result.Data, nil
|
||||
}
|
||||
|
||||
func NewStubBrowserDriver(device *uixt.BrowserDevice) (driver *StubBrowserDriver, err error) {
|
||||
BrowserWebDriver, err := uixt.NewBrowserDriver(device)
|
||||
browserDriver, err := uixt.NewBrowserDriver(device)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "create browser session failed")
|
||||
}
|
||||
driver = &StubBrowserDriver{
|
||||
BrowserDriver: BrowserWebDriver,
|
||||
BrowserDriver: browserDriver,
|
||||
}
|
||||
driver.sessionId = device.UUID()
|
||||
return driver, nil
|
||||
@@ -112,7 +42,8 @@ func (wd *StubBrowserDriver) Source(srcOpt ...option.SourceOption) (string, erro
|
||||
return string(jsonData), err
|
||||
}
|
||||
|
||||
func (wd *StubBrowserDriver) LoginNoneUI(packageName, phoneNumber string, captcha, password string) (info AppLoginInfo, err error) {
|
||||
func (wd *StubBrowserDriver) LoginNoneUI(packageName, phoneNumber, captcha, password string) (
|
||||
info AppLoginInfo, err error) {
|
||||
data := map[string]interface{}{
|
||||
"url": packageName,
|
||||
"web_cookie": password,
|
||||
@@ -129,3 +60,7 @@ func (wd *StubBrowserDriver) LoginNoneUI(packageName, phoneNumber string, captch
|
||||
}
|
||||
return loginSuccss, err
|
||||
}
|
||||
|
||||
func (wd *StubBrowserDriver) LogoutNoneUI(packageName string) error {
|
||||
return errors.New("not implemented")
|
||||
}
|
||||
|
||||
@@ -4,10 +4,11 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/types"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/types"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/code"
|
||||
"github.com/httprunner/httprunner/v5/internal/builtin"
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt"
|
||||
@@ -118,7 +119,7 @@ func (s *StubIOSDriver) OpenUrl(urlStr string, opts ...option.ActionOption) (err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *StubIOSDriver) LoginNoneUI(packageName, phoneNumber string, captcha, password string) (info AppLoginInfo, err error) {
|
||||
func (s *StubIOSDriver) LoginNoneUI(packageName, phoneNumber, captcha, password string) (info AppLoginInfo, err error) {
|
||||
appInfo, err := s.ForegroundInfo()
|
||||
if err != nil {
|
||||
return info, err
|
||||
@@ -132,7 +133,7 @@ func (s *StubIOSDriver) LoginNoneUI(packageName, phoneNumber string, captcha, pa
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StubIOSDriver) LoginXigua(packageName, phoneNumber string, captcha, password string) (info AppLoginInfo, err error) {
|
||||
func (s *StubIOSDriver) LoginXigua(packageName, phoneNumber, captcha, password string) (info AppLoginInfo, err error) {
|
||||
loginSchema := ""
|
||||
if captcha != "" {
|
||||
loginSchema = fmt.Sprintf("snssdk32://local_channel_autologin?login_type=1&account=%s&smscode=%s", phoneNumber, captcha)
|
||||
@@ -145,7 +146,7 @@ func (s *StubIOSDriver) LoginXigua(packageName, phoneNumber string, captcha, pas
|
||||
return info, s.OpenUrl(loginSchema)
|
||||
}
|
||||
|
||||
func (s *StubIOSDriver) LoginDouyin(packageName, phoneNumber string, captcha, password string) (info AppLoginInfo, err error) {
|
||||
func (s *StubIOSDriver) LoginDouyin(packageName, phoneNumber, captcha, password string) (info AppLoginInfo, err error) {
|
||||
params := map[string]interface{}{
|
||||
"phone": phoneNumber,
|
||||
}
|
||||
@@ -308,6 +309,7 @@ func (s *StubIOSDriver) InitSession(capabilities option.Capabilities) error {
|
||||
}
|
||||
return s.WDADriver.InitSession(capabilities)
|
||||
}
|
||||
|
||||
func (s *StubIOSDriver) GetSession() *uixt.DriverSession {
|
||||
if err := s.SetupWda(); err != nil {
|
||||
_ = errors.Wrap(code.DeviceHTTPDriverError, err.Error())
|
||||
|
||||
@@ -4,25 +4,25 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt"
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt/option"
|
||||
)
|
||||
|
||||
var iOSStubDriver *StubIOSDriver
|
||||
|
||||
func setupIOSStubDriver(t *testing.T) {
|
||||
func setupIOSStubDriver(t *testing.T) *StubIOSDriver {
|
||||
iOSDevice, err := uixt.NewIOSDevice(
|
||||
option.WithWDAPort(8700),
|
||||
option.WithWDAMjpegPort(8800),
|
||||
option.WithResetHomeOnStartup(false))
|
||||
assert.Nil(t, err)
|
||||
iOSStubDriver, err = NewStubIOSDriver(iOSDevice)
|
||||
assert.Nil(t, err)
|
||||
require.Nil(t, err)
|
||||
iOSStubDriver, err := NewStubIOSDriver(iOSDevice)
|
||||
require.Nil(t, err)
|
||||
return iOSStubDriver
|
||||
}
|
||||
|
||||
func TestIOSStubDriver_LoginNoneUI(t *testing.T) {
|
||||
setupIOSStubDriver(t)
|
||||
iOSStubDriver := setupIOSStubDriver(t)
|
||||
info, err := iOSStubDriver.LoginNoneUI("com.ss.iphone.ugc.AwemeInhouse", "12343418541", "", "im112233")
|
||||
assert.Nil(t, err)
|
||||
t.Logf("login info: %+v", info)
|
||||
|
||||
Reference in New Issue
Block a user