mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-06 16:07:45 +08:00
fix: NewIOSDevice
This commit is contained in:
+7
-10
@@ -3,13 +3,14 @@ package ios
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/danielpaulus/go-ios/ios"
|
"github.com/danielpaulus/go-ios/ios"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/httprunner/httprunner/v5/code"
|
||||||
"github.com/httprunner/httprunner/v5/internal/sdk"
|
"github.com/httprunner/httprunner/v5/internal/sdk"
|
||||||
"github.com/httprunner/httprunner/v5/pkg/uixt"
|
"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 {
|
if err != nil {
|
||||||
fmt.Println(err)
|
return errors.Wrap(code.DeviceConnectionError,
|
||||||
os.Exit(0)
|
fmt.Sprintf("list ios devices failed: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, d := range devices {
|
for _, d := range devices.DeviceList {
|
||||||
deviceProperties := d.Properties
|
deviceProperties := d.Properties
|
||||||
device := &Device{
|
device := &Device{
|
||||||
d: d,
|
d: d,
|
||||||
@@ -64,10 +65,7 @@ var listDevicesCmd = &cobra.Command{
|
|||||||
ConnectionType: deviceProperties.ConnectionType,
|
ConnectionType: deviceProperties.ConnectionType,
|
||||||
ConnectionSpeed: deviceProperties.ConnectionSpeed,
|
ConnectionSpeed: deviceProperties.ConnectionSpeed,
|
||||||
}
|
}
|
||||||
device.Status = device.GetStatus()
|
fmt.Println(device.UDID, device.ConnectionType, device.GetStatus())
|
||||||
|
|
||||||
fmt.Println(device.UDID, device.ConnectionType, device.Status)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
@@ -76,6 +74,5 @@ var listDevicesCmd = &cobra.Command{
|
|||||||
var udid string
|
var udid string
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
listDevicesCmd.Flags().StringVarP(&udid, "udid", "u", "", "filter by device's udid")
|
|
||||||
iosRootCmd.AddCommand(listDevicesCmd)
|
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(
|
adbClient, err := gadb.NewClientWith(
|
||||||
androidOptions.AdbServerHost, androidOptions.AdbServerPort)
|
androidOptions.AdbServerHost, androidOptions.AdbServerPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errors.Wrap(code.DeviceConnectionError, err.Error())
|
||||||
}
|
}
|
||||||
devices, err := adbClient.DeviceList()
|
devices, err := adbClient.DeviceList()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errors.Wrap(code.DeviceConnectionError, err.Error())
|
||||||
}
|
}
|
||||||
if len(devices) == 0 {
|
if len(devices) == 0 {
|
||||||
return nil, errors.Wrapf(code.DeviceConnectionError,
|
return nil, errors.Wrapf(code.DeviceConnectionError,
|
||||||
|
|||||||
+30
-54
@@ -33,43 +33,6 @@ import (
|
|||||||
"github.com/httprunner/httprunner/v5/pkg/uixt/types"
|
"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) {
|
func StartTunnel(recordsPath string, tunnelInfoPort int, userspaceTUN bool) (err error) {
|
||||||
pm, err := tunnel.NewPairRecordManager(recordsPath)
|
pm, err := tunnel.NewPairRecordManager(recordsPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -98,6 +61,8 @@ func StartTunnel(recordsPath string, tunnelInfoPort int, userspaceTUN bool) (err
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var tunnelManager *tunnel.TunnelManager = nil
|
||||||
|
|
||||||
func RebootTunnel() (err error) {
|
func RebootTunnel() (err error) {
|
||||||
if tunnelManager != nil {
|
if tunnelManager != nil {
|
||||||
_ = tunnelManager.Close()
|
_ = tunnelManager.Close()
|
||||||
@@ -107,36 +72,47 @@ func RebootTunnel() (err error) {
|
|||||||
|
|
||||||
func NewIOSDevice(opts ...option.IOSDeviceOption) (device *IOSDevice, err error) {
|
func NewIOSDevice(opts ...option.IOSDeviceOption) (device *IOSDevice, err error) {
|
||||||
deviceOptions := option.NewIOSDeviceOptions(opts...)
|
deviceOptions := option.NewIOSDeviceOptions(opts...)
|
||||||
deviceList, err := GetIOSDevices(deviceOptions.UDID)
|
|
||||||
|
// get all attached ios devices
|
||||||
|
devices, err := ios.ListDevices()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(code.DeviceConnectionError, err.Error())
|
return nil, errors.Wrap(code.DeviceConnectionError, err.Error())
|
||||||
}
|
}
|
||||||
|
if len(devices.DeviceList) == 0 {
|
||||||
if deviceOptions.UDID == "" && len(deviceList) > 1 {
|
return nil, errors.Wrapf(code.DeviceConnectionError,
|
||||||
return nil, errors.Wrap(code.DeviceConnectionError, "more than one device connected, please specify the udid")
|
"no attached ios devices")
|
||||||
}
|
}
|
||||||
|
|
||||||
dev := deviceList[0]
|
// filter device by udid
|
||||||
udid := dev.Properties.SerialNumber
|
var iosDevice *ios.DeviceEntry
|
||||||
|
|
||||||
if deviceOptions.UDID == "" {
|
if deviceOptions.UDID == "" {
|
||||||
deviceOptions.UDID = udid
|
if len(devices.DeviceList) > 1 {
|
||||||
log.Warn().
|
return nil, errors.Wrap(code.DeviceConnectionError,
|
||||||
Str("udid", udid).
|
"more than one device connected, please specify the udid")
|
||||||
Msg("ios UDID is not specified, select the first one")
|
}
|
||||||
|
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{
|
device = &IOSDevice{
|
||||||
DeviceEntry: dev,
|
DeviceEntry: *iosDevice,
|
||||||
Options: deviceOptions,
|
Options: deviceOptions,
|
||||||
listeners: make(map[int]*forward.ConnListener),
|
listeners: make(map[int]*forward.ConnListener),
|
||||||
}
|
}
|
||||||
log.Info().Str("udid", device.Options.UDID).Msg("init ios device")
|
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
|
return device, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user