如何在Xcode 7中的UI测试中获取对TextField的引用

Dan*_*Dan 10 xcode swift xcode7 xcode-ui-testing uitest

我正在尝试在xcode 7 beta中使用UI测试.我有一个带有两个文本字段的故事板.两个文本字段都有出口和不同的恢复ID.我记录了测试,但生成的代码是不可读的,它不起作用:

        app.otherElements.containingType(.TextField, identifier:"y").childrenMatchingType(.TextField).elementBoundByIndex(0).typeText("hello")
Run Code Online (Sandbox Code Playgroud)

我也尝试了以下内容,并将根据占位符文本工作?!?

app.textFields["PlaceholderText"].typeText("hello")
Run Code Online (Sandbox Code Playgroud)

在UI测试中获取TextField引用的正确方法是什么?

Ame*_*has 30

您需要在storyboard中为该特定textField设置辅助功能标识符.检查下图:

在此输入图像描述

因此,您可以使用可访问性标识符来查询textField,如下所示:

let app = XCUIApplication()
app.launch()

let nameTextField = app.textFields["nameTextField"]
nameTextField.tap()
nameTextField.typeText("Hello John")
Run Code Online (Sandbox Code Playgroud)