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!
To control an interface style for an entire app, you simply set UIUserInterfaceStyle (Appearance) key in your Info. plist file. You can assign it to either Light or Dark value to force a light and dark user interface style. Set Appearance (UIUserInterfaceStyle) key to Light will disable dark mode for an entire app.
Pressing Command + Shift + A will help you switch between light and dark mode in the blink of an eye. You can also get the same result by selecting Features > Toggle Appearance.
SwiftUI Dark Mode — the Easiest Way 1 Create a New Project. Let’s create a new XCode project. ... 2 Create the User Interface. Now replace all the code in ContentView.swift and write the following code on there. ... 3 Creating Colors. ... 4 Creating Color Extensions. ... 5 Updating Foreground and Background Colors. ...
On a device, you can enable Dark Mode by navigating to the Display & Brightness page in the Settings app. However, it’s a lot easier during development to add an option to the Control Centre to quickly switch between dark and light mode: While working in Xcode with the simulator open you might want to use the Environment Overrides window instead.
If you don’t have the time to add support for Dark mode you can simply disable it by adding the UIUserInterfaceStyle to your Info.plist and set it to Light. You can override the user interface style per view controller and set it to light or dark using the following code:
There are multiple ways to enable and switch appearance mode that all have their benefits. Navigate to the Developer page in the Settings app on your simulator and turn on the switch for Dark Appearance: On a device, you can enable Dark Mode by navigating to the Display & Brightness page in the Settings app.
I'm also interested in this question because I'm using UI tests to take screenshots with Fastlane. The goal was to be able to switch between light and dark mode for different tests on the same target.
The solution provided by RobLabs doesn't seem to be working for me on Xcode 11.4 / iOS 13.4. I'm not sure this matches your requirements but I'm using a custom launch argument and then setting the interface style in SceneDelegate, for debug builds only:
In your test:
override func testDarkMode() { // use setUp() to affect all test cases
app = XCUIApplication()
app.launchArguments.append("UITestingDarkModeEnabled")
app.launch()
}
In SceneDelegate.swift:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
(...)
#if DEBUG
if CommandLine.arguments.contains("UITestingDarkModeEnabled") {
window?.overrideUserInterfaceStyle = .dark
}
#endif
(...)
}
And now your test runs in dark mode.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With