Eric Ipsum
Eric Ipsum

Reputation: 745

turn off wifi from device while xcode ui test execution is going on

I am facing a problem in xcode ui test.

That is, while my execution is going on, than in certain condition

i have to off my wifi

I know that i can use the command to do it

networksetup -setairportpower en0 on

I have written a function in osx:

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

}

If i use this in osx, i can turn off wifi.

But i can not use this in my ios app or can not use this in my xcode ui test.

How i can off my wifi while xcode ui testing is ongoing ?

Upvotes: 3

Views: 2897

Answers (1)

joern
joern

Reputation: 27620

With Xcode 9 you can now just tap on the wifi button in the Control Center while running an UITest.

Here is a little helper function that opens the Control Center, taps on the wifi button and closes it again:

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

Just remember that you have to connect your device with a cable when running the test ;-)

Upvotes: 8

Related Questions