如何在Xcode 7的UI测试中获取对TextField的引用 [英] How to obtain reference to TextField in UI Tests in Xcode 7
问题描述
我正在尝试在xcode 7 beta中使用UI测试. 我有一个带有两个文本字段的情节提要.两个文本字段都具有出口和不同的还原ID.我记录了测试,但是生成的代码非常不可读,无法正常工作:
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:
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")
在UI测试中获取对TextField的引用的正确方法是什么?
What is the right way to obtain a reference to a TextField in UI tests?
推荐答案
您需要在情节提要中为该特定textField设置可访问性标识符.查看下面的图片:
You need to set accessibility identifier in storyboard for that particular textField. Check the image below :
因此,您可以使用如下所示的可访问性标识符查询textField:
So you can query textField using accessibility identifier like this :
let app = XCUIApplication()
app.launch()
let nameTextField = app.textFields["nameTextField"]
nameTextField.tap()
nameTextField.typeText("Hello John")
这篇关于如何在Xcode 7的UI测试中获取对TextField的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!