在 xcode ui 测试执行正在进行时关闭设备的 wifi

Eri*_*sum 2 macos xcode ios xctest xcode-ui-testing

我在 xcode ui 测试中遇到问题。

也就是说,当我的执行正在进行时,而不是在某些条件下

i have to off my wifi
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用命令来做到这一点

networksetup -setairportpower en0 on
Run Code Online (Sandbox Code Playgroud)

我在 osx 中写了一个函数:

func runTerminalCommand(args: String...) -> Int32 {
let task = NSTask()
task.launchPath = "/usr/bin/env"
task.arguments = args
task.launch()
task.waitUntilExit()
return task.terminationStatus
Run Code Online (Sandbox Code Playgroud)

}

如果我在 osx 中使用它,我可以关闭 wifi。

但是我不能在我的 ios 应用程序中使用它,或者不能在我的 xcode ui 测试中使用它。

我如何在 xcode ui 测试正在进行时关闭我的 wifi?

joe*_*ern 7

使用Xcode 9,您现在可以在运行 UITest 时点击控制中心中的 wifi 按钮。

这是一个小助手功能,可以打开控制中心,点击 wifi 按钮并再次关闭它:

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

    // 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.1))
    coord1.press(forDuration: 0.1, thenDragTo: coord2)

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

    // open your app back again
    springboard.otherElements["com.your.app.identifier"].tap()
}
Run Code Online (Sandbox Code Playgroud)

请记住,在运行测试时,您必须使用电缆连接您的设备;-)

  • 请注意,这仅适用于真实的测试设备,不适用于无法使用 Wifi 按钮的模拟器 (3认同)