refactor: move install options to pkg/uixt/options/install

This commit is contained in:
lilong.129
2025-02-06 16:45:40 +08:00
parent 0accec04a9
commit b22f24cb6b
9 changed files with 58 additions and 55 deletions

View File

@@ -48,9 +48,9 @@ var installCmd = &cobra.Command{
}
err = driverExt.Install(args[0],
uixt.WithReinstall(replace),
uixt.WithDowngrade(downgrade),
uixt.WithGrantPermission(grant),
options.WithReinstall(replace),
options.WithDowngrade(downgrade),
options.WithGrantPermission(grant),
)
if err != nil {
fmt.Println(err)

View File

@@ -144,7 +144,8 @@ 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, WithRetryTimes(action.MaxRetryTimes)); err != nil {
if err = dExt.InstallByUrl(appUrl,
options.WithRetryTimes(action.MaxRetryTimes)); err != nil {
return errors.Wrap(err, "failed to install app")
}
}

View File

@@ -301,8 +301,8 @@ func (dev *AndroidDevice) Uninstall(packageName string) error {
return err
}
func (dev *AndroidDevice) Install(apkPath string, opts ...InstallOption) error {
installOpts := NewInstallOptions(opts...)
func (dev *AndroidDevice) Install(apkPath string, opts ...options.InstallOption) error {
installOpts := options.NewInstallOptions(opts...)
brand, err := dev.d.Brand()
if err != nil {
return err

View File

@@ -120,7 +120,7 @@ func (dev *HarmonyDevice) NewUSBDriver(opts ...options.DriverOption) (driver IWe
return harmonyDriver, nil
}
func (dev *HarmonyDevice) Install(appPath string, opts ...InstallOption) error {
func (dev *HarmonyDevice) Install(appPath string, opts ...options.InstallOption) error {
return nil
}

View File

@@ -12,54 +12,13 @@ import (
"github.com/httprunner/httprunner/v5/pkg/uixt/options"
)
type InstallOptions struct {
Reinstall bool
GrantPermission bool
Downgrade bool
RetryTimes int
}
type InstallOption func(o *InstallOptions)
func NewInstallOptions(opts ...InstallOption) *InstallOptions {
installOptions := &InstallOptions{}
for _, option := range opts {
option(installOptions)
}
return installOptions
}
func WithReinstall(reinstall bool) InstallOption {
return func(o *InstallOptions) {
o.Reinstall = reinstall
}
}
func WithGrantPermission(grantPermission bool) InstallOption {
return func(o *InstallOptions) {
o.GrantPermission = grantPermission
}
}
func WithDowngrade(downgrade bool) InstallOption {
return func(o *InstallOptions) {
o.Downgrade = downgrade
}
}
func WithRetryTimes(retryTimes int) InstallOption {
return func(o *InstallOptions) {
o.RetryTimes = retryTimes
}
}
type InstallResult struct {
Result int `json:"result"`
ErrorCode int `json:"errorCode"`
ErrorMsg string `json:"errorMsg"`
}
func (dExt *DriverExt) InstallByUrl(url string, opts ...InstallOption) error {
func (dExt *DriverExt) InstallByUrl(url string, opts ...options.InstallOption) error {
// 获取当前目录
cwd, err := os.Getwd()
if err != nil {
@@ -82,7 +41,7 @@ func (dExt *DriverExt) InstallByUrl(url string, opts ...InstallOption) error {
return nil
}
func (dExt *DriverExt) Install(filePath string, opts ...InstallOption) error {
func (dExt *DriverExt) Install(filePath string, opts ...options.InstallOption) error {
if _, ok := dExt.Device.(*AndroidDevice); ok {
stopChan := make(chan struct{})
go func() {

View File

@@ -359,7 +359,7 @@ type IDevice interface {
// TODO: add ctx to NewDriver
NewDriver(...options.DriverOption) (driverExt *DriverExt, err error)
Install(appPath string, opts ...InstallOption) error
Install(appPath string, opts ...options.InstallOption) error
Uninstall(packageName string) error
GetPackageInfo(packageName string) (AppInfo, error)

View File

@@ -323,8 +323,8 @@ func (dev *IOSDevice) NewDriver(opts ...options.DriverOption) (driverExt *Driver
return driverExt, nil
}
func (dev *IOSDevice) Install(appPath string, opts ...InstallOption) (err error) {
installOpts := NewInstallOptions(opts...)
func (dev *IOSDevice) Install(appPath string, opts ...options.InstallOption) (err error) {
installOpts := options.NewInstallOptions(opts...)
for i := 0; i <= installOpts.RetryTimes; i++ {
var conn *zipconduit.Connection
conn, err = zipconduit.New(dev.d)
@@ -343,7 +343,7 @@ func (dev *IOSDevice) Install(appPath string, opts ...InstallOption) (err error)
return err
}
func (dev *IOSDevice) InstallByUrl(url string, opts ...InstallOption) (err error) {
func (dev *IOSDevice) InstallByUrl(url string, opts ...options.InstallOption) (err error) {
appPath, err := builtin.DownloadFileByUrl(url)
if err != nil {
return err

View File

@@ -45,7 +45,8 @@ 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", WithRetryTimes(5))
err := iOSDriverExt.Install("xxx.ipa",
options.WithRetryTimes(5))
log.Error().Err(err)
if err != nil {
t.Fatal(err)

View File

@@ -0,0 +1,42 @@
package options
type InstallOptions struct {
Reinstall bool
GrantPermission bool
Downgrade bool
RetryTimes int
}
type InstallOption func(o *InstallOptions)
func NewInstallOptions(opts ...InstallOption) *InstallOptions {
installOptions := &InstallOptions{}
for _, option := range opts {
option(installOptions)
}
return installOptions
}
func WithReinstall(reinstall bool) InstallOption {
return func(o *InstallOptions) {
o.Reinstall = reinstall
}
}
func WithGrantPermission(grantPermission bool) InstallOption {
return func(o *InstallOptions) {
o.GrantPermission = grantPermission
}
}
func WithDowngrade(downgrade bool) InstallOption {
return func(o *InstallOptions) {
o.Downgrade = downgrade
}
}
func WithRetryTimes(retryTimes int) InstallOption {
return func(o *InstallOptions) {
o.RetryTimes = retryTimes
}
}