From b8083cd03eb967c96f80a5d9831674c5d8279954 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Wed, 23 Oct 2024 15:41:27 +0800 Subject: [PATCH] refactor: device Install interface --- hrp/cmd/adb/install.go | 21 ++++++++++++++------- hrp/cmd/ios/install.go | 7 ++++++- hrp/internal/version/VERSION | 2 +- hrp/pkg/uixt/action.go | 2 +- hrp/pkg/uixt/android_device.go | 3 ++- hrp/pkg/uixt/harmony_device.go | 2 +- hrp/pkg/uixt/install.go | 32 +++++++++++++++++++++----------- hrp/pkg/uixt/interface.go | 4 +--- hrp/pkg/uixt/ios_device.go | 5 +++-- hrp/pkg/uixt/ios_test.go | 2 +- 10 files changed, 51 insertions(+), 29 deletions(-) diff --git a/hrp/cmd/adb/install.go b/hrp/cmd/adb/install.go index cce100de..caa87c2e 100644 --- a/hrp/cmd/adb/install.go +++ b/hrp/cmd/adb/install.go @@ -11,6 +11,12 @@ import ( "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) +var ( + replace bool + downgrade bool + grant bool +) + var installCmd = &cobra.Command{ Use: "install [flags] PACKAGE", Short: "Push package to the device and install them atomically", @@ -39,11 +45,12 @@ var installCmd = &cobra.Command{ fmt.Println(err) return err } - replace, _ := cmd.Flags().GetBool("replace") - downgrade, _ := cmd.Flags().GetBool("downgrade") - grant, _ := cmd.Flags().GetBool("grant") - err = driverExt.Install(args[0], uixt.NewInstallOptions(uixt.WithReinstall(replace), uixt.WithDowngrade(downgrade), uixt.WithGrantPermission(grant))) + err = driverExt.Install(args[0], + uixt.WithReinstall(replace), + uixt.WithDowngrade(downgrade), + uixt.WithGrantPermission(grant), + ) if err != nil { fmt.Println(err) return err @@ -55,8 +62,8 @@ var installCmd = &cobra.Command{ func init() { installCmd.Flags().StringVarP(&serial, "serial", "s", "", "filter by device's serial") - installCmd.Flags().BoolP("replace", "r", false, "replace existing application") - installCmd.Flags().BoolP("downgrade", "d", false, "allow version code downgrade (debuggable packages only)") - installCmd.Flags().BoolP("grant", "g", false, "grant all runtime permissions") + installCmd.Flags().BoolVarP(&replace, "replace", "r", false, "replace existing application") + installCmd.Flags().BoolVarP(&downgrade, "downgrade", "d", false, "allow version code downgrade (debuggable packages only)") + installCmd.Flags().BoolVarP(&grant, "grant", "g", false, "grant all runtime permissions") androidRootCmd.AddCommand(installCmd) } diff --git a/hrp/cmd/ios/install.go b/hrp/cmd/ios/install.go index 7fdcd3eb..97ac5d4b 100644 --- a/hrp/cmd/ios/install.go +++ b/hrp/cmd/ios/install.go @@ -34,8 +34,13 @@ var installCmd = &cobra.Command{ fmt.Println(err) return err } + driverExt, err := device.NewDriver() + if err != nil { + fmt.Println(err) + return err + } - err = device.Install(args[0], uixt.NewInstallOptions()) + err = driverExt.Install(args[0]) if err != nil { fmt.Println(err) return err diff --git a/hrp/internal/version/VERSION b/hrp/internal/version/VERSION index dfa78f7f..498a3ae0 100644 --- a/hrp/internal/version/VERSION +++ b/hrp/internal/version/VERSION @@ -1 +1 @@ -v5.0.0-beta-2410231523 +v5.0.0-beta-2410232102 diff --git a/hrp/pkg/uixt/action.go b/hrp/pkg/uixt/action.go index 246c0920..27438c61 100644 --- a/hrp/pkg/uixt/action.go +++ b/hrp/pkg/uixt/action.go @@ -563,7 +563,7 @@ func (dExt *DriverExt) DoAction(action MobileAction) (err error) { switch action.Method { case ACTION_AppInstall: if appUrl, ok := action.Params.(string); ok { - if err = dExt.InstallByUrl(appUrl, NewInstallOptions(WithRetryTime(action.MaxRetryTimes))); err != nil { + if err = dExt.InstallByUrl(appUrl, WithRetryTimes(action.MaxRetryTimes)); err != nil { return errors.Wrap(err, "failed to install app") } } diff --git a/hrp/pkg/uixt/android_device.go b/hrp/pkg/uixt/android_device.go index e205871f..70a4cc19 100644 --- a/hrp/pkg/uixt/android_device.go +++ b/hrp/pkg/uixt/android_device.go @@ -361,7 +361,8 @@ func (dev *AndroidDevice) Uninstall(packageName string) error { return myexec.RunCommand("adb", "-s", dev.SerialNumber, "uninstall", packageName) } -func (dev *AndroidDevice) Install(apkPath string, opts *InstallOptions) error { +func (dev *AndroidDevice) Install(apkPath string, options ...InstallOption) error { + opts := NewInstallOptions(options...) brand, err := dev.d.Brand() if err != nil { return err diff --git a/hrp/pkg/uixt/harmony_device.go b/hrp/pkg/uixt/harmony_device.go index 6a3853d7..99955672 100644 --- a/hrp/pkg/uixt/harmony_device.go +++ b/hrp/pkg/uixt/harmony_device.go @@ -171,7 +171,7 @@ func (dev *HarmonyDevice) StopPcap() string { return "" } -func (dev *HarmonyDevice) Install(appPath string, opts *InstallOptions) error { +func (dev *HarmonyDevice) Install(appPath string, options ...InstallOption) error { return nil } diff --git a/hrp/pkg/uixt/install.go b/hrp/pkg/uixt/install.go index e0811fc9..3bbaaea0 100644 --- a/hrp/pkg/uixt/install.go +++ b/hrp/pkg/uixt/install.go @@ -15,7 +15,7 @@ type InstallOptions struct { Reinstall bool GrantPermission bool Downgrade bool - RetryTime int + RetryTimes int } type InstallOption func(o *InstallOptions) @@ -46,9 +46,9 @@ func WithDowngrade(downgrade bool) InstallOption { } } -func WithRetryTime(retryTime int) InstallOption { +func WithRetryTimes(retryTimes int) InstallOption { return func(o *InstallOptions) { - o.RetryTime = retryTime + o.RetryTimes = retryTimes } } @@ -58,7 +58,7 @@ type InstallResult struct { ErrorMsg string `json:"errorMsg"` } -func (dExt *DriverExt) InstallByUrl(url string, opts *InstallOptions) error { +func (dExt *DriverExt) InstallByUrl(url string, options ...InstallOption) error { // 获取当前目录 cwd, err := os.Getwd() if err != nil { @@ -73,7 +73,7 @@ func (dExt *DriverExt) InstallByUrl(url string, opts *InstallOptions) error { return err } - err = dExt.Install(appPath, opts) + err = dExt.Install(appPath, options...) if err != nil { log.Error().Err(err).Msg("install app failed") return err @@ -81,7 +81,7 @@ func (dExt *DriverExt) InstallByUrl(url string, opts *InstallOptions) error { return nil } -func (dExt *DriverExt) Install(filePath string, opts *InstallOptions) error { +func (dExt *DriverExt) Install(filePath string, options ...InstallOption) error { if _, ok := dExt.Device.(*AndroidDevice); ok { stopChan := make(chan struct{}) go func() { @@ -92,13 +92,23 @@ func (dExt *DriverExt) Install(filePath string, opts *InstallOptions) error { select { case <-ticker.C: actions := []TapTextAction{ - {Text: "^.*无视风险安装$", Options: []ActionOption{WithTapOffset(100, 0), WithRegex(true), WithIgnoreNotFoundError(true)}}, - {Text: "^已了解此应用未经检测.*", Options: []ActionOption{WithTapOffset(-450, 0), WithRegex(true), WithIgnoreNotFoundError(true)}}, + { + Text: "^.*无视风险安装$", + Options: []ActionOption{WithTapOffset(100, 0), WithRegex(true), WithIgnoreNotFoundError(true)}, + }, + { + Text: "^已了解此应用未经检测.*", + Options: []ActionOption{WithTapOffset(-450, 0), WithRegex(true), WithIgnoreNotFoundError(true)}, + }, } _ = dExt.Driver.TapByTexts(actions...) - _ = dExt.TapByOCR("^(.*无视风险安装|确定|继续|完成|点击继续安装|继续安装旧版本|替换|安装|授权本次安装|继续安装|重新安装)$", WithRegex(true), WithIgnoreNotFoundError(true)) + + _ = dExt.TapByOCR( + "^(.*无视风险安装|确定|继续|完成|点击继续安装|继续安装旧版本|替换|安装|授权本次安装|继续安装|重新安装)$", + WithRegex(true), WithIgnoreNotFoundError(true), + ) case <-stopChan: - fmt.Println("Ticker stopped") + log.Info().Msg("Ticker stopped") return } } @@ -108,7 +118,7 @@ func (dExt *DriverExt) Install(filePath string, opts *InstallOptions) error { }() } - return dExt.Device.Install(filePath, opts) + return dExt.Device.Install(filePath, options...) } func (dExt *DriverExt) Uninstall(packageName string, options ...ActionOption) error { diff --git a/hrp/pkg/uixt/interface.go b/hrp/pkg/uixt/interface.go index adc4c578..32ebf6f4 100644 --- a/hrp/pkg/uixt/interface.go +++ b/hrp/pkg/uixt/interface.go @@ -491,7 +491,7 @@ type IDevice interface { StartPcap() error StopPcap() string - Install(appPath string, opts *InstallOptions) error + Install(appPath string, options ...InstallOption) error Uninstall(packageName string) error GetPackageInfo(packageName string) (AppInfo, error) @@ -620,11 +620,9 @@ type IWebDriver interface { Source(srcOpt ...SourceOption) (string, error) LoginNoneUI(packageName, phoneNumber string, captcha string) error - LogoutNoneUI(packageName string) error TapByText(text string, options ...ActionOption) error - TapByTexts(actions ...TapTextAction) error // AccessibleSource Return application elements accessibility tree diff --git a/hrp/pkg/uixt/ios_device.go b/hrp/pkg/uixt/ios_device.go index dbc9a302..8b0e0735 100644 --- a/hrp/pkg/uixt/ios_device.go +++ b/hrp/pkg/uixt/ios_device.go @@ -473,8 +473,9 @@ func (dev *IOSDevice) StopPcap() string { return dev.pcapFile } -func (dev *IOSDevice) Install(appPath string, opts *InstallOptions) (err error) { - for i := 0; i <= opts.RetryTime; i++ { +func (dev *IOSDevice) Install(appPath string, options ...InstallOption) (err error) { + installOptions := NewInstallOptions(options...) + for i := 0; i <= installOptions.RetryTimes; i++ { err = builtin.RunCommand("go-ios", "install", "--path="+appPath, "--udid="+dev.UDID) if err == nil { return nil diff --git a/hrp/pkg/uixt/ios_test.go b/hrp/pkg/uixt/ios_test.go index 6fb1ddc5..70f82fb4 100644 --- a/hrp/pkg/uixt/ios_test.go +++ b/hrp/pkg/uixt/ios_test.go @@ -41,7 +41,7 @@ func TestViaUSB(t *testing.T) { func TestInstall(t *testing.T) { setup(t) - err := iOSDriverExt.Install("/Users/bytedance/Downloads/com.yueyou.cyreader_1387717110_7.54.20.ipa", NewInstallOptions(WithRetryTime(5))) + err := iOSDriverExt.Install("/Users/bytedance/Downloads/com.yueyou.cyreader_1387717110_7.54.20.ipa", WithRetryTimes(5)) log.Error().Err(err) if err != nil { t.Fatal(err)