iosxcuitest

Force app interface style in Xcode UI tests with iOS target to be light or dark


In my UI test I'm trying to force the app's user interface style to be light or dark. On macOS, when the system appearance is light, I can force the app to be dark with this code:

var app = XCUIApplication()
app.launchArguments += ["-AppleInterfaceStyle", "Dark"]
app.launch()

Sadly, this doesn't work with a iOS target, and UIScreen.main.traitCollection.userInterfaceStyle is read-only. Is this not possible?


Solution

  • Scanning through the documentation I see XCUIDevice has an appearance property.

    Try this:

    XCUIDevice.shared.appearance = .dark // or .light
    

    This requires Xcode 14.3 or later according to the documentation.