Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Dark Mode in XCUIApplication in swift UITests?

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:

  • Extensive search on Apple Development Docs.
  • StackOverflow only shows how to hard-code this in the scheme within Xcode, or how to hack the simulator from the outside by killing the simulator, erasing it, and hacking a plist value.

Thanks for any help!

like image 347
TheJeff Avatar asked Dec 22 '19 20:12

TheJeff


People also ask

How do I restrict dark mode in Swift?

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.

How do you put Iphone simulator in dark mode?

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.

How do I make dark mode in SwiftUI?

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. ...

How do I enable Dark mode in Xcode?

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.

How do I Turn Off dark mode for the user interface?

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:

How do I enable and switch appearance mode?

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.


2 Answers

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.

like image 62
Peter Avatar answered Sep 24 '22 20:09

Peter


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

Update as of Xcode 11.4 Beta

You can now toggle the appearance in the Simulator. This is a great way to test maps and other Dark mode features.

  • From the Simulator Menu Item > Features > Toggle Appearance, or shiftA

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
like image 23
RobLabs Avatar answered Sep 24 '22 20:09

RobLabs