I was wondering if there is a way to set dark mode in-code for the XCUIApplication within a swift UITests project.
I have a need to launch the app in light mode and dark mode in the same test. Setting this in the scheme as a hard coded value will not work, or hacking the simulator from the outside will not work either (for performance & maintainability reasons among others).
Currently I set launch arguments like so:
let app = XCUIApplication()
var launchArguments: [AnyHashable] = []
launchArguments.append("-AppleLanguages")
launchArguments.append(langCode)
launchArguments.append("-AppleLocale")
launchArguments.append(localeCode)
app.launchArguments = launchArguments
app.launch()
And it works great.
How do I set Dark Mode for a XCUIApplication instance?
What I've done:
Thanks for any help!
In macOS, from the Terminal.app, you can issue this command
defaults read NSGlobalDomain AppleInterfaceStyle
Which responds with
Dark
In your XCTestCase
, this should work
func testAppleInterfaceStyleDark() {
let app = XCUIApplication()
var launchArguments: [AnyHashable] = []
launchArguments.append("-AppleInterfaceStyle")
launchArguments.append("Dark")
app.launchArguments = launchArguments as! [String]
app.launch()
}
You can now toggle the appearance in the Simulator. This is a great way to test maps and other Dark mode features.
Simulator supports toggling appearance for iOS simulators (13.0 and later). From within the app select Debug > Toggle Appearance. From the command line use the
simctl ui
subcommand, e.g. to set dark appearance
xcrun simctl ui <device> appearance dark