Ios 如何辨别哪个设备I';我在Xcode用户界面测试中是开着的吗?
在运行Xcode UI测试时,我想知道正在使用哪个设备/环境(例如iPad Air 2、iOS 9.0、模拟器) 如何获取此信息?使用Swift 3(根据需要将.pad更改为.phone): 使用旧版本的Swift:Ios 如何辨别哪个设备I';我在Xcode用户界面测试中是开着的吗?,ios,testing,xcode7,xcode-ui-testing,Ios,Testing,Xcode7,Xcode Ui Testing,在运行Xcode UI测试时,我想知道正在使用哪个设备/环境(例如iPad Air 2、iOS 9.0、模拟器) 如何获取此信息?使用Swift 3(根据需要将.pad更改为.phone): 使用旧版本的Swift: UIDevice.currentDevice().userInterfaceIdiom 不幸的是,没有直接查询当前设备的方法。但是,您可以通过查询设备的大小类别来解决此问题: private func isIpad(app: XCUIApplication) -> Bool
UIDevice.currentDevice().userInterfaceIdiom
不幸的是,没有直接查询当前设备的方法。但是,您可以通过查询设备的大小类别来解决此问题:
private func isIpad(app: XCUIApplication) -> Bool {
return app.windows.elementBoundByIndex(0).horizontalSizeClass == .Regular && app.windows.elementBoundByIndex(0).verticalSizeClass == .Regular
}
正如您在中所看到的,目前只有iPad设备同时具有垂直和水平尺寸等级“常规”。也许有人会在目标C上的XCTest中派上用场:
// Check if the device is iPhone
if ( ([[app windows] elementBoundByIndex:0].horizontalSizeClass != XCUIUserInterfaceSizeClassRegular) || ([[app windows] elementBoundByIndex:0].verticalSizeClass != XCUIUserInterfaceSizeClassRegular) ) {
// do something for iPhone
}
else {
// do something for iPad
}
您可以使用
windows
元素框xguiapplication().windows.element(boundBy:0.frame)进行检查,并检查设备类型
您还可以使用currentDevice
属性为xguidevice
设置扩展名:
/// Device types
public enum Devices: CGFloat {
/// iPhone
case iPhone4 = 480
case iPhone5 = 568
case iPhone7 = 667
case iPhone7Plus = 736
/// iPad - Portraite
case iPad = 1024
case iPadPro = 1366
/// iPad - Landscape
case iPad_Landscape = 768
case iPadPro_Landscape = 0
}
/// Check current device
extension XCUIDevice {
public static var currentDevice:Devices {
get {
let orientation = XCUIDevice.shared().orientation
let frame = XCUIApplication().windows.element(boundBy: 0).frame
switch orientation {
case .landscapeLeft, .landscapeRight:
return frame.width == 1024 ? .iPadPro_Landscape : Devices(rawValue: frame.width)!
default:
return Devices(rawValue: frame.height)!
}
}
}
}
用法
让currentDevice=xguidevice.currentDevice
- Swift:5.2.4
- Xcode:11.6
var-isiPad:Bool{
返回UIDevice.current.userInterfaceIdiom==.pad
}
使用iOS13+,您现在可以使用UITraitCollection.current
获取当前环境的完整特征集。()
如果您只想获得trait集合的水平/垂直大小类,那么您可以通过访问测试中的myxguielement.horizontalSizeClass
和.verticalSizeClass
更向后兼容测试(Xcode 10.0+),因为它们是通过所有UI元素采用的协议公开的。(请注意,尽管我在取消xguiapplication()
时遇到了.unspecified
;但最好在窗口中使用真正的UI元素。如果手头没有,您仍然可以使用类似于app!.windows.element(boundBy:0)。horizontalSizeClass=.regular
的东西,如前所述。)“不幸的是,没有直接查询当前设备的方法”有点误导-您无法获得完整的设备详细信息,但您可以区分iPad和iPhone,请参阅cakes88使用UIDevice.current.userInterfaceIdiom的回答。不确定为什么会被否决。XUIApplication().windows.element(boundBy:0).frame是我真正需要的能够区分不同设备的产品线。谢谢!
/// Device types
public enum Devices: CGFloat {
/// iPhone
case iPhone4 = 480
case iPhone5 = 568
case iPhone7 = 667
case iPhone7Plus = 736
/// iPad - Portraite
case iPad = 1024
case iPadPro = 1366
/// iPad - Landscape
case iPad_Landscape = 768
case iPadPro_Landscape = 0
}
/// Check current device
extension XCUIDevice {
public static var currentDevice:Devices {
get {
let orientation = XCUIDevice.shared().orientation
let frame = XCUIApplication().windows.element(boundBy: 0).frame
switch orientation {
case .landscapeLeft, .landscapeRight:
return frame.width == 1024 ? .iPadPro_Landscape : Devices(rawValue: frame.width)!
default:
return Devices(rawValue: frame.height)!
}
}
}
}