在xcode ui测试执行正在

时间:2016-10-10 14:21:50

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

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

也就是说,当我的执行正在进行时,而不是在某种情况下

i have to off my wifi

我知道我可以使用命令来执行此操作

networksetup -setairportpower en0 on

我在osx中​​编写了一个函数:

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

}

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

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

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

1 个答案:

答案 0 :(得分:4)

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

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