gpt4 book ai didi

swift - 如何在 swift UITests 的 XCUIApplication 中设置暗模式?

转载 作者:行者123 更新时间:2023-12-04 12:11:37 30 4
gpt4 key购买 nike

我想知道是否有办法在 swift UITests 项目中为 XCUIApplication 设置暗模式代码。

我需要在同一个测试中以亮模式和暗模式启动应用程序。在方案中将此设置为硬编码值将不起作用,或者从外部入侵模拟器也不起作用(出于性能和可维护性等原因)。

目前我设置启动参数如下:

    let app = XCUIApplication()
var launchArguments: [AnyHashable] = []
launchArguments.append("-AppleLanguages")
launchArguments.append(langCode)
launchArguments.append("-AppleLocale")
launchArguments.append(localeCode)
app.launchArguments = launchArguments
app.launch()

而且效果很好。

如何为 XCUIApplication 实例设置暗模式?

我所做的:
  • 广泛搜索 Apple 开发文档。
  • StackOverflow 只展示了如何在 Xcode 中的方案中对此进行硬编码,或者如何通过杀死模拟器、删除它并破解 plist 值来从外部破解模拟器。

  • 谢谢你的帮助!

    最佳答案

    我也对这个问题感兴趣,因为我正在使用 UI 测试来使用 FaSTLane 截取屏幕截图。目标是能够针对同一目标的不同测试在明暗模式之间切换。

    RobLabs 提供的解决方案在 Xcode 11.4/iOS 13.4 上似乎对我不起作用。我不确定这是否符合您的要求,但我正在使用自定义启动参数,然后在 SceneDelegate 中设置界面样式,仅用于调试版本:

    在您的测试中:

    override func testDarkMode() { // use setUp() to affect all test cases
    app = XCUIApplication()
    app.launchArguments.append("UITestingDarkModeEnabled")
    app.launch()
    }

    在 SceneDelegate.swift 中:
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    (...)

    #if DEBUG
    if CommandLine.arguments.contains("UITestingDarkModeEnabled") {
    window?.overrideUserInterfaceStyle = .dark
    }
    #endif

    (...)
    }

    现在您的测试在黑暗模式下运行。

    关于swift - 如何在 swift UITests 的 XCUIApplication 中设置暗模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59447982/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com