How to access the App Delegate from a UI Test?

这一生的挚爱 提交于 2019-12-22 10:06:53

问题


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

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


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/38084480/how-to-access-the-app-delegate-from-a-ui-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!