feat: 支持tunnel, 优化server结构。

This commit is contained in:
余泓铮
2025-02-19 20:26:20 +08:00
parent 1c6d12f7bd
commit ef320a3947
21 changed files with 233 additions and 170 deletions

View File

@@ -96,3 +96,14 @@ type XTDriver struct {
// cache screenshot results
screenResults []*ScreenResult
}
func (dExt *XTDriver) GetIDriver() IDriver {
return dExt.IDriver
}
type IXTDriver interface {
IDriver
GetIDriver() IDriver
GetScreenResult(opts ...option.ActionOption) (screenResult *ScreenResult, err error)
DoAction(action MobileAction) (err error)
}

View File

@@ -50,9 +50,6 @@ func NewStubAndroidDriver(dev *uixt.AndroidDevice) (*StubAndroidDriver, error) {
return nil, err
}
// register driver session reset handler
driver.Session.RegisterResetHandler(driver.Setup)
return driver, nil
}

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"time"
"github.com/httprunner/httprunner/v5/internal/builtin"
"github.com/httprunner/httprunner/v5/pkg/uixt"
"github.com/httprunner/httprunner/v5/pkg/uixt/ai"
"github.com/httprunner/httprunner/v5/pkg/uixt/option"
@@ -34,7 +33,7 @@ type XTDriver struct {
}
func (dExt *XTDriver) InstallByUrl(url string, opts ...option.InstallOption) error {
appPath, err := builtin.DownloadFileByUrl(url)
appPath, err := uixt.DownloadFileByUrl(url)
if err != nil {
return err
}

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"github.com/httprunner/httprunner/v5/pkg/uixt/types"
"net/url"
"os"
"time"
"github.com/httprunner/httprunner/v5/code"
@@ -44,9 +45,6 @@ func NewStubIOSDriver(dev *uixt.IOSDevice) (*StubIOSDriver, error) {
return nil, err
}
// register driver session reset handler
driver.Session.RegisterResetHandler(driver.Setup)
return driver, nil
}
@@ -565,9 +563,12 @@ func (s *StubIOSDriver) PressButton(devBtn types.DeviceButton) error {
}
func (s *StubIOSDriver) ScreenShot(opts ...option.ActionOption) (*bytes.Buffer, error) {
if os.Getenv("WINGS_LOCAL") == "true" {
return s.Device.ScreenShot()
}
err := s.setUpWda()
if err != nil {
return s.Device.ScreenShot()
return nil, err
}
return s.wdaDriver.ScreenShot()
}

View File

@@ -1,9 +1,17 @@
package uixt
import (
"crypto/md5"
"fmt"
"github.com/BurntSushi/locker"
"github.com/httprunner/httprunner/v5/internal/builtin"
"github.com/httprunner/httprunner/v5/internal/config"
"io"
"math"
"math/rand/v2"
"net/http"
"os"
"path/filepath"
"time"
"github.com/pkg/errors"
@@ -259,3 +267,53 @@ func sleepStrict(startTime time.Time, strictMilliseconds int64) {
Msg("sleep remaining duration time")
time.Sleep(time.Duration(dur) * time.Millisecond)
}
func DownloadFileByUrl(fileUrl string) (filePath string, err error) {
hash := md5.Sum([]byte(fileUrl))
fileName := fmt.Sprintf("%x", hash)
filePath = filepath.Join(config.DownloadsPath, fileName)
locker.Lock(filePath)
defer locker.Unlock(filePath)
if builtin.FileExists(filePath) {
return filePath, nil
}
fmt.Printf("Downloading file to %s from URL %s\n", filePath, fileUrl)
// Create an HTTP client with default settings.
client := &http.Client{}
// Build the HTTP GET request.
req, err := http.NewRequest("GET", fileUrl, nil)
if err != nil {
return "", err
}
// Perform the request.
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
// Check the HTTP status code.
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to download file: %s", resp.Status)
}
// Create the output file.
outFile, err := os.Create(fileName)
if err != nil {
return "", err
}
defer outFile.Close()
// Copy the response body to the file.
_, err = io.Copy(outFile, resp.Body)
if err != nil {
return "", err
}
fmt.Printf("File downloaded successfully: %s\n", fileName)
return filePath, nil
}

View File

@@ -28,7 +28,7 @@ import (
"github.com/httprunner/httprunner/v5/pkg/uixt/types"
)
func StartTunnel(recordsPath string, tunnelInfoPort int, userspaceTUN bool) (err error) {
func StartTunnel(ctx context.Context, recordsPath string, tunnelInfoPort int, userspaceTUN bool) (err error) {
pm, err := tunnel.NewPairRecordManager(recordsPath)
if err != nil {
return err
@@ -38,7 +38,7 @@ func StartTunnel(recordsPath string, tunnelInfoPort int, userspaceTUN bool) (err
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
for {
err := tm.UpdateTunnels(context.Background())
err := tm.UpdateTunnels(ctx)
if err != nil {
log.Error().Err(err).Msg("failed to update tunnels")
}
@@ -62,7 +62,7 @@ func RebootTunnel() (err error) {
if tunnelManager != nil {
_ = tunnelManager.Close()
}
return StartTunnel(os.TempDir(), ios.HttpApiPort(), true)
return StartTunnel(context.Background(), os.TempDir(), ios.HttpApiPort(), true)
}
func NewIOSDevice(opts ...option.IOSDeviceOption) (device *IOSDevice, err error) {