diff --git a/hrp/cmd/ios/apps.go b/hrp/cmd/ios/apps.go index a77f74c7..d8a62f41 100644 --- a/hrp/cmd/ios/apps.go +++ b/hrp/cmd/ios/apps.go @@ -15,8 +15,9 @@ type Application struct { } var listAppsCmd = &cobra.Command{ - Use: "apps", - Short: "List all iOS installed apps", + Use: "apps", + Short: "List all iOS installed apps", + PersistentPreRun: func(cmd *cobra.Command, args []string) {}, RunE: func(cmd *cobra.Command, args []string) error { device, err := getDevice(udid) if err != nil { @@ -56,7 +57,7 @@ var listAppsCmd = &cobra.Command{ var appType string func init() { - listAppsCmd.Flags().StringVarP(&udid, "udid", "u", "", "filter by device's udid") + listAppsCmd.Flags().StringVarP(&udid, "udid", "u", "", "specify device by udid") listAppsCmd.Flags().StringVarP(&appType, "type", "t", "user", "filter application type [user|system|internal|all]") iosRootCmd.AddCommand(listAppsCmd) } diff --git a/hrp/cmd/ios/devices.go b/hrp/cmd/ios/devices.go index ec884e19..58484e1f 100644 --- a/hrp/cmd/ios/devices.go +++ b/hrp/cmd/ios/devices.go @@ -66,8 +66,9 @@ func (device *Device) ToFormat() string { } var listDevicesCmd = &cobra.Command{ - Use: "devices", - Short: "List all iOS devices", + Use: "devices", + Short: "List all iOS devices", + PersistentPreRun: func(cmd *cobra.Command, args []string) {}, RunE: func(cmd *cobra.Command, args []string) error { devices, err := uixt.IOSDevices(udid) if err != nil { diff --git a/hrp/cmd/ios/init.go b/hrp/cmd/ios/init.go index 22642744..8cfea1f3 100644 --- a/hrp/cmd/ios/init.go +++ b/hrp/cmd/ios/init.go @@ -11,9 +11,8 @@ import ( ) var iosRootCmd = &cobra.Command{ - Use: "ios", - Short: "simple utils for ios device management", - PersistentPreRun: func(cmd *cobra.Command, args []string) {}, + Use: "ios", + Short: "simple utils for ios device management", } func getDevice(udid string) (giDevice.Device, error) { diff --git a/hrp/cmd/ios/mount.go b/hrp/cmd/ios/mount.go new file mode 100644 index 00000000..53972220 --- /dev/null +++ b/hrp/cmd/ios/mount.go @@ -0,0 +1,90 @@ +package ios + +import ( + "encoding/base64" + "fmt" + "path/filepath" + "strings" + + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" + + "github.com/httprunner/httprunner/v4/hrp/internal/builtin" +) + +// mountCmd represents the mount command +var mountCmd = &cobra.Command{ + Use: "mount", + Short: "A brief description of your command", + RunE: func(cmd *cobra.Command, args []string) error { + device, err := getDevice(udid) + if err != nil { + return err + } + + value, err := device.GetValue("", "ProductVersion") + if err != nil { + return fmt.Errorf("get device ProductVersion failed: %v", err) + } + log.Info().Str("version", value.(string)).Msg("get device version") + + imageSignatures, errImage := device.Images() + + if listDeveloperDiskImage { + for i, imgSign := range imageSignatures { + fmt.Printf("[%d] %s\n", i+1, base64.StdEncoding.EncodeToString(imgSign)) + } + return nil + } + + if errImage == nil && len(imageSignatures) > 0 { + log.Info().Msg("ios developer image is already mounted") + return nil + } + + log.Info().Str("dir", developerDiskImageDir).Msg("start to mount ios developer image") + + if !builtin.IsFolderPathExists(developerDiskImageDir) { + return fmt.Errorf("developer disk image directory not exist: %s", developerDiskImageDir) + } + + ver := strings.Split(value.(string), ".") + if len(ver) < 2 { + return fmt.Errorf("got invalid device ProductVersion: %v", value) + } + version := ver[0] + "." + ver[1] + + var dmgPath, signaturePath string + if builtin.IsFilePathExists(filepath.Join(developerDiskImageDir, "DeveloperDiskImage.dmg")) { + dmgPath = filepath.Join(developerDiskImageDir, "DeveloperDiskImage.dmg") + signaturePath = filepath.Join(developerDiskImageDir, "DeveloperDiskImage.dmg.signature") + } else if builtin.IsFilePathExists(filepath.Join(developerDiskImageDir, version, "DeveloperDiskImage.dmg.")) { + dmgPath = filepath.Join(developerDiskImageDir, version, "DeveloperDiskImage.dmg") + signaturePath = filepath.Join(developerDiskImageDir, version, "DeveloperDiskImage.dmg.signature") + } else { + log.Error().Str("dir", developerDiskImageDir).Msg("developer disk image not found in directory") + return fmt.Errorf("developer disk image not found") + } + + if err = device.MountDeveloperDiskImage(dmgPath, signaturePath); err != nil { + return fmt.Errorf("mount developer disk image failed: %s", err) + } + + log.Info().Msg("mount developer disk image successfully") + return nil + }, +} + +const defaultDeveloperDiskImageDir = "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/" + +var ( + developerDiskImageDir string + listDeveloperDiskImage bool +) + +func init() { + mountCmd.Flags().BoolVar(&listDeveloperDiskImage, "list", false, "list developer disk images") + mountCmd.Flags().StringVarP(&developerDiskImageDir, "dir", "d", defaultDeveloperDiskImageDir, "specify DeveloperDiskImage directory") + mountCmd.Flags().StringVarP(&udid, "udid", "u", "", "specify device by udid") + iosRootCmd.AddCommand(mountCmd) +} diff --git a/hrp/cmd/ios/ps.go b/hrp/cmd/ios/ps.go index 22f5a94b..d40dcc4d 100644 --- a/hrp/cmd/ios/ps.go +++ b/hrp/cmd/ios/ps.go @@ -9,8 +9,9 @@ import ( ) var psCmd = &cobra.Command{ - Use: "ps", - Short: "show running processes", + Use: "ps", + Short: "show running processes", + PersistentPreRun: func(cmd *cobra.Command, args []string) {}, RunE: func(cmd *cobra.Command, args []string) error { device, err := getDevice(udid) if err != nil { @@ -54,7 +55,7 @@ var psCmd = &cobra.Command{ var isAll bool func init() { - psCmd.Flags().StringVarP(&udid, "udid", "u", "", "filter by device's udid") + psCmd.Flags().StringVarP(&udid, "udid", "u", "", "specify device by udid") psCmd.Flags().BoolVarP(&isAll, "all", "a", false, "print all processes including system processes") iosRootCmd.AddCommand(psCmd) } diff --git a/hrp/cmd/ios/reboot.go b/hrp/cmd/ios/reboot.go index 3cddb6c0..d38d9db8 100644 --- a/hrp/cmd/ios/reboot.go +++ b/hrp/cmd/ios/reboot.go @@ -7,8 +7,9 @@ import ( ) var rebootCmd = &cobra.Command{ - Use: "reboot", - Short: "reboot or shutdown ios device", + Use: "reboot", + Short: "reboot or shutdown ios device", + PersistentPreRun: func(cmd *cobra.Command, args []string) {}, RunE: func(cmd *cobra.Command, args []string) error { device, err := getDevice(udid) if err != nil { @@ -31,7 +32,7 @@ var rebootCmd = &cobra.Command{ var isShutdown bool func init() { - rebootCmd.Flags().StringVarP(&udid, "udid", "u", "", "filter by device's udid") + rebootCmd.Flags().StringVarP(&udid, "udid", "u", "", "specify device by udid") rebootCmd.Flags().BoolVarP(&isShutdown, "shutdown", "s", false, "shutdown ios device") iosRootCmd.AddCommand(rebootCmd) } diff --git a/hrp/internal/uixt/android_test.go b/hrp/internal/uixt/android_test.go index 71c3790c..eecf7b91 100644 --- a/hrp/internal/uixt/android_test.go +++ b/hrp/internal/uixt/android_test.go @@ -1134,7 +1134,7 @@ func TestElement_SendKeys(t *testing.T) { // return // err = elem.SendKeys("abc") - err = elem.SendKeys("456", 0) + err = elem.SendKeys("456") if err != nil { t.Fatal(err) }