mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-19 19:43:03 +08:00
refactor: move hrp/ to root folder
This commit is contained in:
75
cmd/ios/apps.go
Normal file
75
cmd/ios/apps.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package ios
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/internal/sdk"
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt"
|
||||
)
|
||||
|
||||
type Application struct {
|
||||
CFBundleVersion string `json:"version"`
|
||||
CFBundleDisplayName string `json:"name"`
|
||||
CFBundleIdentifier string `json:"bundleId"`
|
||||
}
|
||||
|
||||
var listAppsCmd = &cobra.Command{
|
||||
Use: "apps",
|
||||
Short: "List all iOS installed apps",
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {},
|
||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
startTime := time.Now()
|
||||
defer func() {
|
||||
sdk.SendGA4Event("hrp_ios_apps", map[string]interface{}{
|
||||
"args": strings.Join(args, "-"),
|
||||
"success": err == nil,
|
||||
"engagement_time_msec": time.Since(startTime).Milliseconds(),
|
||||
})
|
||||
}()
|
||||
|
||||
device, err := getDevice(udid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
device.GetDeviceInfo()
|
||||
var applicationType uixt.ApplicationType
|
||||
switch appType {
|
||||
case "user":
|
||||
applicationType = uixt.ApplicationTypeUser
|
||||
case "system":
|
||||
applicationType = uixt.ApplicationTypeSystem
|
||||
case "internal":
|
||||
applicationType = uixt.ApplicationTypeInternal
|
||||
case "all":
|
||||
applicationType = uixt.ApplicationTypeAny
|
||||
}
|
||||
|
||||
result, err := device.ListApps(applicationType)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get app list failed %v", err)
|
||||
}
|
||||
|
||||
for _, app := range result {
|
||||
a := Application{}
|
||||
mapstructure.Decode(app, &a)
|
||||
|
||||
fmt.Printf("%-30.30s %-50.50s %-s\n",
|
||||
a.CFBundleDisplayName, a.CFBundleIdentifier, a.CFBundleVersion)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var appType string
|
||||
|
||||
func init() {
|
||||
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)
|
||||
}
|
||||
81
cmd/ios/devices.go
Normal file
81
cmd/ios/devices.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package ios
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/danielpaulus/go-ios/ios"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/internal/sdk"
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt"
|
||||
)
|
||||
|
||||
type Device struct {
|
||||
d ios.DeviceEntry
|
||||
UDID string `json:"UDID"`
|
||||
Status string `json:"status"`
|
||||
ConnectionType string `json:"connectionType"`
|
||||
ConnectionSpeed int `json:"connectionSpeed"`
|
||||
DeviceDetail *uixt.DeviceDetail `json:"deviceDetail,omitempty"`
|
||||
}
|
||||
|
||||
func (device *Device) GetStatus() string {
|
||||
if device.ConnectionType != "" {
|
||||
return "online"
|
||||
} else {
|
||||
return "offline"
|
||||
}
|
||||
}
|
||||
|
||||
func (device *Device) ToFormat() string {
|
||||
result, _ := json.MarshalIndent(device, "", "\t")
|
||||
return string(result)
|
||||
}
|
||||
|
||||
var listDevicesCmd = &cobra.Command{
|
||||
Use: "devices",
|
||||
Short: "List all iOS devices",
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {},
|
||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
startTime := time.Now()
|
||||
defer func() {
|
||||
sdk.SendGA4Event("hrp_ios_devices", map[string]interface{}{
|
||||
"args": strings.Join(args, "-"),
|
||||
"success": err == nil,
|
||||
"engagement_time_msec": time.Since(startTime).Milliseconds(),
|
||||
})
|
||||
}()
|
||||
|
||||
devices, err := uixt.GetIOSDevices(udid)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
for _, d := range devices {
|
||||
deviceProperties := d.Properties
|
||||
device := &Device{
|
||||
d: d,
|
||||
UDID: deviceProperties.SerialNumber,
|
||||
ConnectionType: deviceProperties.ConnectionType,
|
||||
ConnectionSpeed: deviceProperties.ConnectionSpeed,
|
||||
}
|
||||
device.Status = device.GetStatus()
|
||||
|
||||
fmt.Println(device.UDID, device.ConnectionType, device.Status)
|
||||
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var udid string
|
||||
|
||||
func init() {
|
||||
listDevicesCmd.Flags().StringVarP(&udid, "udid", "u", "", "filter by device's udid")
|
||||
iosRootCmd.AddCommand(listDevicesCmd)
|
||||
}
|
||||
24
cmd/ios/init.go
Normal file
24
cmd/ios/init.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package ios
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt"
|
||||
)
|
||||
|
||||
var iosRootCmd = &cobra.Command{
|
||||
Use: "ios",
|
||||
Short: "simple utils for ios device management",
|
||||
}
|
||||
|
||||
func getDevice(udid string) (*uixt.IOSDevice, error) {
|
||||
device, err := uixt.NewIOSDevice(uixt.WithUDID(udid))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return device, nil
|
||||
}
|
||||
|
||||
func Init(rootCmd *cobra.Command) {
|
||||
rootCmd.AddCommand(iosRootCmd)
|
||||
}
|
||||
52
cmd/ios/install.go
Normal file
52
cmd/ios/install.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package ios
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/internal/sdk"
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt"
|
||||
)
|
||||
|
||||
var installCmd = &cobra.Command{
|
||||
Use: "install [flags] PACKAGE",
|
||||
Short: "push package to the device and install them automatically",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
startTime := time.Now()
|
||||
defer func() {
|
||||
sdk.SendGA4Event("hrp_adb_devices", map[string]interface{}{
|
||||
"args": strings.Join(args, "-"),
|
||||
"success": err == nil,
|
||||
"engagement_time_msec": time.Since(startTime).Milliseconds(),
|
||||
})
|
||||
}()
|
||||
_, err = getDevice(udid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
device, err := uixt.NewIOSDevice(uixt.WithUDID(udid))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
err = device.Install(args[0])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
fmt.Println("success")
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
installCmd.Flags().StringVarP(&udid, "udid", "u", "", "filter by device's serial")
|
||||
|
||||
iosRootCmd.AddCommand(installCmd)
|
||||
}
|
||||
14
cmd/ios/ios_test.go
Normal file
14
cmd/ios/ios_test.go
Normal file
@@ -0,0 +1,14 @@
|
||||
//go:build localtest
|
||||
|
||||
package ios
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestGetDevice(t *testing.T) {
|
||||
device, err := getDevice(udid)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Logf("device: %v", device)
|
||||
}
|
||||
77
cmd/ios/mount.go
Normal file
77
cmd/ios/mount.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package ios
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/internal/builtin"
|
||||
"github.com/httprunner/httprunner/v5/internal/sdk"
|
||||
)
|
||||
|
||||
// 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) (err error) {
|
||||
startTime := time.Now()
|
||||
defer func() {
|
||||
sdk.SendGA4Event("hrp_ios_mount", map[string]interface{}{
|
||||
"args": strings.Join(args, "-"),
|
||||
"success": err == nil,
|
||||
"engagement_time_msec": time.Since(startTime).Milliseconds(),
|
||||
})
|
||||
}()
|
||||
|
||||
device, err := getDevice(udid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
images, errImage := device.ListImages()
|
||||
if err != nil {
|
||||
return fmt.Errorf("list device images failed: %v", err)
|
||||
}
|
||||
if listDeveloperDiskImage {
|
||||
for i, imgSign := range images {
|
||||
fmt.Printf("[%d] %s\n", i+1, imgSign)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if errImage == nil && len(images) > 0 {
|
||||
log.Info().Strs("images", images).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)
|
||||
}
|
||||
|
||||
if err = device.AutoMountImage(developerDiskImageDir); 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 developer disk image directory")
|
||||
mountCmd.Flags().StringVarP(&udid, "udid", "u", "", "specify device by udid")
|
||||
iosRootCmd.AddCommand(mountCmd)
|
||||
}
|
||||
50
cmd/ios/ps.go
Normal file
50
cmd/ios/ps.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package ios
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/internal/sdk"
|
||||
)
|
||||
|
||||
var psCmd = &cobra.Command{
|
||||
Use: "ps",
|
||||
Short: "show running processes",
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {},
|
||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
startTime := time.Now()
|
||||
defer func() {
|
||||
sdk.SendGA4Event("hrp_ios_ps", map[string]interface{}{
|
||||
"args": strings.Join(args, "-"),
|
||||
"success": err == nil,
|
||||
"engagement_time_msec": time.Since(startTime).Milliseconds(),
|
||||
})
|
||||
}()
|
||||
|
||||
device, err := getDevice(udid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
runningProcesses, err := device.ListProcess(!isAll)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, p := range runningProcesses {
|
||||
fmt.Printf("%4d %-"+fmt.Sprintf("%d", len(runningProcesses))+"s %20s %s\n",
|
||||
p.Pid, p.Name, time.Since(p.StartDate).String(), bundleID)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var isAll bool
|
||||
|
||||
func init() {
|
||||
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)
|
||||
}
|
||||
44
cmd/ios/reboot.go
Normal file
44
cmd/ios/reboot.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package ios
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/internal/sdk"
|
||||
)
|
||||
|
||||
var rebootCmd = &cobra.Command{
|
||||
Use: "reboot",
|
||||
Short: "reboot ios device",
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {},
|
||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
startTime := time.Now()
|
||||
defer func() {
|
||||
sdk.SendGA4Event("hrp_ios_reboot", map[string]interface{}{
|
||||
"args": strings.Join(args, "-"),
|
||||
"success": err == nil,
|
||||
"engagement_time_msec": time.Since(startTime).Milliseconds(),
|
||||
})
|
||||
}()
|
||||
|
||||
device, err := getDevice(udid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = device.Reboot()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("reboot %s success\n", device.UDID)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rebootCmd.Flags().StringVarP(&udid, "udid", "u", "", "specify device by udid")
|
||||
iosRootCmd.AddCommand(rebootCmd)
|
||||
}
|
||||
59
cmd/ios/uninstall.go
Normal file
59
cmd/ios/uninstall.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package ios
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/internal/sdk"
|
||||
"github.com/httprunner/httprunner/v5/pkg/uixt"
|
||||
)
|
||||
|
||||
var uninstallCmd = &cobra.Command{
|
||||
Use: "uninstall [flags] PACKAGE",
|
||||
Short: "uninstall package automatically",
|
||||
Args: cobra.MinimumNArgs(0),
|
||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
startTime := time.Now()
|
||||
defer func() {
|
||||
sdk.SendGA4Event("hrp_adb_devices", map[string]interface{}{
|
||||
"args": strings.Join(args, "-"),
|
||||
"success": err == nil,
|
||||
"engagement_time_msec": time.Since(startTime).Milliseconds(),
|
||||
})
|
||||
}()
|
||||
if len(bundleId) == 0 {
|
||||
return fmt.Errorf("bundleId is empty")
|
||||
}
|
||||
|
||||
_, err = getDevice(udid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
device, err := uixt.NewIOSDevice(uixt.WithUDID(udid))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
err = device.Uninstall(bundleId)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
fmt.Println("success")
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var bundleId string
|
||||
|
||||
func init() {
|
||||
uninstallCmd.Flags().StringVarP(&udid, "udid", "u", "", "filter by device's serial")
|
||||
uninstallCmd.Flags().StringVarP(&bundleId, "bundleId", "b", "", "bundleId to uninstall")
|
||||
|
||||
iosRootCmd.AddCommand(uninstallCmd)
|
||||
}
|
||||
56
cmd/ios/xctest.go
Normal file
56
cmd/ios/xctest.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package ios
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/internal/sdk"
|
||||
)
|
||||
|
||||
var xctestCmd = &cobra.Command{
|
||||
Use: "xctest",
|
||||
Short: "run xctest",
|
||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
startTime := time.Now()
|
||||
defer func() {
|
||||
sdk.SendGA4Event("hrp_ios_xctest", map[string]interface{}{
|
||||
"args": strings.Join(args, "-"),
|
||||
"success": err == nil,
|
||||
"engagement_time_msec": time.Since(startTime).Milliseconds(),
|
||||
})
|
||||
}()
|
||||
|
||||
if bundleID == "" {
|
||||
return fmt.Errorf("bundleID is required")
|
||||
}
|
||||
device, err := getDevice(udid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = device.RunXCTest(context.Background(), bundleID, testRunnerBundleID, xctestConfig)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "run xctest failed")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var (
|
||||
bundleID string
|
||||
testRunnerBundleID string
|
||||
xctestConfig string
|
||||
)
|
||||
|
||||
func init() {
|
||||
xctestCmd.Flags().StringVarP(&udid, "udid", "u", "", "specify ios device's UDID")
|
||||
xctestCmd.Flags().StringVarP(&bundleID, "bundleID", "b", "com.gtf.wda.runner.xctrunner", "specify ios bundleID")
|
||||
xctestCmd.Flags().StringVarP(&testRunnerBundleID, "testRunnerBundleID", "t", "com.gtf.wda.runner.xctrunner", "specify ios testRunnerBundleID")
|
||||
xctestCmd.Flags().StringVarP(&xctestConfig, "xctestConfig", "x", "GtfWdaRunner.xctest", "specify ios xctestConfig")
|
||||
iosRootCmd.AddCommand(xctestCmd)
|
||||
}
|
||||
Reference in New Issue
Block a user