如何在 Xcode 7 的 UI 测试中获取对 TextField 的引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31801091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to obtain reference to TextField in UI Tests in Xcode 7
提问by Dan
I am trying to use UI tests in xcode 7 beta. I have a storyboard with two text fields. Both text fields have outlets and different Restoration IDs. I recorded the test but generated code is quite unreadable and it doesn't work:
我正在尝试在 xcode 7 beta 中使用 UI 测试。我有一个带有两个文本字段的故事板。两个文本字段都有出口和不同的恢复 ID。我记录了测试,但生成的代码非常不可读,而且不起作用:
app.otherElements.containingType(.TextField, identifier:"y").childrenMatchingType(.TextField).elementBoundByIndex(0).typeText("hello")
I also tried the following and will work based on Placeholder text?!?
我还尝试了以下操作,并将根据占位符文本工作?!?
app.textFields["PlaceholderText"].typeText("hello")
What is the right way to obtain a reference to a TextField in UI tests?
在 UI 测试中获取对 TextField 的引用的正确方法是什么?
回答by Ameet Dhas
You need to set accessibility identifier in storyboard for that particular textField. Check the image below :
您需要在情节提要中为该特定文本字段设置可访问性标识符。检查下面的图像:
So you can query textField using accessibility identifier like this :
因此,您可以使用可访问性标识符来查询 textField,如下所示:
let app = XCUIApplication()
app.launch()
let nameTextField = app.textFields["nameTextField"]
nameTextField.tap()
nameTextField.typeText("Hello John")