4

I want to automate how ios App behave while running the XCUITest. When manually executing the test, I turn off the wi fi adapter. How do I do that using Xcode UI test?

thanks

p.s. I found that we can use below command to disable wi fi. But to do this I will need to send my app to background. I need to do this without sending current app to background.

let settingsApp = XCUIApplication(bundleIdentifier: "com.apple.Preferences")

settingsApp.launch()

settingsApp.tables.cells["Airplane Mode"].tap()
Desdenova
  • 5,326
  • 8
  • 37
  • 45
Flashmark
  • 119
  • 2
  • 10

1 Answers1

11

You can re-activate your app afterwards with the new activate() method on XCUIApplication. It will resume your app without restarting it.

    let app = XCUIApplication()
    app.launch()

    let settingsApp = XCUIApplication(bundleIdentifier: "com.apple.Preferences")
    settingsApp.launch()
    settingsApp.tables.cells["Airplane Mode"].tap()

    // Relaunch app without restarting it
    app.activate()
Richard Venable
  • 8,310
  • 3
  • 49
  • 52
  • Additionally, if you want to know if the airplane mode is current enabled before you go to Settings, try `let isAirplaneMode = XCUIApplication(bundleIdentifier: "com.apple.springboard").statusBars.images["Airplane mode on"]` – Ting Yi Shih Jul 22 '21 at 05:43
  • 1
    What's worth mentioning (as of 2022): Airplane mode is only available on real iOS/iPadOS devices, but not on simulators. See https://stackoverflow.com/questions/1614802/simulate-airplane-mode-in-iphone-simulator for more information. – Jochen Holzer Oct 19 '22 at 06:54