Sen*_*ful 19 testing ios xcode7 xcode-ui-testing
在运行Xcode UI测试时,我想知道正在使用哪个设备/环境(例如iPad Air 2,iOS 9.0,Simulator).
我怎样才能获得这些信息?
cak*_*s88 19
使用Swift 3(根据需要将.pad更改为.phone):
if UIDevice.current.userInterfaceIdiom == .pad {
// Ipad specific checks
}
Run Code Online (Sandbox Code Playgroud)
使用旧版本的Swift:
UIDevice.currentDevice().userInterfaceIdiom
Run Code Online (Sandbox Code Playgroud)
遗憾的是,没有直接查询当前设备的方法.但是,您可以通过查询设备的大小类来解决:
private func isIpad(app: XCUIApplication) -> Bool {
return app.windows.elementBoundByIndex(0).horizontalSizeClass == .Regular && app.windows.elementBoundByIndex(0).verticalSizeClass == .Regular
}
Run Code Online (Sandbox Code Playgroud)
正如您在尺寸类的Apple描述中所看到的,只有iPad设备(当前)具有垂直和水平尺寸类"常规".
您可以使用windows元素框架XCUIApplication().windows.element(boundBy: 0).frame进行检查并检查设备类型。
您还可以XCUIDevice使用currentDevice属性设置扩展名:
/// 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)!
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
用法
let currentDevice = XCUIDevice.currentDevice
| 归档时间: |
|
| 查看次数: |
3623 次 |
| 最近记录: |