如何从 UI 测试访问 App Delegate? [英] How to access the App Delegate from a UI Test?

查看:25
本文介绍了如何从 UI 测试访问 App Delegate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 UI 测试期间从已启动应用程序的应用程序委托访问特定属性 router,但我不知道这是否可行.我试过了:

I want to access a particular property router from the App Delegate of the launched app during a UI Test, but I can't figure out if this is possible or not. I have tried:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let router = appDelegate.router

但这会导致失败,甚至无法构建.我在我的项目模块上使用 @testable.有什么想法吗?

but this gives a failure and won't even build. I am using @testable on my project module. Any ideas?

推荐答案

Xcode UI 测试的设计使得测试代码只能看到用户可以看到的内容,因此不能使用或检查被测应用程序中的任何对象测试代码.这也是为什么屏幕上的视图被表示为 XCUIElement 对象而不是 UIView 后代的原因.

Xcode UI testing is designed such that the test code can only see what a user can see, so no objects from the application under test can be used or inspected in the test code. This is also why the views on the screen are represented as XCUIElement objects instead of UIView descendants.

UI 测试在与被测应用程序不同的可执行文件中运行.将附加信息从应用传达给 UI 测试的唯一方法是构造一个包含信息的字符串并将其用作元素的可访问性标识符.

The UI tests run in a separate executable from the application under test. The only way to communicate additional information from the app to a UI test is by constructing a string containing the information and using it as the accessibility identifier for an element.

如果您想测试需要从应用程序代码访问对象的内容,您很可能需要编写单元测试而不是 UI 测试.

If you want to test something that requires access to an object from the application code, it is most likely that you need to write a unit test instead of a UI test.

这篇关于如何从 UI 测试访问 App Delegate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