mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-06 20:32:44 +08:00
update: fucntion call
This commit is contained in:
@@ -1 +1 @@
|
||||
v5.0.0-beta-2505072021
|
||||
v5.0.0-beta-2505072139
|
||||
|
||||
@@ -40,7 +40,7 @@ func (r *Router) rightClickHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
err = driver.IDriver.(*uixt.BrowserDriver).
|
||||
RightClick(rightClickReq.X, rightClickReq.Y)
|
||||
SecondaryClick(rightClickReq.X, rightClickReq.Y)
|
||||
if err != nil {
|
||||
RenderError(c, err)
|
||||
return
|
||||
|
||||
@@ -300,7 +300,7 @@ func (s *StepMobile) SwipeToTapTexts(texts interface{}, opts ...option.ActionOpt
|
||||
|
||||
func (s *StepMobile) RightClick(x, y float64, options ...option.ActionOption) *StepMobile {
|
||||
action := uixt.MobileAction{
|
||||
Method: uixt.ACTION_RightClick,
|
||||
Method: uixt.ACTION_SecondaryClick,
|
||||
Params: []float64{x, y},
|
||||
Options: option.NewActionOptions(options...),
|
||||
}
|
||||
@@ -310,7 +310,7 @@ func (s *StepMobile) RightClick(x, y float64, options ...option.ActionOption) *S
|
||||
|
||||
func (s *StepMobile) RightClickBySelector(selector string, options ...option.ActionOption) *StepMobile {
|
||||
action := uixt.MobileAction{
|
||||
Method: uixt.ACTION_RightClickBySelector,
|
||||
Method: uixt.ACTION_SecondaryClickBySelector,
|
||||
Params: selector,
|
||||
Options: option.NewActionOptions(options...),
|
||||
}
|
||||
|
||||
@@ -566,15 +566,6 @@ func (ad *ADBDriver) ScreenShot(opts ...option.ActionOption) (raw *bytes.Buffer,
|
||||
return raw, nil
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) TapByHierarchy(text string, opts ...option.ActionOption) error {
|
||||
log.Info().Str("text", text).Msg("ADBDriver.TapByHierarchy")
|
||||
sourceTree, err := ad.sourceTree()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ad.tapByTextUsingHierarchy(sourceTree, text, opts...)
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) Source(srcOpt ...option.SourceOption) (source string, err error) {
|
||||
_, err = ad.runShellCommand("rm", "-rf", "/sdcard/window_dump.xml")
|
||||
if err != nil {
|
||||
@@ -1123,3 +1114,24 @@ func ConvertPoints(lines []string) (eps []ExportPoint) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) HoverBySelector(selector string, options ...option.ActionOption) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) TapBySelector(text string, opts ...option.ActionOption) error {
|
||||
log.Info().Str("text", text).Msg("ADBDriver.TapByHierarchy")
|
||||
sourceTree, err := ad.sourceTree()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ad.tapByTextUsingHierarchy(sourceTree, text, opts...)
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) SecondaryClick(x, y float64) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
func (ad *ADBDriver) SecondaryClickBySelector(selector string, options ...option.ActionOption) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -181,7 +181,7 @@ func (wd *BrowserDriver) Scroll(delta int) (err error) {
|
||||
data := map[string]interface{}{
|
||||
"delta": delta,
|
||||
}
|
||||
_, err = wd.HttpPOST(data, wd.sessionId, "ui/scroll")
|
||||
_, err = wd.Session.POST(data, wd.concatURL(wd.sessionId, "ui/scroll"))
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -200,7 +200,7 @@ func (wd *BrowserDriver) CreateNetListener() (*websocket.Conn, error) {
|
||||
return c, err
|
||||
}
|
||||
|
||||
func (wd *BrowserDriver) ClosePage(pageIndex int) (err error) {
|
||||
func (wd *BrowserDriver) CloseTab(pageIndex int) (err error) {
|
||||
data := map[string]interface{}{
|
||||
"page_index": pageIndex,
|
||||
}
|
||||
@@ -233,7 +233,7 @@ func (wd *BrowserDriver) TapBySelector(selector string, options ...option.Action
|
||||
return err
|
||||
}
|
||||
|
||||
func (wd *BrowserDriver) RightClick(x, y float64) (err error) {
|
||||
func (wd *BrowserDriver) SecondaryClick(x, y float64) (err error) {
|
||||
data := map[string]interface{}{
|
||||
"x": x,
|
||||
"y": y,
|
||||
@@ -242,7 +242,7 @@ func (wd *BrowserDriver) RightClick(x, y float64) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
func (wd *BrowserDriver) RightClickBySelector(selector string, options ...option.ActionOption) (err error) {
|
||||
func (wd *BrowserDriver) SecondaryClickBySelector(selector string, options ...option.ActionOption) (err error) {
|
||||
data := map[string]interface{}{
|
||||
"selector": selector,
|
||||
}
|
||||
@@ -396,21 +396,6 @@ func (wd *BrowserDriver) ScreenShot(options ...option.ActionOption) (*bytes.Buff
|
||||
return bytes.NewBuffer(screenRaw), nil
|
||||
}
|
||||
|
||||
func (wd *BrowserDriver) HttpPOST(data interface{}, pathElem ...string) (response *WebAgentResponse, err error) {
|
||||
var bsJSON []byte = nil
|
||||
if data != nil {
|
||||
if bsJSON, err = json.Marshal(data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return wd.httpRequest(http.MethodPost, wd.concatURL(pathElem...), bsJSON)
|
||||
}
|
||||
|
||||
func (wd *BrowserDriver) HttpGet(data interface{}, pathElem ...string) (response *WebAgentResponse, err error) {
|
||||
return wd.httpRequest(http.MethodGet, wd.concatURL(pathElem...), nil)
|
||||
}
|
||||
|
||||
func (wd *BrowserDriver) concatURL(elem ...string) string {
|
||||
tmp, _ := url.Parse(wd.urlPrefix.String())
|
||||
commonPath := path.Join(append([]string{wd.urlPrefix.Path}, "api/v1/")...)
|
||||
@@ -418,45 +403,6 @@ func (wd *BrowserDriver) concatURL(elem ...string) string {
|
||||
return tmp.String()
|
||||
}
|
||||
|
||||
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.Header.Set("Content-Type", "application/json")
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 新建http client
|
||||
client := &http.Client{
|
||||
Timeout: 60 * time.Second, // 设置超时时间为5秒
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rawResp, _ := io.ReadAll(resp.Body)
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, errors.New(resp.Status)
|
||||
}
|
||||
|
||||
// 将结果解析为 JSON
|
||||
var result WebAgentResponse
|
||||
if err = json.Unmarshal(rawResp, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if result.Code != 0 {
|
||||
log.Info().Msgf("%v", result.Message)
|
||||
return nil, errors.New(result.Message)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &result, err
|
||||
}
|
||||
|
||||
func (wd *BrowserDriver) Status() (deviceStatus types.DeviceStatus, err error) {
|
||||
log.Warn().Msg("Status not implemented in ADBDriver")
|
||||
return
|
||||
@@ -658,7 +604,7 @@ func (wd *BrowserDriver) ForegroundInfo() (app types.AppInfo, err error) {
|
||||
|
||||
// PressBack Presses the back button
|
||||
func (wd *BrowserDriver) PressBack(options ...option.ActionOption) error {
|
||||
_, err := wd.HttpPOST(map[string]interface{}{}, wd.sessionId, "ui/back")
|
||||
_, err := wd.Session.POST(nil, wd.concatURL(wd.sessionId, "ui/back"))
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -50,11 +50,17 @@ type IDriver interface {
|
||||
Home() error
|
||||
Unlock() error
|
||||
Back() error
|
||||
// hover
|
||||
HoverBySelector(selector string, opts ...option.ActionOption) error
|
||||
// tap
|
||||
TapXY(x, y float64, opts ...option.ActionOption) error // by percentage or absolute coordinate
|
||||
TapAbsXY(x, y float64, opts ...option.ActionOption) error // by absolute coordinate
|
||||
TapXY(x, y float64, opts ...option.ActionOption) error // by percentage or absolute coordinate
|
||||
TapAbsXY(x, y float64, opts ...option.ActionOption) error // by absolute coordinate
|
||||
TapBySelector(text string, opts ...option.ActionOption) error
|
||||
DoubleTap(x, y float64, opts ...option.ActionOption) error // by absolute coordinate
|
||||
TouchAndHold(x, y float64, opts ...option.ActionOption) error
|
||||
// secondary click
|
||||
SecondaryClick(x, y float64) error
|
||||
SecondaryClickBySelector(selector string, options ...option.ActionOption) error
|
||||
// swipe
|
||||
Drag(fromX, fromY, toX, toY float64, opts ...option.ActionOption) error
|
||||
Swipe(fromX, fromY, toX, toY float64, opts ...option.ActionOption) error // by percentage
|
||||
|
||||
@@ -50,9 +50,9 @@ const (
|
||||
ACTION_AIAction ActionMethod = "ai_action" // action with ai
|
||||
ACTION_TapBySelector ActionMethod = "tap_by_selector"
|
||||
ACTION_HoverBySelector ActionMethod = "hover_by_selector"
|
||||
ACTION_ClosePage ActionMethod = "close_page"
|
||||
ACTION_RightClick ActionMethod = "right_click"
|
||||
ACTION_RightClickBySelector ActionMethod = "right_click_by_selector"
|
||||
ACTION_WebCloseTab ActionMethod = "web_close_tab"
|
||||
ACTION_SecondaryClick ActionMethod = "secondary_click"
|
||||
ACTION_SecondaryClickBySelector ActionMethod = "secondary_click_by_selector"
|
||||
ACTION_GetElementTextBySelector ActionMethod = "get_element_text_by_selector"
|
||||
|
||||
// custom actions
|
||||
@@ -185,40 +185,40 @@ func (dExt *XTDriver) DoAction(action MobileAction) (err error) {
|
||||
return fmt.Errorf("app_terminate params should be bundleId(string), got %v", action.Params)
|
||||
case ACTION_Home:
|
||||
return dExt.Home()
|
||||
case ACTION_RightClick:
|
||||
case ACTION_SecondaryClick:
|
||||
if params, err := builtin.ConvertToFloat64Slice(action.Params); err == nil {
|
||||
if len(params) != 2 {
|
||||
return fmt.Errorf("invalid tap location params: %v", params)
|
||||
}
|
||||
x, y := params[0], params[1]
|
||||
return dExt.IDriver.(*BrowserDriver).RightClick(x, y)
|
||||
return dExt.SecondaryClick(x, y)
|
||||
}
|
||||
return fmt.Errorf("invalid %s params: %v", ACTION_RightClick, action.Params)
|
||||
return fmt.Errorf("invalid %s params: %v", ACTION_SecondaryClick, action.Params)
|
||||
case ACTION_HoverBySelector:
|
||||
if selector, ok := action.Params.(string); ok {
|
||||
return dExt.IDriver.(*BrowserDriver).HoverBySelector(selector, action.GetOptions()...)
|
||||
return dExt.HoverBySelector(selector, action.GetOptions()...)
|
||||
}
|
||||
return fmt.Errorf("invalid %s params: %v", ACTION_HoverBySelector, action.Params)
|
||||
case ACTION_TapBySelector:
|
||||
if selector, ok := action.Params.(string); ok {
|
||||
return dExt.IDriver.(*BrowserDriver).TapBySelector(selector, action.GetOptions()...)
|
||||
return dExt.TapBySelector(selector, action.GetOptions()...)
|
||||
}
|
||||
return fmt.Errorf("invalid %s params: %v", ACTION_TapBySelector, action.Params)
|
||||
case ACTION_RightClickBySelector:
|
||||
case ACTION_SecondaryClickBySelector:
|
||||
if selector, ok := action.Params.(string); ok {
|
||||
return dExt.IDriver.(*BrowserDriver).RightClickBySelector(selector, action.GetOptions()...)
|
||||
return dExt.SecondaryClickBySelector(selector, action.GetOptions()...)
|
||||
}
|
||||
return fmt.Errorf("invalid %s params: %v", ACTION_RightClickBySelector, action.Params)
|
||||
case ACTION_ClosePage:
|
||||
return fmt.Errorf("invalid %s params: %v", ACTION_SecondaryClickBySelector, action.Params)
|
||||
case ACTION_WebCloseTab:
|
||||
if param, ok := action.Params.(json.Number); ok {
|
||||
paramInt64, _ := param.Int64()
|
||||
return dExt.IDriver.(*BrowserDriver).ClosePage(int(paramInt64))
|
||||
return dExt.IDriver.(*BrowserDriver).CloseTab(int(paramInt64))
|
||||
} else if param, ok := action.Params.(int64); ok {
|
||||
return dExt.IDriver.(*BrowserDriver).ClosePage(int(param))
|
||||
return dExt.IDriver.(*BrowserDriver).CloseTab(int(param))
|
||||
} else {
|
||||
return dExt.IDriver.(*BrowserDriver).ClosePage(action.Params.(int))
|
||||
return dExt.IDriver.(*BrowserDriver).CloseTab(action.Params.(int))
|
||||
}
|
||||
// return fmt.Errorf("invalid %s params: %v", ACTION_ClosePage, action.Params)
|
||||
// return fmt.Errorf("invalid %s params: %v", ACTION_WebCloseTab, action.Params)
|
||||
case ACTION_SetIme:
|
||||
if ime, ok := action.Params.(string); ok {
|
||||
err = dExt.SetIme(ime)
|
||||
|
||||
@@ -57,7 +57,7 @@ func (dExt *XTDriver) TapByCV(opts ...option.ActionOption) error {
|
||||
return dExt.TapAbsXY(point.X, point.Y, opts...)
|
||||
}
|
||||
|
||||
func (dExt *XTDriver) RightClickByOCR(ocrText string, opts ...option.ActionOption) error {
|
||||
func (dExt *XTDriver) SecondaryClickByOCR(ocrText string, opts ...option.ActionOption) error {
|
||||
actionOptions := option.NewActionOptions(opts...)
|
||||
point, err := dExt.FindScreenText(ocrText, opts...)
|
||||
if err != nil {
|
||||
@@ -66,5 +66,5 @@ func (dExt *XTDriver) RightClickByOCR(ocrText string, opts ...option.ActionOptio
|
||||
}
|
||||
return err
|
||||
}
|
||||
return dExt.IDriver.(*BrowserDriver).RightClick(point.Center().X, point.Center().Y)
|
||||
return dExt.SecondaryClick(point.Center().X, point.Center().Y)
|
||||
}
|
||||
|
||||
@@ -270,10 +270,6 @@ func (s *DriverSession) Request(method string, urlStr string, rawBody []byte) (
|
||||
}
|
||||
|
||||
func (s *DriverSession) SetupPortForward(localPort int) error {
|
||||
// conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", localPort))
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("create tcp connection error %v", err)
|
||||
// }
|
||||
s.client.Transport = &http.Transport{
|
||||
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
return net.Dial(network, fmt.Sprintf("127.0.0.1:%d", localPort))
|
||||
|
||||
@@ -181,14 +181,18 @@ func (dExt *XTDriver) assertForegroundApp(appName, assert string) error {
|
||||
}
|
||||
|
||||
func (dExt *XTDriver) assertSelector(selector, assert string) error {
|
||||
driver, ok := dExt.IDriver.(*BrowserDriver)
|
||||
if !ok {
|
||||
return errors.New("assert selector only supports browser driver")
|
||||
}
|
||||
switch assert {
|
||||
case AssertionExists:
|
||||
_, err := dExt.IDriver.(*BrowserDriver).IsElementExistBySelector(selector)
|
||||
_, err := driver.IsElementExistBySelector(selector)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "assert ocr exists failed")
|
||||
}
|
||||
case AssertionNotExists:
|
||||
_, err := dExt.IDriver.(*BrowserDriver).IsElementExistBySelector(selector)
|
||||
_, err := driver.IsElementExistBySelector(selector)
|
||||
if err == nil {
|
||||
return errors.New("assert ocr not exists failed")
|
||||
}
|
||||
|
||||
@@ -315,3 +315,19 @@ func (hd *HDCDriver) ClearFiles(paths ...string) error {
|
||||
log.Warn().Msg("ClearFiles not implemented in HDCDriver")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (hd *HDCDriver) HoverBySelector(selector string, options ...option.ActionOption) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
func (hd *HDCDriver) TapBySelector(text string, opts ...option.ActionOption) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (hd *HDCDriver) SecondaryClick(x, y float64) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
func (hd *HDCDriver) SecondaryClickBySelector(selector string, options ...option.ActionOption) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1053,3 +1053,19 @@ func (wd *WDADriver) StopCaptureLog() (result interface{}, err error) {
|
||||
func (wd *WDADriver) GetSession() *DriverSession {
|
||||
return wd.Session
|
||||
}
|
||||
|
||||
func (wd *WDADriver) HoverBySelector(selector string, options ...option.ActionOption) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
func (wd *WDADriver) TapBySelector(text string, opts ...option.ActionOption) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (wd *WDADriver) SecondaryClick(x, y float64) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
func (wd *WDADriver) SecondaryClickBySelector(selector string, options ...option.ActionOption) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user