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

How do I identify control center elements in Xcode UI tests?

我成功地打开了设备的控制中心,但我无法识别按钮,我更需要一个 Wi-Fi。我试过录音机,它被识别为

app.scrollViews.otherElements.scrollViews.otherElements.switches["Wi-Fi"]

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

有人知道这个的解决方案吗?

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

要禁用 wifi,您需要从物理上断开设备与 Internet 的连接,因为无法通过编程方式断开与 wifi 的连接。

使用 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()
}

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

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

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

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

XCUIApplication* settings = [[XCUIApplication alloc] initWithBundleIdentifier:@"com.apple.Preferences"];<br> [设置启动];<br> XCUIElement* airplaneModeCell = settings.tables.cells[@"Airplane Mode"]; // 对 Cell 做你必须做的事...