fix: NewIOSDevice
This commit is contained in:
parent
2d6e1fb5e4
commit
08b40fe7c6
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -1 +1 @@
|
|||
v5.0.0+2502102045
|
||||
v5.0.0+2502102128
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue