From 08b40fe7c6197d6ddaa08442b9b242fea2bd93c6 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Mon, 10 Feb 2025 21:28:45 +0800 Subject: [PATCH] fix: NewIOSDevice --- cmd/ios/devices.go | 17 ++++---- internal/version/VERSION | 2 +- pkg/uixt/android_device.go | 4 +- pkg/uixt/ios_device.go | 84 ++++++++++++++------------------------ 4 files changed, 40 insertions(+), 67 deletions(-) diff --git a/cmd/ios/devices.go b/cmd/ios/devices.go index 5cd607ad..f9718f32 100644 --- a/cmd/ios/devices.go +++ b/cmd/ios/devices.go @@ -3,13 +3,14 @@ package ios import ( "encoding/json" "fmt" - "os" "strings" "time" "github.com/danielpaulus/go-ios/ios" + "github.com/pkg/errors" "github.com/spf13/cobra" + "github.com/httprunner/httprunner/v5/code" "github.com/httprunner/httprunner/v5/internal/sdk" "github.com/httprunner/httprunner/v5/pkg/uixt" ) @@ -50,13 +51,13 @@ var listDevicesCmd = &cobra.Command{ }) }() - devices, err := uixt.GetIOSDevices(udid) + devices, err := ios.ListDevices() if err != nil { - fmt.Println(err) - os.Exit(0) + return errors.Wrap(code.DeviceConnectionError, + fmt.Sprintf("list ios devices failed: %v", err)) } - for _, d := range devices { + for _, d := range devices.DeviceList { deviceProperties := d.Properties device := &Device{ d: d, @@ -64,10 +65,7 @@ var listDevicesCmd = &cobra.Command{ ConnectionType: deviceProperties.ConnectionType, ConnectionSpeed: deviceProperties.ConnectionSpeed, } - device.Status = device.GetStatus() - - fmt.Println(device.UDID, device.ConnectionType, device.Status) - + fmt.Println(device.UDID, device.ConnectionType, device.GetStatus()) } return nil }, @@ -76,6 +74,5 @@ var listDevicesCmd = &cobra.Command{ var udid string func init() { - listDevicesCmd.Flags().StringVarP(&udid, "udid", "u", "", "filter by device's udid") iosRootCmd.AddCommand(listDevicesCmd) } diff --git a/internal/version/VERSION b/internal/version/VERSION index d6f35cce..f709c3d2 100644 --- a/internal/version/VERSION +++ b/internal/version/VERSION @@ -1 +1 @@ -v5.0.0+2502102045 +v5.0.0+2502102128 diff --git a/pkg/uixt/android_device.go b/pkg/uixt/android_device.go index 21a9731e..71b717a1 100644 --- a/pkg/uixt/android_device.go +++ b/pkg/uixt/android_device.go @@ -44,11 +44,11 @@ func NewAndroidDevice(opts ...option.AndroidDeviceOption) (device *AndroidDevice adbClient, err := gadb.NewClientWith( androidOptions.AdbServerHost, androidOptions.AdbServerPort) if err != nil { - return nil, err + return nil, errors.Wrap(code.DeviceConnectionError, err.Error()) } devices, err := adbClient.DeviceList() if err != nil { - return nil, err + return nil, errors.Wrap(code.DeviceConnectionError, err.Error()) } if len(devices) == 0 { return nil, errors.Wrapf(code.DeviceConnectionError, diff --git a/pkg/uixt/ios_device.go b/pkg/uixt/ios_device.go index 0c90cc0b..b70a5c39 100644 --- a/pkg/uixt/ios_device.go +++ b/pkg/uixt/ios_device.go @@ -33,43 +33,6 @@ import ( "github.com/httprunner/httprunner/v5/pkg/uixt/types" ) -var tunnelManager *tunnel.TunnelManager = nil - -func GetIOSDevices(udid ...string) (deviceList []ios.DeviceEntry, err error) { - devices, err := ios.ListDevices() - if err != nil { - return nil, errors.Wrap(code.DeviceConnectionError, - fmt.Sprintf("list ios devices failed: %v", err)) - } - for _, d := range devices.DeviceList { - if len(udid) > 0 { - for _, u := range udid { - if u != "" && u != d.Properties.SerialNumber { - continue - } - // filter non-usb ios devices - if d.Properties.ConnectionType != "USB" { - continue - } - deviceList = append(deviceList, d) - } - } else { - deviceList = devices.DeviceList - } - } - - if len(deviceList) == 0 { - var err error - if udid == nil || (len(udid) == 1 && udid[0] == "") { - err = fmt.Errorf("no ios device found") - } else { - err = fmt.Errorf("no ios device found for udid %v", udid) - } - return nil, err - } - return deviceList, nil -} - func StartTunnel(recordsPath string, tunnelInfoPort int, userspaceTUN bool) (err error) { pm, err := tunnel.NewPairRecordManager(recordsPath) if err != nil { @@ -98,6 +61,8 @@ func StartTunnel(recordsPath string, tunnelInfoPort int, userspaceTUN bool) (err return nil } +var tunnelManager *tunnel.TunnelManager = nil + func RebootTunnel() (err error) { if tunnelManager != nil { _ = tunnelManager.Close() @@ -107,36 +72,47 @@ func RebootTunnel() (err error) { func NewIOSDevice(opts ...option.IOSDeviceOption) (device *IOSDevice, err error) { deviceOptions := option.NewIOSDeviceOptions(opts...) - deviceList, err := GetIOSDevices(deviceOptions.UDID) + + // get all attached ios devices + devices, err := ios.ListDevices() if err != nil { return nil, errors.Wrap(code.DeviceConnectionError, err.Error()) } - - if deviceOptions.UDID == "" && len(deviceList) > 1 { - return nil, errors.Wrap(code.DeviceConnectionError, "more than one device connected, please specify the udid") + if len(devices.DeviceList) == 0 { + return nil, errors.Wrapf(code.DeviceConnectionError, + "no attached ios devices") } - dev := deviceList[0] - udid := dev.Properties.SerialNumber - + // filter device by udid + var iosDevice *ios.DeviceEntry if deviceOptions.UDID == "" { - deviceOptions.UDID = udid - log.Warn(). - Str("udid", udid). - Msg("ios UDID is not specified, select the first one") + if len(devices.DeviceList) > 1 { + return nil, errors.Wrap(code.DeviceConnectionError, + "more than one device connected, please specify the udid") + } + iosDevice = &devices.DeviceList[0] + deviceOptions.UDID = iosDevice.Properties.SerialNumber + log.Warn().Str("udid", deviceOptions.UDID). + Msg("ios UDID is not specified, select the attached one") + } else { + for _, d := range devices.DeviceList { + if d.Properties.SerialNumber == deviceOptions.UDID { + iosDevice = &d + break + } + } + if iosDevice == nil { + return nil, errors.Wrapf(code.DeviceConnectionError, + "ios device %s not attached", deviceOptions.UDID) + } } device = &IOSDevice{ - DeviceEntry: dev, + DeviceEntry: *iosDevice, Options: deviceOptions, listeners: make(map[int]*forward.ConnListener), } log.Info().Str("udid", device.Options.UDID).Msg("init ios device") - err = device.Setup() - if err != nil { - _ = device.Teardown() - return nil, err - } return device, nil }