From 2b06e4a2809349882dce21986874af1fb456530d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E8=81=AA?= Date: Wed, 7 May 2025 21:39:22 +0800 Subject: [PATCH] update: fucntion call --- internal/version/VERSION | 2 +- server/ui.go | 2 +- step_ui.go | 4 +-- uixt/android_driver_adb.go | 30 ++++++++++++------ uixt/browser_driver.go | 64 +++----------------------------------- uixt/driver.go | 10 ++++-- uixt/driver_action.go | 32 +++++++++---------- uixt/driver_ext_tap.go | 4 +-- uixt/driver_session.go | 4 --- uixt/driver_utils.go | 8 +++-- uixt/harmony_driver_hdc.go | 16 ++++++++++ uixt/ios_driver_wda.go | 16 ++++++++++ 12 files changed, 94 insertions(+), 98 deletions(-) diff --git a/internal/version/VERSION b/internal/version/VERSION index 1c967846..f7be7d64 100644 --- a/internal/version/VERSION +++ b/internal/version/VERSION @@ -1 +1 @@ -v5.0.0-beta-2505072021 +v5.0.0-beta-2505072139 diff --git a/server/ui.go b/server/ui.go index 80cf4199..186180e3 100644 --- a/server/ui.go +++ b/server/ui.go @@ -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 diff --git a/step_ui.go b/step_ui.go index e2ec27f2..e575c2bb 100644 --- a/step_ui.go +++ b/step_ui.go @@ -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...), } diff --git a/uixt/android_driver_adb.go b/uixt/android_driver_adb.go index 725784f5..e7711ecf 100644 --- a/uixt/android_driver_adb.go +++ b/uixt/android_driver_adb.go @@ -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 +} diff --git a/uixt/browser_driver.go b/uixt/browser_driver.go index 39a05a08..6499a3fe 100644 --- a/uixt/browser_driver.go +++ b/uixt/browser_driver.go @@ -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 } diff --git a/uixt/driver.go b/uixt/driver.go index fbecc92a..72d5ab06 100644 --- a/uixt/driver.go +++ b/uixt/driver.go @@ -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 diff --git a/uixt/driver_action.go b/uixt/driver_action.go index 30d4050c..d23f81e8 100644 --- a/uixt/driver_action.go +++ b/uixt/driver_action.go @@ -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) diff --git a/uixt/driver_ext_tap.go b/uixt/driver_ext_tap.go index 4ff2d044..2458a3a8 100644 --- a/uixt/driver_ext_tap.go +++ b/uixt/driver_ext_tap.go @@ -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) } diff --git a/uixt/driver_session.go b/uixt/driver_session.go index 9bbebcd8..693a4fe3 100644 --- a/uixt/driver_session.go +++ b/uixt/driver_session.go @@ -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)) diff --git a/uixt/driver_utils.go b/uixt/driver_utils.go index 65c30ec1..162b6d97 100644 --- a/uixt/driver_utils.go +++ b/uixt/driver_utils.go @@ -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") } diff --git a/uixt/harmony_driver_hdc.go b/uixt/harmony_driver_hdc.go index 1a6a8644..0865e365 100644 --- a/uixt/harmony_driver_hdc.go +++ b/uixt/harmony_driver_hdc.go @@ -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 +} diff --git a/uixt/ios_driver_wda.go b/uixt/ios_driver_wda.go index c8912474..889c95c0 100644 --- a/uixt/ios_driver_wda.go +++ b/uixt/ios_driver_wda.go @@ -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 +}