gpt4 book ai didi

swift - 使用带有 UI 测试的 Cucumberish 从 Storyboard加载 Controller

转载 作者:行者123 更新时间:2023-11-28 13:55:33 24 4
gpt4 key购买 nike

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

但是,在相应屏幕上使用 UI 测试之前,我想从 Storyboard加载任何 Controller 。

这是我的初始化代码:

@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(已加载)。我不知道为什么它正在使用我的单元测试。

最佳答案

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

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

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

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

关于swift - 使用带有 UI 测试的 Cucumberish 从 Storyboard加载 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53926214/

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