使用带有 UI 测试的 Cucumberish 从故事板加载控制器

Load controller from storyboard using Cucumberish with UI Testing

我正在尝试使用 Cucumberish 框架通过 BDD 实施 UI 测试。 我非常了解功能文件解析系统,并且设法测试了主屏幕的一些 UI 元素。

但是我想在相应屏幕上使用 UI 测试之前从故事板加载任何控制器。

这是我的初始化代码:

@objc class CucumberishSwiftInit: NSObject {
@objc class func CucumberishSwiftInit()
{
    var application : XCUIApplication!
    //A closure that will be executed just before executing any of your features
    beforeStart { () -> Void in
        application = XCUIApplication()
    }
    //A Given step definition
    Given("the app is running") { (args, userInfo) -> Void in
        application.launch()
        let bundle = Bundle.main

        // I double checked the storyboard name and I can access it in my Unit Tests target
        // application crashes here
        let storyboard = UIStoryboard(name: "Main", bundle: bundle)

        // never executed 
        Swift.print("storyboard \(storyboard)")
    }
    let bundle = Bundle(for: CucumberishSwiftInit.self)

    Cucumberish.executeFeatures(inDirectory: "Features", from: bundle, includeTags: nil, excludeTags: ["skip"])
}
}

一些特征文件:

@run @welcome
Feature: Some feature
Some feature desc

Scenario: Can load screen
    Given the app is running
    ...

应用程序在 UIStoryboard init 语句上崩溃,捕获 "NSInvalidArgumentException",“在 NSBundle 包中找不到名为 'Main' 的故事板(已加载)。我不知道为什么因为它正在与我的单元测试一起使用。

您收到的错误是由于您尝试加载的 .storyboard 文件不是您正在 运行ning 的应用程序包的一部分。

发生这种情况的原因是,当您 运行 一个 UI 测试时,您的代码 运行 与您的应用程序不在同一个进程中,它只能与之交互通过 XCUIApplication 代理。 (机制可能略有不同,但这就是要点,不幸的是,我可以提供的文档很少link。

UI 测试是一种不同于 XCTest 的测试方式。无法从 .storyboard 以编程方式加载屏幕实例。

换句话说,您不能在 UI 测试中使用应用程序中的任何代码,而是必须像真实用户一样与其交互,并为屏幕上显示的内容编写断言。