refactor: move shoots related server to ext

This commit is contained in:
lilong.129
2025-02-10 19:57:39 +08:00
parent 1ec383180b
commit c6b0aefc56
12 changed files with 305 additions and 156 deletions

View File

@@ -13,7 +13,7 @@ var serverCmd = &cobra.Command{
Long: `start hrp server, call httprunner by HTTP`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return server.NewServer(port)
return server.NewRouter().Run(port)
},
}

View File

@@ -1 +1 @@
v5.0.0+2502101335
v5.0.0+2502101959

View File

@@ -10,12 +10,12 @@ import (
)
func foregroundAppHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
dExt, err := GetContextDriver(c)
if err != nil {
return
}
appInfo, err := dExt.Driver.GetForegroundApp()
appInfo, err := dExt.GetDriver().GetForegroundApp()
if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to unlick screen", c.HandlerName()))
c.JSON(http.StatusInternalServerError,
@@ -31,7 +31,7 @@ func foregroundAppHandler(c *gin.Context) {
}
func clearAppHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
dExt, err := GetContextDriver(c)
if err != nil {
return
}
@@ -42,7 +42,7 @@ func clearAppHandler(c *gin.Context) {
return
}
err = dExt.Driver.Clear(appClearReq.PackageName)
err = dExt.GetDriver().Clear(appClearReq.PackageName)
if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to unlick screen", c.HandlerName()))
c.JSON(http.StatusInternalServerError,
@@ -58,7 +58,7 @@ func clearAppHandler(c *gin.Context) {
}
func launchAppHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
dExt, err := GetContextDriver(c)
if err != nil {
return
}
@@ -69,7 +69,7 @@ func launchAppHandler(c *gin.Context) {
return
}
err = dExt.Driver.AppLaunch(appLaunchReq.PackageName)
err = dExt.GetDriver().AppLaunch(appLaunchReq.PackageName)
if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to launch app %s", c.HandlerName(), appLaunchReq.PackageName))
c.JSON(http.StatusInternalServerError,
@@ -85,7 +85,7 @@ func launchAppHandler(c *gin.Context) {
}
func terminalAppHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
dExt, err := GetContextDriver(c)
if err != nil {
return
}
@@ -96,7 +96,7 @@ func terminalAppHandler(c *gin.Context) {
return
}
success, err := dExt.Driver.AppTerminate(appTerminalReq.PackageName)
success, err := dExt.GetDriver().AppTerminate(appTerminalReq.PackageName)
if !success {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to launch app %s", c.HandlerName(), appTerminalReq.PackageName))
c.JSON(http.StatusInternalServerError,

View File

@@ -15,7 +15,7 @@ import (
var uiClients = make(map[string]uixt.IDriverExt) // UI automation clients for iOS and Android, key is udid/serial
func handleDeviceContext() gin.HandlerFunc {
func (r *Router) HandleDeviceContext() gin.HandlerFunc {
return func(c *gin.Context) {
platform := c.Param("platform")
serial := c.Param("serial")
@@ -81,7 +81,7 @@ func handleDeviceContext() gin.HandlerFunc {
}
}
func getContextDriver(c *gin.Context) (*uixt.DriverExt, error) {
func GetContextDriver(c *gin.Context) (uixt.IDriverExt, error) {
driverObj, exists := c.Get("driver")
if !exists {
handlerInitDeviceDriverFailedContext(c)

View File

@@ -6,10 +6,9 @@ import (
"strings"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v5/code"
"github.com/httprunner/httprunner/v5/pkg/gadb"
"github.com/rs/zerolog/log"
)
func listDeviceHandler(c *gin.Context) {
@@ -92,57 +91,3 @@ func listDeviceHandler(c *gin.Context) {
}
}
}
func loginHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
if err != nil {
return
}
var loginReq LoginRequest
if err := c.ShouldBindJSON(&loginReq); err != nil {
handlerValidateRequestFailedContext(c, err)
return
}
info, err := dExt.Driver.LoginNoneUI(loginReq.PackageName, loginReq.PhoneNumber, loginReq.Captcha, loginReq.Password)
if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to login", c.HandlerName()))
c.JSON(http.StatusInternalServerError,
HttpResponse{
Code: code.GetErrorCode(err),
Message: err.Error(),
},
)
c.Abort()
return
}
c.JSON(http.StatusOK, HttpResponse{Code: 0, Message: "success", Result: info})
}
func logoutHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
if err != nil {
return
}
var logoutReq LogoutRequest
if err := c.ShouldBindJSON(&logoutReq); err != nil {
handlerValidateRequestFailedContext(c, err)
return
}
err = dExt.Driver.LogoutNoneUI(logoutReq.PackageName)
if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to login", c.HandlerName()))
c.JSON(http.StatusInternalServerError,
HttpResponse{
Code: code.GetErrorCode(err),
Message: err.Error(),
},
)
c.Abort()
return
}
c.JSON(http.StatusOK, HttpResponse{Code: 0, Message: "success"})
}

242
server/ext/shoots.go Normal file
View File

@@ -0,0 +1,242 @@
package server_ext
import (
"fmt"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v5/code"
"github.com/httprunner/httprunner/v5/pkg/uixt"
"github.com/httprunner/httprunner/v5/pkg/uixt/driver_ext"
"github.com/httprunner/httprunner/v5/pkg/uixt/option"
"github.com/httprunner/httprunner/v5/server"
)
func NewExtServer(port int) error {
router := server.NewRouter()
apiV1PlatformSerial := router.Group("/api/v1").Group("/:platform").Group("/:serial")
// shoots operations
apiV1PlatformSerial.GET("/shoots/source", handleDeviceContext(), sourceHandler)
apiV1PlatformSerial.POST("/shoots/login", handleDeviceContext(), loginHandler)
apiV1PlatformSerial.POST("/shoots/logout", handleDeviceContext(), logoutHandler)
err := router.Engine.Run(fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
log.Err(err).Msg("failed to start http server")
return err
}
return nil
}
func sourceHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
if err != nil {
return
}
app, err := dExt.GetForegroundApp()
if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to get foreground app", c.HandlerName()))
c.JSON(http.StatusInternalServerError,
server.HttpResponse{
Code: code.GetErrorCode(err),
Message: err.Error(),
},
)
c.Abort()
return
}
source, err := dExt.Source(option.NewSourceOption().WithProcessName(app.PackageName))
if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to get source %s", c.HandlerName(), app.PackageName))
c.JSON(http.StatusInternalServerError,
server.HttpResponse{
Code: code.GetErrorCode(err),
Message: err.Error(),
},
)
c.Abort()
return
}
c.JSON(http.StatusOK, server.HttpResponse{Result: source})
}
func loginHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
if err != nil {
return
}
var loginReq LoginRequest
if err := c.ShouldBindJSON(&loginReq); err != nil {
log.Error().Err(err).Msg("validate request failed")
c.JSON(http.StatusBadRequest, server.HttpResponse{
Code: code.GetErrorCode(code.InvalidParamError),
Message: fmt.Sprintf("validate request param failed: %s", err.Error()),
})
c.Abort()
return
}
var info driver_ext.AppLoginInfo
platform := c.Param("platform")
if platform == "android" {
info, err = dExt.(*driver_ext.ShootsAndroidDriver).LoginNoneUI(
loginReq.PackageName, loginReq.PhoneNumber, loginReq.Captcha, loginReq.Password)
} else {
// ios
info, err = dExt.(*driver_ext.ShootsIOSDriver).LoginNoneUI(
loginReq.PackageName, loginReq.PhoneNumber, loginReq.Captcha, loginReq.Password)
}
if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to login", c.HandlerName()))
c.JSON(http.StatusInternalServerError,
server.HttpResponse{
Code: code.GetErrorCode(err),
Message: err.Error(),
},
)
c.Abort()
return
}
c.JSON(http.StatusOK, server.HttpResponse{Code: 0, Message: "success", Result: info})
}
func logoutHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
if err != nil {
return
}
var logoutReq LogoutRequest
if err := c.ShouldBindJSON(&logoutReq); err != nil {
log.Error().Err(err).Msg("validate request failed")
c.JSON(http.StatusBadRequest, server.HttpResponse{
Code: code.GetErrorCode(code.InvalidParamError),
Message: fmt.Sprintf("validate request param failed: %s", err.Error()),
})
c.Abort()
return
}
platform := c.Param("platform")
if platform == "android" {
err = dExt.(*driver_ext.ShootsAndroidDriver).LogoutNoneUI(logoutReq.PackageName)
} else {
// ios
err = dExt.(*driver_ext.ShootsIOSDriver).LogoutNoneUI(logoutReq.PackageName)
}
if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to login", c.HandlerName()))
c.JSON(http.StatusInternalServerError,
server.HttpResponse{
Code: code.GetErrorCode(err),
Message: err.Error(),
},
)
c.Abort()
return
}
c.JSON(http.StatusOK, server.HttpResponse{Code: 0, Message: "success"})
}
type LoginRequest struct {
PackageName string `json:"packageName"`
PhoneNumber string `json:"phoneNumber"`
Captcha string `json:"captcha"`
Password string `json:"password"`
}
type LogoutRequest struct {
PackageName string `json:"packageName"`
}
var uiClients = make(map[string]uixt.IDriver) // UI automation clients for iOS and Android, key is udid/serial
func handleDeviceContext() gin.HandlerFunc {
return func(c *gin.Context) {
platform := c.Param("platform")
serial := c.Param("serial")
if serial == "" {
log.Error().Str("platform", platform).Msg(fmt.Sprintf("[%s]: serial is empty", c.HandlerName()))
c.JSON(http.StatusBadRequest, server.HttpResponse{
Code: code.GetErrorCode(code.InvalidParamError),
Message: "serial is empty",
})
c.Abort()
return
}
// get cached driver
if driver, ok := uiClients[serial]; ok {
c.Set("driver", driver)
c.Next()
return
}
switch strings.ToLower(platform) {
case "android":
device, err := uixt.NewAndroidDevice(
option.WithSerialNumber(serial))
if err != nil {
log.Error().Err(err).Str("platform", platform).Str("serial", serial).
Msg("android device not found")
c.JSON(http.StatusBadRequest,
server.HttpResponse{
Code: code.GetErrorCode(err),
Message: err.Error(),
},
)
c.Abort()
return
}
device.Setup()
driver, err := driver_ext.NewShootsAndroidDriver(device)
if err != nil {
log.Error().Err(err).Str("platform", platform).Str("serial", serial).
Msg("failed to init shoots android driver")
c.JSON(http.StatusInternalServerError,
server.HttpResponse{
Code: code.GetErrorCode(err),
Message: err.Error(),
},
)
c.Abort()
return
}
c.Set("driver", driver)
// cache driver
uiClients[serial] = driver
default:
c.JSON(http.StatusBadRequest, server.HttpResponse{
Code: code.GetErrorCode(code.InvalidParamError),
Message: fmt.Sprintf("unsupported platform %s", platform),
})
c.Abort()
return
}
c.Next()
}
}
func getContextDriver(c *gin.Context) (uixt.IDriver, error) {
driverObj, exists := c.Get("driver")
if !exists {
log.Error().Msg("init device driver failed")
c.JSON(http.StatusInternalServerError,
server.HttpResponse{
Code: code.GetErrorCode(code.MobileUIDriverError),
Message: "init driver failed",
},
)
c.Abort()
return nil, fmt.Errorf("driver not found")
}
dExt := driverObj.(uixt.IDriver)
return dExt, nil
}

