refactor: device Install interface

This commit is contained in:
lilong.129
2024-10-23 21:02:06 +08:00
parent 4b568dac01
commit b8083cd03e
10 changed files with 51 additions and 29 deletions
+14 -7
View File
@@ -11,6 +11,12 @@ import (
"github.com/httprunner/httprunner/v4/hrp/pkg/uixt" "github.com/httprunner/httprunner/v4/hrp/pkg/uixt"
) )
var (
replace bool
downgrade bool
grant bool
)
var installCmd = &cobra.Command{ var installCmd = &cobra.Command{
Use: "install [flags] PACKAGE", Use: "install [flags] PACKAGE",
Short: "Push package to the device and install them atomically", Short: "Push package to the device and install them atomically",
@@ -39,11 +45,12 @@ var installCmd = &cobra.Command{
fmt.Println(err) fmt.Println(err)
return 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 { if err != nil {
fmt.Println(err) fmt.Println(err)
return err return err
@@ -55,8 +62,8 @@ var installCmd = &cobra.Command{
func init() { func init() {
installCmd.Flags().StringVarP(&serial, "serial", "s", "", "filter by device's serial") installCmd.Flags().StringVarP(&serial, "serial", "s", "", "filter by device's serial")
installCmd.Flags().BoolP("replace", "r", false, "replace existing application") installCmd.Flags().BoolVarP(&replace, "replace", "r", false, "replace existing application")
installCmd.Flags().BoolP("downgrade", "d", false, "allow version code downgrade (debuggable packages only)") installCmd.Flags().BoolVarP(&downgrade, "downgrade", "d", false, "allow version code downgrade (debuggable packages only)")
installCmd.Flags().BoolP("grant", "g", false, "grant all runtime permissions") installCmd.Flags().BoolVarP(&grant, "grant", "g", false, "grant all runtime permissions")
androidRootCmd.AddCommand(installCmd) androidRootCmd.AddCommand(installCmd)
} }
+6 -1
View File
@@ -34,8 +34,13 @@ var installCmd = &cobra.Command{
fmt.Println(err) fmt.Println(err)
return 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 { if err != nil {
fmt.Println(err) fmt.Println(err)
return err return err
+1 -1
View File
@@ -1 +1 @@
v5.0.0-beta-2410231523 v5.0.0-beta-2410232102
+1 -1
View File
@@ -563,7 +563,7 @@ func (dExt *DriverExt) DoAction(action MobileAction) (err error) {
switch action.Method { switch action.Method {
case ACTION_AppInstall: case ACTION_AppInstall:
if appUrl, ok := action.Params.(string); ok { 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") return errors.Wrap(err, "failed to install app")
} }
} }
+2 -1
View File
@@ -361,7 +361,8 @@ func (dev *AndroidDevice) Uninstall(packageName string) error {
return myexec.RunCommand("adb", "-s", dev.SerialNumber, "uninstall", packageName) 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() brand, err := dev.d.Brand()
if err != nil { if err != nil {
return err return err
+1 -1
View File
@@ -171,7 +171,7 @@ func (dev *HarmonyDevice) StopPcap() string {
return "" return ""
} }
func (dev *HarmonyDevice) Install(appPath string, opts *InstallOptions) error { func (dev *HarmonyDevice) Install(appPath string, options ...InstallOption) error {
return nil return nil
} }
+21 -11
View File
@@ -15,7 +15,7 @@ type InstallOptions struct {
Reinstall bool Reinstall bool
GrantPermission bool GrantPermission bool
Downgrade bool Downgrade bool
RetryTime int RetryTimes int
} }
type InstallOption func(o *InstallOptions) 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) { return func(o *InstallOptions) {
o.RetryTime = retryTime o.RetryTimes = retryTimes
} }
} }
@@ -58,7 +58,7 @@ type InstallResult struct {
ErrorMsg string `json:"errorMsg"` 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() cwd, err := os.Getwd()
if err != nil { if err != nil {
@@ -73,7 +73,7 @@ func (dExt *DriverExt) InstallByUrl(url string, opts *InstallOptions) error {
return err return err
} }
err = dExt.Install(appPath, opts) err = dExt.Install(appPath, options...)
if err != nil { if err != nil {
log.Error().Err(err).Msg("install app failed") log.Error().Err(err).Msg("install app failed")
return err return err
@@ -81,7 +81,7 @@ func (dExt *DriverExt) InstallByUrl(url string, opts *InstallOptions) error {
return nil 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 { if _, ok := dExt.Device.(*AndroidDevice); ok {
stopChan := make(chan struct{}) stopChan := make(chan struct{})
go func() { go func() {
@@ -92,13 +92,23 @@ func (dExt *DriverExt) Install(filePath string, opts *InstallOptions) error {
select { select {
case <-ticker.C: case <-ticker.C:
actions := []TapTextAction{ 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.Driver.TapByTexts(actions...)
_ = dExt.TapByOCR("^(.*无视风险安装|确定|继续|完成|点击继续安装|继续安装旧版本|替换|安装|授权本次安装|继续安装|重新安装)$", WithRegex(true), WithIgnoreNotFoundError(true))
_ = dExt.TapByOCR(
"^(.*无视风险安装|确定|继续|完成|点击继续安装|继续安装旧版本|替换|安装|授权本次安装|继续安装|重新安装)$",
WithRegex(true), WithIgnoreNotFoundError(true),
)
case <-stopChan: case <-stopChan:
fmt.Println("Ticker stopped") log.Info().Msg("Ticker stopped")
return 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 { func (dExt *DriverExt) Uninstall(packageName string, options ...ActionOption) error {
+1 -3
View File
@@ -491,7 +491,7 @@ type IDevice interface {
StartPcap() error StartPcap() error
StopPcap() string StopPcap() string
Install(appPath string, opts *InstallOptions) error Install(appPath string, options ...InstallOption) error
Uninstall(packageName string) error Uninstall(packageName string) error
GetPackageInfo(packageName string) (AppInfo, error) GetPackageInfo(packageName string) (AppInfo, error)
@@ -620,11 +620,9 @@ type IWebDriver interface {
Source(srcOpt ...SourceOption) (string, error) Source(srcOpt ...SourceOption) (string, error)
LoginNoneUI(packageName, phoneNumber string, captcha string) error LoginNoneUI(packageName, phoneNumber string, captcha string) error
LogoutNoneUI(packageName string) error LogoutNoneUI(packageName string) error
TapByText(text string, options ...ActionOption) error TapByText(text string, options ...ActionOption) error
TapByTexts(actions ...TapTextAction) error TapByTexts(actions ...TapTextAction) error
// AccessibleSource Return application elements accessibility tree // AccessibleSource Return application elements accessibility tree
+3 -2
View File
@@ -473,8 +473,9 @@ func (dev *IOSDevice) StopPcap() string {
return dev.pcapFile return dev.pcapFile
} }
func (dev *IOSDevice) Install(appPath string, opts *InstallOptions) (err error) { func (dev *IOSDevice) Install(appPath string, options ...InstallOption) (err error) {
for i := 0; i <= opts.RetryTime; i++ { installOptions := NewInstallOptions(options...)
for i := 0; i <= installOptions.RetryTimes; i++ {
err = builtin.RunCommand("go-ios", "install", "--path="+appPath, "--udid="+dev.UDID) err = builtin.RunCommand("go-ios", "install", "--path="+appPath, "--udid="+dev.UDID)
if err == nil { if err == nil {
return nil return nil
+1 -1
View File
@@ -41,7 +41,7 @@ func TestViaUSB(t *testing.T) {
func TestInstall(t *testing.T) { func TestInstall(t *testing.T) {
setup(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) log.Error().Err(err)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)