XCode 7 UI测试 - 无法在搜索栏中键入文本

Ste*_*hen 11 xctest swift xcode7 xcode-ui-testing

以下UI测试代码将成功点击UISearchBar元素.出现软件键盘,搜索栏看起来像是焦点.(即,它会像有人敲击它一样动画)

    let searchBar = XCUIApplication().otherElements["accessibility_label"]

    searchBar.tap()
    searchBar.typeText("search text")
Run Code Online (Sandbox Code Playgroud)

但是,typeText失败:

UI测试失败 - 元素和任何后代都没有键盘焦点.元件:

注意:硬件 - >键盘 - >连接硬件键盘关闭.这解决了文本字段的相同问题,但搜索栏仍然失败.

小智 12

我发现了一些东西

let app = XCUIApplication()
app.tables.searchFields["Search"].tap()
app.searchFields["Search"].typeText("something")
Run Code Online (Sandbox Code Playgroud)

它看起来很奇怪,但它对我有用.


Tob*_*obe 5

它对我有用设置

searchBar.accessibilityTraits = UIAccessibilityTraitSearchField
Run Code Online (Sandbox Code Playgroud)

在视图控制器中,然后在 UI 测试中访问它

let searchfield = app.searchFields.element(boundBy: 0) 
searchfield.typeText("foobar")
Run Code Online (Sandbox Code Playgroud)


Kor*_*tor 3

我成功地使用了以下解决方法:

let firstCell = app.cells.elementBoundByIndex(0)
let start = firstCell.coordinateWithNormalizedOffset(CGVectorMake(0, 0))
let finish = firstCell.coordinateWithNormalizedOffset(CGVectorMake(0, 3))
start.pressForDuration(0, thenDragToCoordinate: finish)

app.searchFields.element.tap()
app.searchFields.element.typeText("search text")
Run Code Online (Sandbox Code Playgroud)

这可以有效地将搜索栏滚动到视图中并点击它来聚焦,然后就typeText()可以使用了。