refactor: device Install interface

This commit is contained in:
lilong.129
2024-10-23 15:41:27 +08:00
parent 4b568dac01
commit b8083cd03e
10 changed files with 51 additions and 29 deletions

View File

@@ -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)
}

View File

@@ -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

View File

@@ -1 +1 @@
v5.0.0-beta-2410231523
v5.0.0-beta-2410232102

View File

@@ -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")
}
}

View File

@@ -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

View File

@@ -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
}

View File

@@ -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 {

View File

@@ -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

View File

@@ -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

View File

@@ -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)