View File

@@ -11,12 +11,12 @@ import (
)
func unlockHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
dExt, err := GetContextDriver(c)
if err != nil {
return
}
err = dExt.Driver.Unlock()
err = dExt.GetDriver().Unlock()
if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to unlick screen", c.HandlerName()))
c.JSON(http.StatusInternalServerError,
@@ -32,12 +32,12 @@ func unlockHandler(c *gin.Context) {
}
func homeHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
dExt, err := GetContextDriver(c)
if err != nil {
return
}
err = dExt.Driver.Homescreen()
err = dExt.GetDriver().Homescreen()
if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to enter homescreen", c.HandlerName()))
c.JSON(http.StatusInternalServerError,
@@ -53,7 +53,7 @@ func homeHandler(c *gin.Context) {
}
func keycodeHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
dExt, err := GetContextDriver(c)
if err != nil {
return
}
@@ -64,7 +64,7 @@ func keycodeHandler(c *gin.Context) {
return
}
err = dExt.Driver.PressKeyCode(uixt.KeyCode(keycodeReq.Keycode))
err = dExt.GetDriver().PressKeyCode(uixt.KeyCode(keycodeReq.Keycode))
if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to input keycode %d", c.HandlerName(), keycodeReq.Keycode))
c.JSON(http.StatusInternalServerError,

View File

@@ -8,41 +8,48 @@ import (
"github.com/rs/zerolog/log"
)
func NewServer(port int) error {
router := gin.Default()
router.GET("/ping", pingHandler)
func NewRouter() *Router {
router := &Router{}
router.Init()
return router
}
apiV1Platform := router.Group("/api/v1").Group("/:platform")
type Router struct {
*gin.Engine
}
func (r *Router) Init() {
r.Engine = gin.Default()
r.Engine.GET("/ping", pingHandler)
apiV1Platform := r.Engine.Group("/api/v1").Group("/:platform")
apiV1Platform.GET("/devices", listDeviceHandler)
apiV1PlatformSerial := apiV1Platform.Group("/:serial")
// UI operations
apiV1PlatformSerial.POST("/ui/tap", handleDeviceContext(), tapHandler)
apiV1PlatformSerial.POST("/ui/drag", handleDeviceContext(), dragHandler)
apiV1PlatformSerial.POST("/ui/input", handleDeviceContext(), inputHandler)
apiV1PlatformSerial.POST("/ui/tap", r.HandleDeviceContext(), tapHandler)
apiV1PlatformSerial.POST("/ui/drag", r.HandleDeviceContext(), dragHandler)
apiV1PlatformSerial.POST("/ui/input", r.HandleDeviceContext(), inputHandler)
// Key operations
apiV1PlatformSerial.POST("/key/unlock", handleDeviceContext(), unlockHandler)
apiV1PlatformSerial.POST("/key/home", handleDeviceContext(), homeHandler)
apiV1PlatformSerial.POST("/key", handleDeviceContext(), keycodeHandler)
apiV1PlatformSerial.POST("/key/unlock", r.HandleDeviceContext(), unlockHandler)
apiV1PlatformSerial.POST("/key/home", r.HandleDeviceContext(), homeHandler)
apiV1PlatformSerial.POST("/key", r.HandleDeviceContext(), keycodeHandler)
// App operations
apiV1PlatformSerial.GET("/app/foreground", handleDeviceContext(), foregroundAppHandler)
apiV1PlatformSerial.POST("/app/clear", handleDeviceContext(), clearAppHandler)
apiV1PlatformSerial.POST("/app/launch", handleDeviceContext(), launchAppHandler)
apiV1PlatformSerial.POST("/app/terminal", handleDeviceContext(), terminalAppHandler)
apiV1PlatformSerial.GET("/app/foreground", r.HandleDeviceContext(), foregroundAppHandler)
apiV1PlatformSerial.POST("/app/clear", r.HandleDeviceContext(), clearAppHandler)
apiV1PlatformSerial.POST("/app/launch", r.HandleDeviceContext(), launchAppHandler)
apiV1PlatformSerial.POST("/app/terminal", r.HandleDeviceContext(), terminalAppHandler)
// get screen info
apiV1PlatformSerial.GET("/screenshot", handleDeviceContext(), screenshotHandler)
apiV1PlatformSerial.POST("/screenresult", handleDeviceContext(), screenResultHandler)
apiV1PlatformSerial.GET("/shoots/source", handleDeviceContext(), sourceHandler)
apiV1PlatformSerial.GET("/adb/source", handleDeviceContext(), adbSourceHandler)
// shoots operations
apiV1PlatformSerial.POST("/shoots/login", handleDeviceContext(), loginHandler)
apiV1PlatformSerial.POST("/shoots/logout", handleDeviceContext(), logoutHandler)
apiV1PlatformSerial.GET("/screenshot", r.HandleDeviceContext(), screenshotHandler)
apiV1PlatformSerial.POST("/screenresult", r.HandleDeviceContext(), screenResultHandler)
apiV1PlatformSerial.GET("/adb/source", r.HandleDeviceContext(), adbSourceHandler)
// run uixt actions
apiV1PlatformSerial.POST("/uixt/action", handleDeviceContext(), uixtActionHandler)
apiV1PlatformSerial.POST("/uixt/actions", handleDeviceContext(), uixtActionsHandler)
apiV1PlatformSerial.POST("/uixt/action", r.HandleDeviceContext(), uixtActionHandler)
apiV1PlatformSerial.POST("/uixt/actions", r.HandleDeviceContext(), uixtActionsHandler)
}
err := router.Run(fmt.Sprintf("127.0.0.1:%d", port))
func (r *Router) Run(port int) error {
err := r.Engine.Run(fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
log.Err(err).Msg("failed to start http server")
return err

View File

@@ -51,14 +51,3 @@ type AppLaunchRequest struct {
type AppTerminalRequest struct {
PackageName string `json:"packageName"`
}
type LoginRequest struct {
PackageName string `json:"packageName"`
PhoneNumber string `json:"phoneNumber"`
Captcha string `json:"captcha"`
Password string `json:"password"`
}
type LogoutRequest struct {
PackageName string `json:"packageName"`
}

View File

@@ -13,12 +13,12 @@ import (
)
func screenshotHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
dExt, err := GetContextDriver(c)
if err != nil {
return
}
raw, err := dExt.Driver.Screenshot()
raw, err := dExt.GetDriver().Screenshot()
if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to get screenshot", c.HandlerName()))
c.JSON(http.StatusInternalServerError,
@@ -41,7 +41,7 @@ func screenshotHandler(c *gin.Context) {
}
func screenResultHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
dExt, err := GetContextDriver(c)
if err != nil {
return
}
@@ -78,47 +78,13 @@ func screenResultHandler(c *gin.Context) {
)
}
func sourceHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
if err != nil {
return
}
app, err := dExt.Driver.GetForegroundApp()
if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to get foreground app", c.HandlerName()))
c.JSON(http.StatusInternalServerError,
HttpResponse{
Code: code.GetErrorCode(err),
Message: err.Error(),
},
)
c.Abort()
return
}
source, err := dExt.Driver.Source(option.NewSourceOption().WithProcessName(app.PackageName))
if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to get source %s", c.HandlerName(), app.PackageName))
c.JSON(http.StatusInternalServerError,
HttpResponse{
Code: code.GetErrorCode(err),
Message: err.Error(),
},
)
c.Abort()
return
}
c.JSON(http.StatusOK, HttpResponse{Result: source})
}
func adbSourceHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
dExt, err := GetContextDriver(c)
if err != nil {
return
}
source, err := dExt.Driver.Source()
source, err := dExt.GetDriver().Source()
if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to get adb source", c.HandlerName()))
c.JSON(http.StatusInternalServerError,

View File

@@ -12,7 +12,7 @@ import (
)
func tapHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
dExt, err := GetContextDriver(c)
if err != nil {
return
}
@@ -72,7 +72,7 @@ func tapHandler(c *gin.Context) {
}
func dragHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
dExt, err := GetContextDriver(c)
if err != nil {
return
}
@@ -107,7 +107,7 @@ func dragHandler(c *gin.Context) {
return
}
} else {
err := dExt.Driver.Swipe(
err := dExt.GetDriver().Swipe(
dragReq.FromX, dragReq.FromY, dragReq.ToX, dragReq.ToY,
actionOptions...)
if err != nil {
@@ -129,7 +129,7 @@ func dragHandler(c *gin.Context) {
}
func inputHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
dExt, err := GetContextDriver(c)
if err != nil {
return
}
@@ -140,7 +140,7 @@ func inputHandler(c *gin.Context) {
return
}
err = dExt.Driver.SendKeys(inputReq.Text,
err = dExt.GetDriver().SendKeys(inputReq.Text,
option.WithFrequency(inputReq.Frequency))
if err != nil {
log.Err(err).Msg(fmt.Sprintf("[%s]: failed to input text %s", c.HandlerName(), inputReq.Text))

View File

@@ -11,7 +11,7 @@ import (
// exec a single uixt action
func uixtActionHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
dExt, err := GetContextDriver(c)
if err != nil {
return
}
@@ -40,7 +40,7 @@ func uixtActionHandler(c *gin.Context) {
// exec multiple uixt actions
func uixtActionsHandler(c *gin.Context) {
dExt, err := getContextDriver(c)
dExt, err := GetContextDriver(c)
if err != nil {
return
}