如何在Xcode UI测试中识别控制中心元素?

时间:2017-03-27 08:18:44

标签: ios swift xcode xctest xcode-ui-testing

我设法打开了设备的控制中心,但是我无法识别按钮,我需要更准确地使用Wi-Fi。我尝试使用录音机,它被识别为

app.scrollViews.otherElements.scrollViews.otherElements.switches [" Wi-Fi和#34;]

但是当我尝试再次运行测试时,它会失败,因为它找不到元素。 我也试图将它作为其他类型的元素(按钮或各种条形元素),但没有任何作用。还尝试通过其标签简单地使用app.buttons [" Wi-Fi"]来识别它,但仍然没有结果。

有没有人知道这方面的解决方案?

3 个答案:

答案 0 :(得分:0)

控制中心不在您测试的应用程序范围内,因此您的UI测试无法访问。

要禁用wifi,您需要在物理上断开设备与互联网的连接,因为它无法以编程方式与wifi断开连接。

答案 1 :(得分:0)

使用Xcode 9,现在可以访问控制中心(Springboard控制它)。现在它只能在物理设备上使用,因为Xcode 9 beta模拟器没有控制中心。也许这将在Xcode正式发布时修复。现在你必须使用真正的设备。

此测试打开控制中心并点按WiFi按钮:

func testSwitchOffWiFi() {
    let app = XCUIApplication()
    let springboard = XCUIApplication(bundleIdentifier: "com.apple.springboard")

    app.launch()

    // open control center
    let coord1 = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.99))
    let coord2 = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.5))
    coord1.press(forDuration: 0.1, thenDragTo: coord2)

    let wifiButton = springboard.switches["wifi-button"]
    wifiButton.tap()
}

答案 2 :(得分:0)

使用设置应用程序即可轻松完成...

  • 首先,如果您查询任务栏,则知道是否存在飞行模式图标。

XCUIElement* airplaneModeIcon = app.windows.otherElements[@"Airplane mode on"]; const bool isAirplaneModeEnabled = airplaneModeIcon.exists;

  • 之后,如果您确实需要将飞行模式设置为开或关,则需要启动设置应用。

XCUIApplication* settings = [[XCUIApplication alloc] initWithBundleIdentifier:@"com.apple.Preferences"];
[settings launch];
XCUIElement* airplaneModeCell = settings.tables.cells[@"Airplane Mode"]; // Do what you have to do with the Cell...