From 72f7b77bc81e8b9861dd0f2115f6416b5ae4c77e Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Tue, 11 Feb 2025 15:34:51 +0800 Subject: [PATCH] refactor: XTDriver --- examples/uitest/bili/android/cli.go | 6 ++-- examples/uitest/bili/ios/cli.go | 12 +++---- examples/worldcup/main.go | 6 ++-- internal/version/VERSION | 2 +- pkg/uixt/android_driver_adb.go | 6 ++-- pkg/uixt/android_driver_uia2.go | 4 +-- pkg/uixt/android_test.go | 34 +++++++++---------- pkg/uixt/demo/main_test.go | 2 +- pkg/uixt/driver.go | 18 ++++------ pkg/uixt/driver_action.go | 18 +++++----- .../driver_ext/shoots_android_driver_test.go | 4 +-- pkg/uixt/driver_ext/shoots_ios_driver.go | 12 +++---- pkg/uixt/driver_input.go | 15 -------- pkg/uixt/driver_install.go | 8 ++--- pkg/uixt/driver_screenshot.go | 6 ++-- pkg/uixt/driver_swipe.go | 6 ++-- pkg/uixt/driver_swipe_test.go | 2 +- pkg/uixt/driver_tap.go | 10 +++--- pkg/uixt/driver_test.go | 2 +- pkg/uixt/harmony_driver_hdc.go | 4 +-- pkg/uixt/ios_driver_wda.go | 6 ++-- pkg/uixt/ios_test.go | 6 ++-- runner.go | 6 ++-- server/app.go | 8 ++--- server/key.go | 6 ++-- server/source.go | 4 +-- server/ui.go | 4 +-- step_mobile_ui.go | 4 +-- 28 files changed, 100 insertions(+), 121 deletions(-) delete mode 100644 pkg/uixt/driver_input.go diff --git a/examples/uitest/bili/android/cli.go b/examples/uitest/bili/android/cli.go index a4075321..67d9c112 100644 --- a/examples/uitest/bili/android/cli.go +++ b/examples/uitest/bili/android/cli.go @@ -98,7 +98,7 @@ func watchVideo(driver *uixt.XTDriver) (err error) { if err != nil { // 未找到横屏图标,该页面可能不是横版视频(直播|广告|Feed) // 退出回到推荐页 - driver.GetDriver().PressBack() + driver.PressBack() return nil } @@ -106,7 +106,7 @@ func watchVideo(driver *uixt.XTDriver) (err error) { time.Sleep(10 * time.Second) // 返回视频页面 - err = driver.GetDriver().PressBack() + err = driver.PressBack() if err != nil { return err } @@ -114,7 +114,7 @@ func watchVideo(driver *uixt.XTDriver) (err error) { time.Sleep(1 * time.Second) // 返回推荐页 - err = driver.GetDriver().PressBack() + err = driver.PressBack() if err != nil { return err } diff --git a/examples/uitest/bili/ios/cli.go b/examples/uitest/bili/ios/cli.go index 2992bd26..82937613 100644 --- a/examples/uitest/bili/ios/cli.go +++ b/examples/uitest/bili/ios/cli.go @@ -36,17 +36,17 @@ func launchAppDriver(pkgName string) (driverExt *uixt.XTDriver, err error) { return nil, err } - //_, err = driver.Driver.AppTerminate(pkgName) + //_, err = driver.AppTerminate(pkgName) //if err != nil { // return nil, err //} // - //err = driver.Driver.Homescreen() + //err = driver.Homescreen() //if err != nil { // return nil, err //} // - //err = driver.Driver.AppLaunch(pkgName) + //err = driver.AppLaunch(pkgName) //if err != nil { // return nil, err //} @@ -100,7 +100,7 @@ func watchVideo(driver *uixt.XTDriver) (err error) { if err != nil { // 未找到横屏图标,该页面可能不是横版视频(直播|广告|Feed) // 退出回到推荐页 - driver.GetDriver().PressBack() + driver.PressBack() return nil } @@ -108,7 +108,7 @@ func watchVideo(driver *uixt.XTDriver) (err error) { time.Sleep(10 * time.Second) // 返回视频页面 - err = driver.GetDriver().PressBack() + err = driver.PressBack() if err != nil { return err } @@ -116,7 +116,7 @@ func watchVideo(driver *uixt.XTDriver) (err error) { time.Sleep(1 * time.Second) // 返回推荐页 - err = driver.GetDriver().PressBack() + err = driver.PressBack() if err != nil { return err } diff --git a/examples/worldcup/main.go b/examples/worldcup/main.go index 30a38724..16e28be4 100644 --- a/examples/worldcup/main.go +++ b/examples/worldcup/main.go @@ -102,7 +102,7 @@ func NewWorldCupLive(driver *uixt.XTDriver, matchName, bundleID string, duration log.Fatal().Err(err).Msg("failed to open file") } // write title - f.WriteString(fmt.Sprintf("%s\t%s\t%s\n", matchName, driver.GetDriver().GetDevice().UUID(), bundleID)) + f.WriteString(fmt.Sprintf("%s\t%s\t%s\n", matchName, driver.GetDevice().UUID(), bundleID)) f.WriteString("utc_time\tutc_timestamp\tlive_time\tlive_seconds\n") if interval == 0 { @@ -174,13 +174,13 @@ func (wc *WorldCupLive) EnterLive(bundleID string) error { log.Info().Msg("enter world cup live") // kill app - _, err := wc.driver.GetDriver().AppTerminate(bundleID) + _, err := wc.driver.AppTerminate(bundleID) if err != nil { log.Error().Err(err).Msg("terminate app failed") } // launch app - err = wc.driver.GetDriver().AppLaunch(bundleID) + err = wc.driver.AppLaunch(bundleID) if err != nil { log.Error().Err(err).Msg("launch app failed") return err diff --git a/internal/version/VERSION b/internal/version/VERSION index 1f9d25aa..6ed433db 100644 --- a/internal/version/VERSION +++ b/internal/version/VERSION @@ -1 +1 @@ -v5.0.0+2502111501 +v5.0.0+2502111534 diff --git a/pkg/uixt/android_driver_adb.go b/pkg/uixt/android_driver_adb.go index 28529141..b6bcec9d 100644 --- a/pkg/uixt/android_driver_adb.go +++ b/pkg/uixt/android_driver_adb.go @@ -296,7 +296,7 @@ func (ad *ADBDriver) AppTerminate(packageName string) (successful bool, err erro return true, nil } -func (ad *ADBDriver) Tap(x, y float64, opts ...option.ActionOption) error { +func (ad *ADBDriver) TapXY(x, y float64, opts ...option.ActionOption) error { actionOptions := option.NewActionOptions(opts...) if len(actionOptions.Offset) == 2 { @@ -317,7 +317,7 @@ func (ad *ADBDriver) Tap(x, y float64, opts ...option.ActionOption) error { return nil } -func (ad *ADBDriver) DoubleTap(x, y float64, opts ...option.ActionOption) error { +func (ad *ADBDriver) DoubleTapXY(x, y float64, opts ...option.ActionOption) error { // adb shell input tap x y xStr := fmt.Sprintf("%.1f", x) yStr := fmt.Sprintf("%.1f", y) @@ -608,7 +608,7 @@ func (ad *ADBDriver) tapByTextUsingHierarchy(hierarchy *Hierarchy, text string, } for _, bound := range bounds { width, height := bound.Center() - err := ad.Tap(width, height, opts...) + err := ad.TapXY(width, height, opts...) if err != nil { return err } diff --git a/pkg/uixt/android_driver_uia2.go b/pkg/uixt/android_driver_uia2.go index 06c7e283..2c00f94b 100644 --- a/pkg/uixt/android_driver_uia2.go +++ b/pkg/uixt/android_driver_uia2.go @@ -245,7 +245,7 @@ func (ud *UIA2Driver) Orientation() (orientation types.Orientation, err error) { return } -func (ud *UIA2Driver) DoubleTap(x, y float64, opts ...option.ActionOption) error { +func (ud *UIA2Driver) DoubleTapXY(x, y float64, opts ...option.ActionOption) error { return ud.DoubleFloatTap(x, y) } @@ -271,7 +271,7 @@ func (ud *UIA2Driver) DoubleFloatTap(x, y float64) error { return err } -func (ud *UIA2Driver) Tap(x, y float64, opts ...option.ActionOption) (err error) { +func (ud *UIA2Driver) TapXY(x, y float64, opts ...option.ActionOption) (err error) { // register(postHandler, new Tap("/wd/hub/session/:sessionId/appium/tap")) actionOptions := option.NewActionOptions(opts...) diff --git a/pkg/uixt/android_test.go b/pkg/uixt/android_test.go index c641a932..376d9a2d 100644 --- a/pkg/uixt/android_test.go +++ b/pkg/uixt/android_test.go @@ -97,7 +97,7 @@ func TestDriver_DeviceSize(t *testing.T) { func TestDriver_Source(t *testing.T) { setupAndroidUIA2Driver(t) - source, err := driverExt.GetDriver().Source() + source, err := driverExt.Source() if err != nil { t.Fatal(err) } @@ -134,7 +134,7 @@ func TestDriver_DeviceInfo(t *testing.T) { func TestDriver_Tap(t *testing.T) { setupAndroidUIA2Driver(t) - driverExt.GetDriver().StartCaptureLog("") + driverExt.StartCaptureLog("") err := driverExt.TapXY(0.5, 0.5, option.WithIdentifier("test"), option.WithPressDuration(4)) @@ -143,18 +143,18 @@ func TestDriver_Tap(t *testing.T) { } //time.Sleep(time.Second) // - //err = driverExt.GetDriver().Tap(60.5, 125.5, WithIdentifier("test")) + //err = driverExt.Tap(60.5, 125.5, WithIdentifier("test")) //if err != nil { // t.Fatal(err) //} //time.Sleep(time.Second) - //result, _ := driverExt.GetDriver().StopCaptureLog() + //result, _ := driverExt.StopCaptureLog() //t.Log(result) } func TestDriver_Swipe(t *testing.T) { setupAndroidUIA2Driver(t) - err := driverExt.GetDriver().Swipe(400, 1000, 400, 500, + err := driverExt.Swipe(400, 1000, 400, 500, option.WithPressDuration(0.5)) if err != nil { t.Fatal(err) @@ -186,7 +186,7 @@ func TestDriver_Drag(t *testing.T) { func TestDriver_SendKeys(t *testing.T) { setupAndroidUIA2Driver(t) - err := driverExt.GetDriver().SendKeys("辽宁省沈阳市新民市民族街36-4", + err := driverExt.SendKeys("辽宁省沈阳市新民市民族街36-4", option.WithIdentifier("test")) if err != nil { t.Fatal(err) @@ -224,10 +224,10 @@ func TestDriver_SetRotation(t *testing.T) { func TestDriver_GetOrientation(t *testing.T) { setupAndroidUIA2Driver(t) - _, _ = driverExt.GetDriver().AppTerminate("com.quark.browser") - _ = driverExt.GetDriver().AppLaunch("com.quark.browser") + _, _ = driverExt.AppTerminate("com.quark.browser") + _ = driverExt.AppLaunch("com.quark.browser") time.Sleep(2 * time.Second) - _ = driverExt.GetDriver().Homescreen() + _ = driverExt.Homescreen() } func Test_getFreePort(t *testing.T) { @@ -262,10 +262,10 @@ func TestDriver_IsAppInForeground(t *testing.T) { setupAndroidUIA2Driver(t) // setupAndroidAdbDriver(t) - err := driverExt.GetDriver().AppLaunch("com.android.settings") + err := driverExt.AppLaunch("com.android.settings") checkErr(t, err) - app, err := driverExt.GetDriver().GetForegroundApp() + app, err := driverExt.GetForegroundApp() checkErr(t, err) if app.PackageName != "com.android.settings" { t.FailNow() @@ -274,18 +274,18 @@ func TestDriver_IsAppInForeground(t *testing.T) { t.FailNow() } - err = driverExt.GetDriver().AssertForegroundApp("com.android.settings") + err = driverExt.AssertForegroundApp("com.android.settings") if err != nil { t.Fatal(err) } time.Sleep(2 * time.Second) - _, err = driverExt.GetDriver().AppTerminate("com.android.settings") + _, err = driverExt.AppTerminate("com.android.settings") if err != nil { t.Fatal(err) } - err = driverExt.GetDriver().AssertForegroundApp("com.android.settings") + err = driverExt.AssertForegroundApp("com.android.settings") if err == nil { t.Fatal(err) } @@ -385,7 +385,7 @@ func TestTapTexts(t *testing.T) { }, }, } - err := driverExt.GetDriver().TapByTexts(actions...) + err := driverExt.TapByTexts(actions...) if err != nil { t.Fatal(err) } @@ -393,7 +393,7 @@ func TestTapTexts(t *testing.T) { func TestRecordVideo(t *testing.T) { setupAndroidAdbDriver(t) - path, err := driverExt.GetDriver().(*ADBDriver).RecordScreen("", 5*time.Second) + path, err := driverExt.RecordScreen("", 5*time.Second) if err != nil { t.Fatal(err) } @@ -403,7 +403,7 @@ func TestRecordVideo(t *testing.T) { func Test_Android_Backspace(t *testing.T) { setupAndroidAdbDriver(t) - err := driverExt.GetDriver().Backspace(1) + err := driverExt.Backspace(1) if err != nil { t.Fatal(err) } diff --git a/pkg/uixt/demo/main_test.go b/pkg/uixt/demo/main_test.go index f4024dce..993bad79 100644 --- a/pkg/uixt/demo/main_test.go +++ b/pkg/uixt/demo/main_test.go @@ -33,7 +33,7 @@ func TestIOSDemo(t *testing.T) { // release session defer func() { - driverExt.GetDriver().DeleteSession() + driverExt.DeleteSession() }() // 持续监测手机屏幕,直到出现青少年模式弹窗后,点击「我知道了」 diff --git a/pkg/uixt/driver.go b/pkg/uixt/driver.go index 8e2c8632..626516af 100644 --- a/pkg/uixt/driver.go +++ b/pkg/uixt/driver.go @@ -49,8 +49,8 @@ type IDriver interface { Homescreen() error Unlock() (err error) // tap - Tap(x, y float64, opts ...option.ActionOption) error - DoubleTap(x, y float64, opts ...option.ActionOption) error + TapXY(x, y float64, opts ...option.ActionOption) error + DoubleTapXY(x, y float64, opts ...option.ActionOption) error TapByText(text string, opts ...option.ActionOption) error // TODO: remove TapByTexts(actions ...TapTextAction) error // TODO: remove // swipe @@ -93,7 +93,7 @@ type IDriver interface { func NewXTDriver(driver IDriver, opts ...ai.AIServiceOption) *XTDriver { services := ai.NewAIService(opts...) driverExt := &XTDriver{ - Driver: driver, + IDriver: driver, CVService: services.ICVService, LLMService: services.ILLMService, } @@ -104,8 +104,6 @@ var _ IDriverExt = (*XTDriver)(nil) // XTDriver = IDriver + AI type IDriverExt interface { - GetDriver() IDriver // get original driver - GetScreenResult(opts ...option.ActionOption) (screenResult *ScreenResult, err error) GetScreenTexts(opts ...option.ActionOption) (ocrTexts ai.OCRTexts, err error) GetScreenShot(fileName string) (raw *bytes.Buffer, path string, err error) @@ -134,18 +132,14 @@ type IDriverExt interface { } type XTDriver struct { - Driver IDriver + IDriver CVService ai.ICVService // OCR/CV LLMService ai.ILLMService // LLM } -func (dExt *XTDriver) GetDriver() IDriver { - return dExt.Driver -} - func (dExt *XTDriver) Setup() error { // unlock device screen - err := dExt.Driver.Unlock() + err := dExt.Unlock() if err != nil { log.Error().Err(err).Msg("unlock device screen failed") return err @@ -188,7 +182,7 @@ func (dExt *XTDriver) assertOCR(text, assert string) error { } func (dExt *XTDriver) assertForegroundApp(appName, assert string) (err error) { - err = dExt.Driver.AssertForegroundApp(appName) + err = dExt.AssertForegroundApp(appName) switch assert { case AssertionEqual: if err != nil { diff --git a/pkg/uixt/driver_action.go b/pkg/uixt/driver_action.go index ee646ec1..ba5e7312 100644 --- a/pkg/uixt/driver_action.go +++ b/pkg/uixt/driver_action.go @@ -113,7 +113,7 @@ func (dExt *XTDriver) ParseActionOptions(opts ...option.ActionOption) []option.A func (dExt *XTDriver) GenAbsScope(x1, y1, x2, y2 float64) option.AbsScope { // convert relative scope to absolute scope - windowSize, _ := dExt.Driver.WindowSize() + windowSize, _ := dExt.WindowSize() absX1 := int(x1 * float64(windowSize.Width)) absY1 := int(y1 * float64(windowSize.Height)) absX2 := int(x2 * float64(windowSize.Width)) @@ -153,13 +153,13 @@ func (dExt *XTDriver) DoAction(action MobileAction) (err error) { } case ACTION_AppClear: if packageName, ok := action.Params.(string); ok { - if err = dExt.Driver.AppClear(packageName); err != nil { + if err = dExt.AppClear(packageName); err != nil { return errors.Wrap(err, "failed to clear app") } } case ACTION_AppLaunch: if bundleId, ok := action.Params.(string); ok { - return dExt.Driver.AppLaunch(bundleId) + return dExt.AppLaunch(bundleId) } return fmt.Errorf("invalid %s params, should be bundleId(string), got %v", ACTION_AppLaunch, action.Params) @@ -185,7 +185,7 @@ func (dExt *XTDriver) DoAction(action MobileAction) (err error) { return fmt.Errorf("invalid %s params: %v", ACTION_SwipeToTapTexts, action.Params) case ACTION_AppTerminate: if bundleId, ok := action.Params.(string); ok { - success, err := dExt.Driver.AppTerminate(bundleId) + success, err := dExt.AppTerminate(bundleId) if err != nil { return errors.Wrap(err, "failed to terminate app") } @@ -196,10 +196,10 @@ 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.Driver.Homescreen() + return dExt.Homescreen() case ACTION_SetIme: if ime, ok := action.Params.(string); ok { - err = dExt.Driver.SetIme(ime) + err = dExt.SetIme(ime) if err != nil { return errors.Wrap(err, "failed to set ime") } @@ -208,7 +208,7 @@ func (dExt *XTDriver) DoAction(action MobileAction) (err error) { case ACTION_GetSource: if packageName, ok := action.Params.(string); ok { source := option.NewSourceOption().WithProcessName(packageName) - _, err = dExt.Driver.Source(source) + _, err = dExt.Source(source) if err != nil { return errors.Wrap(err, "failed to set ime") } @@ -274,9 +274,9 @@ func (dExt *XTDriver) DoAction(action MobileAction) (err error) { // append \n to send text with enter // send \b\b\b to delete 3 chars param := fmt.Sprintf("%v", action.Params) - return dExt.Driver.Input(param, action.GetOptions()...) + return dExt.Input(param) case ACTION_Back: - return dExt.Driver.PressBack() + return dExt.PressBack() case ACTION_Sleep: if param, ok := action.Params.(json.Number); ok { seconds, _ := param.Float64() diff --git a/pkg/uixt/driver_ext/shoots_android_driver_test.go b/pkg/uixt/driver_ext/shoots_android_driver_test.go index 8295d8d8..42ea77d5 100644 --- a/pkg/uixt/driver_ext/shoots_android_driver_test.go +++ b/pkg/uixt/driver_ext/shoots_android_driver_test.go @@ -73,7 +73,7 @@ func TestSwipe(t *testing.T) { func TestTap(t *testing.T) { setupShootsAndroidDriver(t) - err := shootsAndroidDriver.Tap(900, 400) + err := shootsAndroidDriver.TapXY(900, 400) if err != nil { t.Fatal(err) } @@ -81,7 +81,7 @@ func TestTap(t *testing.T) { func TestDoubleTap(t *testing.T) { setupShootsAndroidDriver(t) - err := shootsAndroidDriver.DoubleTap(500, 500) + err := shootsAndroidDriver.DoubleTapXY(500, 500) if err != nil { t.Fatal(err) } diff --git a/pkg/uixt/driver_ext/shoots_ios_driver.go b/pkg/uixt/driver_ext/shoots_ios_driver.go index 11a1d7ae..463fa566 100644 --- a/pkg/uixt/driver_ext/shoots_ios_driver.go +++ b/pkg/uixt/driver_ext/shoots_ios_driver.go @@ -200,22 +200,22 @@ func (s *ShootsIOSDriver) Orientation() (orientation types.Orientation, err erro return s.WDADriver.Orientation() } -// Tap Sends a tap event at the coordinate. -func (s *ShootsIOSDriver) Tap(x, y float64, opts ...option.ActionOption) error { +// TapXY Sends a tap event at the coordinate. +func (s *ShootsIOSDriver) TapXY(x, y float64, opts ...option.ActionOption) error { err := s.setUpWda() if err != nil { return err } - return s.WDADriver.Tap(x, y, opts...) + return s.WDADriver.TapXY(x, y, opts...) } -// DoubleTap Sends a double tap event at the coordinate. -func (s *ShootsIOSDriver) DoubleTap(x, y float64, opts ...option.ActionOption) error { +// DoubleTapXY Sends a double tap event at the coordinate. +func (s *ShootsIOSDriver) DoubleTapXY(x, y float64, opts ...option.ActionOption) error { err := s.setUpWda() if err != nil { return err } - return s.WDADriver.DoubleTap(x, y, opts...) + return s.WDADriver.DoubleTapXY(x, y, opts...) } // TouchAndHold Initiates a long-press gesture at the coordinate, holding for the specified duration. diff --git a/pkg/uixt/driver_input.go b/pkg/uixt/driver_input.go deleted file mode 100644 index c015552c..00000000 --- a/pkg/uixt/driver_input.go +++ /dev/null @@ -1,15 +0,0 @@ -package uixt - -import ( - "github.com/pkg/errors" - - "github.com/httprunner/httprunner/v5/code" -) - -func (dExt *XTDriver) Input(text string) (err error) { - err = dExt.Driver.Input(text) - if err != nil { - return errors.Wrap(code.MobileUIInputError, err.Error()) - } - return nil -} diff --git a/pkg/uixt/driver_install.go b/pkg/uixt/driver_install.go index d078c410..26820df0 100644 --- a/pkg/uixt/driver_install.go +++ b/pkg/uixt/driver_install.go @@ -42,7 +42,7 @@ func (dExt *XTDriver) InstallByUrl(url string, opts ...option.InstallOption) err } func (dExt *XTDriver) Install(filePath string, opts ...option.InstallOption) error { - if _, ok := dExt.Driver.GetDevice().(*AndroidDevice); ok { + if _, ok := dExt.GetDevice().(*AndroidDevice); ok { stopChan := make(chan struct{}) go func() { ticker := time.NewTicker(5 * time.Second) @@ -69,7 +69,7 @@ func (dExt *XTDriver) Install(filePath string, opts ...option.InstallOption) err }, }, } - _ = dExt.Driver.TapByTexts(actions...) + _ = dExt.TapByTexts(actions...) _ = dExt.TapByOCR( "^(.*无视风险安装|确定|继续|完成|点击继续安装|继续安装旧版本|替换|授权本次安装|稍后提醒|继续安装|重新安装|安装)$", @@ -87,12 +87,12 @@ func (dExt *XTDriver) Install(filePath string, opts ...option.InstallOption) err }() } - return dExt.Driver.GetDevice().Install(filePath, opts...) + return dExt.GetDevice().Install(filePath, opts...) } func (dExt *XTDriver) Uninstall(packageName string, opts ...option.ActionOption) error { actionOptions := option.NewActionOptions(opts...) - err := dExt.Driver.GetDevice().Uninstall(packageName) + err := dExt.GetDevice().Uninstall(packageName) if err != nil { log.Warn().Err(err).Msg("failed to uninstall") } diff --git a/pkg/uixt/driver_screenshot.go b/pkg/uixt/driver_screenshot.go index e37cdaea..bcce10c0 100644 --- a/pkg/uixt/driver_screenshot.go +++ b/pkg/uixt/driver_screenshot.go @@ -74,7 +74,7 @@ func (dExt *XTDriver) GetScreenResult(opts ...option.ActionOption) (screenResult continue } - windowSize, err = dExt.Driver.WindowSize() + windowSize, err = dExt.WindowSize() if err != nil { lastErr = errors.Wrap(code.DeviceGetInfoError, err.Error()) continue @@ -101,7 +101,7 @@ func (dExt *XTDriver) GetScreenResult(opts ...option.ActionOption) (screenResult } // cache screen result - dExt.Driver.GetSession().addScreenResult(screenResult) + dExt.GetSession().addScreenResult(screenResult) if imageResult != nil { screenResult.Texts = imageResult.OCRResult.ToOCRTexts() @@ -200,7 +200,7 @@ func (dExt *XTDriver) FindUIResult(opts ...option.ActionOption) (point ai.PointF // GetScreenShot takes screenshot and saves image file to $CWD/screenshots/ folder func (dExt *XTDriver) GetScreenShot(fileName string) (raw *bytes.Buffer, path string, err error) { - if raw, err = dExt.Driver.Screenshot(); err != nil { + if raw, err = dExt.Screenshot(); err != nil { log.Error().Err(err).Msg("capture screenshot data failed") return nil, "", errors.Wrap(code.DeviceScreenShotError, err.Error()) } diff --git a/pkg/uixt/driver_swipe.go b/pkg/uixt/driver_swipe.go index 28779f56..debcd15f 100644 --- a/pkg/uixt/driver_swipe.go +++ b/pkg/uixt/driver_swipe.go @@ -16,12 +16,12 @@ import ( // SwipeRelative swipe from relative position [fromX, fromY] to relative position [toX, toY] func (dExt *XTDriver) SwipeRelative(fromX, fromY, toX, toY float64, opts ...option.ActionOption) error { - absFromX, absFromY, absToX, absToY, err := convertToAbsoluteCoordinates(dExt.Driver, fromX, fromY, toX, toY) + absFromX, absFromY, absToX, absToY, err := convertToAbsoluteCoordinates(dExt.IDriver, fromX, fromY, toX, toY) if err != nil { return err } - err = dExt.Driver.Swipe(absFromX, absFromY, absToX, absToY, opts...) + err = dExt.Swipe(absFromX, absFromY, absToX, absToY, opts...) if err != nil { return errors.Wrap(code.MobileUISwipeError, err.Error()) } @@ -166,7 +166,7 @@ func (dExt *XTDriver) swipeToTapTexts(texts []string, opts ...option.ActionOptio func (dExt *XTDriver) SwipeToTapApp(appName string, opts ...option.ActionOption) error { // go to home screen - if err := dExt.Driver.Homescreen(); err != nil { + if err := dExt.Homescreen(); err != nil { return errors.Wrap(err, "go to home screen failed") } diff --git a/pkg/uixt/driver_swipe_test.go b/pkg/uixt/driver_swipe_test.go index ac686b44..97529832 100644 --- a/pkg/uixt/driver_swipe_test.go +++ b/pkg/uixt/driver_swipe_test.go @@ -30,7 +30,7 @@ func TestAndroidSwipeToTapApp(t *testing.T) { func TestAndroidSwipeToTapTexts(t *testing.T) { setupAndroidAdbDriver(t) - err := driverExt.GetDriver().AppLaunch("com.ss.android.ugc.aweme") + err := driverExt.AppLaunch("com.ss.android.ugc.aweme") checkErr(t, err) err = driverExt.swipeToTapTexts([]string{"点击进入直播间", "直播中"}, option.WithDirection("up")) diff --git a/pkg/uixt/driver_tap.go b/pkg/uixt/driver_tap.go index 1a726886..6b5c4c48 100644 --- a/pkg/uixt/driver_tap.go +++ b/pkg/uixt/driver_tap.go @@ -11,7 +11,7 @@ import ( func (dExt *XTDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error { // tap on absolute coordinate [x, y] - err := dExt.Driver.Tap(x, y, opts...) + err := dExt.TapXY(x, y, opts...) if err != nil { return errors.Wrap(code.MobileUITapError, err.Error()) } @@ -24,7 +24,7 @@ func (dExt *XTDriver) TapXY(x, y float64, opts ...option.ActionOption) error { return fmt.Errorf("x, y percentage should be <= 1, got x=%v, y=%v", x, y) } - windowSize, err := dExt.Driver.WindowSize() + windowSize, err := dExt.WindowSize() if err != nil { return err } @@ -88,13 +88,13 @@ func (dExt *XTDriver) DoubleTapXY(x, y float64, opts ...option.ActionOption) err return fmt.Errorf("x, y percentage should be < 1, got x=%v, y=%v", x, y) } - windowSize, err := dExt.Driver.WindowSize() + windowSize, err := dExt.WindowSize() if err != nil { return err } x = x * float64(windowSize.Width) y = y * float64(windowSize.Height) - err = dExt.Driver.DoubleTap(x, y, opts...) + err = dExt.DoubleTapXY(x, y, opts...) if err != nil { return errors.Wrap(code.MobileUITapError, err.Error()) } @@ -111,7 +111,7 @@ func (dExt *XTDriver) DoubleTapOffset(param string, xOffset, yOffset float64, op return err } - err = dExt.Driver.DoubleTap(point.X+xOffset, point.Y+yOffset, opts...) + err = dExt.DoubleTapXY(point.X+xOffset, point.Y+yOffset, opts...) if err != nil { return errors.Wrap(code.MobileUITapError, err.Error()) } diff --git a/pkg/uixt/driver_test.go b/pkg/uixt/driver_test.go index 54366a5a..ce2361a1 100644 --- a/pkg/uixt/driver_test.go +++ b/pkg/uixt/driver_test.go @@ -22,7 +22,7 @@ func TestNewDriverExt(t *testing.T) { t.Log(texts) // get original dirver - driver = driverExt.GetDriver().(*ADBDriver) + driver = driverExt.IDriver.(*ADBDriver) // get device device = driver.GetDevice().(*AndroidDevice) diff --git a/pkg/uixt/harmony_driver_hdc.go b/pkg/uixt/harmony_driver_hdc.go index 9a6c08ef..23dfd334 100644 --- a/pkg/uixt/harmony_driver_hdc.go +++ b/pkg/uixt/harmony_driver_hdc.go @@ -151,7 +151,7 @@ func (hd *HDCDriver) Orientation() (orientation types.Orientation, err error) { return types.OrientationPortrait, nil } -func (hd *HDCDriver) Tap(x, y float64, opts ...option.ActionOption) error { +func (hd *HDCDriver) TapXY(x, y float64, opts ...option.ActionOption) error { actionOptions := option.NewActionOptions(opts...) if len(actionOptions.Offset) == 2 { @@ -168,7 +168,7 @@ func (hd *HDCDriver) Tap(x, y float64, opts ...option.ActionOption) error { return hd.uiDriver.InjectGesture(ghdc.NewGesture().Start(ghdc.Point{X: int(x), Y: int(y)}).Pause(100)) } -func (hd *HDCDriver) DoubleTap(x, y float64, opts ...option.ActionOption) error { +func (hd *HDCDriver) DoubleTapXY(x, y float64, opts ...option.ActionOption) error { return types.ErrDriverNotImplemented } diff --git a/pkg/uixt/ios_driver_wda.go b/pkg/uixt/ios_driver_wda.go index ceaa6d95..02641948 100644 --- a/pkg/uixt/ios_driver_wda.go +++ b/pkg/uixt/ios_driver_wda.go @@ -520,7 +520,7 @@ func (wd *WDADriver) AssertForegroundApp(bundleId string, viewControllerType ... return nil } -func (wd *WDADriver) Tap(x, y float64, opts ...option.ActionOption) (err error) { +func (wd *WDADriver) TapXY(x, y float64, opts ...option.ActionOption) (err error) { // [[FBRoute POST:@"/wda/tap/:uuid"] respondWithTarget:self action:@selector(handleTap:)] actionOptions := option.NewActionOptions(opts...) @@ -544,7 +544,7 @@ func (wd *WDADriver) Tap(x, y float64, opts ...option.ActionOption) (err error) return } -func (wd *WDADriver) DoubleTap(x, y float64, opts ...option.ActionOption) (err error) { +func (wd *WDADriver) DoubleTapXY(x, y float64, opts ...option.ActionOption) (err error) { // [[FBRoute POST:@"/wda/doubleTap"] respondWithTarget:self action:@selector(handleDoubleTapCoordinate:)] actionOptions := option.NewActionOptions(opts...) x = wd.toScale(x) @@ -569,7 +569,7 @@ func (wd *WDADriver) TouchAndHold(x, y float64, opts ...option.ActionOption) (er if actionOptions.Duration == 0 { opts = append(opts, option.WithDuration(1)) } - return wd.Tap(x, y, opts...) + return wd.TapXY(x, y, opts...) } func (wd *WDADriver) Drag(fromX, fromY, toX, toY float64, opts ...option.ActionOption) (err error) { diff --git a/pkg/uixt/ios_test.go b/pkg/uixt/ios_test.go index 1544daba..abc4a262 100644 --- a/pkg/uixt/ios_test.go +++ b/pkg/uixt/ios_test.go @@ -44,7 +44,7 @@ func TestViaUSB(t *testing.T) { func TestInstall(t *testing.T) { setup(t) - err := iOSDriverExt.GetDriver().GetDevice().Install("xxx.ipa", + err := iOSDriverExt.GetDevice().Install("xxx.ipa", option.WithRetryTimes(5)) log.Error().Err(err) if err != nil { @@ -268,7 +268,7 @@ func Test_remoteWD_AppTerminate(t *testing.T) { func Test_remoteWD_Tap(t *testing.T) { setup(t) - err := driver.Tap(200, 300) + err := driver.TapXY(200, 300) if err != nil { t.Fatal(err) } @@ -277,7 +277,7 @@ func Test_remoteWD_Tap(t *testing.T) { func Test_remoteWD_DoubleTap(t *testing.T) { setup(t) - err := driver.DoubleTap(200, 300) + err := driver.DoubleTapXY(200, 300) if err != nil { t.Fatal(err) } diff --git a/runner.go b/runner.go index 6a2e4080..78dbfc7c 100644 --- a/runner.go +++ b/runner.go @@ -236,7 +236,7 @@ func (r *HRPRunner) Run(testcases ...ITestCase) (err error) { // release UI driver session defer func() { for _, client := range caseRunner.uixtDrivers { - client.Driver.DeleteSession() + client.DeleteSession() } }() @@ -604,8 +604,8 @@ func (r *SessionRunner) Start(givenVars map[string]interface{}) (summary *TestCa "uuid": uuid, } - if client.GetDriver().GetDevice().LogEnabled() { - log, err1 := client.GetDriver().StopCaptureLog() + if client.GetDevice().LogEnabled() { + log, err1 := client.StopCaptureLog() if err1 != nil { if err == nil { err = errors.Wrap(err1, "stop capture log failed") diff --git a/server/app.go b/server/app.go index 45533c6d..90a9848c 100644 --- a/server/app.go +++ b/server/app.go @@ -15,7 +15,7 @@ func foregroundAppHandler(c *gin.Context) { return } - appInfo, err := dExt.GetDriver().GetForegroundApp() + appInfo, err := dExt.GetForegroundApp() if err != nil { log.Err(err).Msg(fmt.Sprintf("[%s]: failed to unlick screen", c.HandlerName())) c.JSON(http.StatusInternalServerError, @@ -42,7 +42,7 @@ func clearAppHandler(c *gin.Context) { return } - err = dExt.Driver.AppClear(appClearReq.PackageName) + err = dExt.AppClear(appClearReq.PackageName) if err != nil { log.Err(err).Msg(fmt.Sprintf("[%s]: failed to unlick screen", c.HandlerName())) c.JSON(http.StatusInternalServerError, @@ -69,7 +69,7 @@ func launchAppHandler(c *gin.Context) { return } - err = dExt.GetDriver().AppLaunch(appLaunchReq.PackageName) + err = dExt.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, @@ -96,7 +96,7 @@ func terminalAppHandler(c *gin.Context) { return } - success, err := dExt.GetDriver().AppTerminate(appTerminalReq.PackageName) + success, err := dExt.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, diff --git a/server/key.go b/server/key.go index 27ab8ff7..f0d7fd59 100644 --- a/server/key.go +++ b/server/key.go @@ -16,7 +16,7 @@ func unlockHandler(c *gin.Context) { return } - err = dExt.GetDriver().Unlock() + err = dExt.Unlock() if err != nil { log.Err(err).Msg(fmt.Sprintf("[%s]: failed to unlick screen", c.HandlerName())) c.JSON(http.StatusInternalServerError, @@ -37,7 +37,7 @@ func homeHandler(c *gin.Context) { return } - err = dExt.GetDriver().Homescreen() + err = dExt.Homescreen() if err != nil { log.Err(err).Msg(fmt.Sprintf("[%s]: failed to enter homescreen", c.HandlerName())) c.JSON(http.StatusInternalServerError, @@ -64,7 +64,7 @@ func keycodeHandler(c *gin.Context) { return } - err = dExt.GetDriver().PressKeyCode(uixt.KeyCode(keycodeReq.Keycode)) + err = dExt.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, diff --git a/server/source.go b/server/source.go index 9a202994..8fa69d7f 100644 --- a/server/source.go +++ b/server/source.go @@ -18,7 +18,7 @@ func screenshotHandler(c *gin.Context) { return } - raw, err := dExt.GetDriver().Screenshot() + raw, err := dExt.Screenshot() if err != nil { log.Err(err).Msg(fmt.Sprintf("[%s]: failed to get screenshot", c.HandlerName())) c.JSON(http.StatusInternalServerError, @@ -84,7 +84,7 @@ func adbSourceHandler(c *gin.Context) { return } - source, err := dExt.GetDriver().Source() + source, err := dExt.Source() if err != nil { log.Err(err).Msg(fmt.Sprintf("[%s]: failed to get adb source", c.HandlerName())) c.JSON(http.StatusInternalServerError, diff --git a/server/ui.go b/server/ui.go index cbda5a0d..428cc232 100644 --- a/server/ui.go +++ b/server/ui.go @@ -107,7 +107,7 @@ func dragHandler(c *gin.Context) { return } } else { - err := dExt.GetDriver().Swipe( + err := dExt.Swipe( dragReq.FromX, dragReq.FromY, dragReq.ToX, dragReq.ToY, actionOptions...) if err != nil { @@ -140,7 +140,7 @@ func inputHandler(c *gin.Context) { return } - err = dExt.GetDriver().SendKeys(inputReq.Text, + err = dExt.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)) diff --git a/step_mobile_ui.go b/step_mobile_ui.go index f3c381c2..f477f9cd 100644 --- a/step_mobile_ui.go +++ b/step_mobile_ui.go @@ -666,7 +666,7 @@ func runStepMobileUI(s *SessionRunner, step IStep) (stepResult *StepResult, err }, StartTime: startTime.Unix(), } - if app, err1 := uiDriver.GetDriver().GetForegroundApp(); err1 == nil { + if app, err1 := uiDriver.GetForegroundApp(); err1 == nil { attachments["foreground_app"] = app.AppBaseInfo } else { log.Warn().Err(err1).Msg("save foreground app failed, ignore") @@ -693,7 +693,7 @@ func runStepMobileUI(s *SessionRunner, step IStep) (stepResult *StepResult, err } // save attachments - session := uiDriver.GetDriver().GetSession() + session := uiDriver.GetSession() for key, value := range session.GetData(true) { attachments[key] = value }