iOS UI测试:如何获取元素列表

时间:2017-05-02 05:29:08

标签: ios iphone xcode xctest xcode-ui-testing

我在iOS上使用XCUITest进行测试,在搜索测试用例中,我想验证所有搜索建议都以用户输入的字母开头,例如我输入" p&# 34;,搜索建议应该有" ps4","电话"," ps3"等

通过环顾四周,我觉得大多数元素查询方法都会尝试返回单个元素, 例如:      let predicate = NSPredicate(format: "label BEGINSWITH[cd] 'p'")      app.buttons.elementMatchingPredicate(predicate)

虽然在某些情况下,匹配相同条件的元素列表也很重要,有没有办法实现这一点?

1 个答案:

答案 0 :(得分:1)

@nieschumi首先看起来这个任务不适合ui。 那么,为什么你不能使用其中一个功能:

 /*! Returns a new query that applies the specified attributes or predicate to the receiver. The predicate will be evaluated against objects of type id<XCUIElementAttributes>. */
open func matching(_ predicate: NSPredicate) -> XCUIElementQuery

open func matching(_ elementType: XCUIElementType, identifier: String?) -> XCUIElementQuery

open func matching(identifier: String) -> XCUIElementQuery


/*! Returns a new query for finding elements that contain a descendant matching the specification. The predicate will be evaluated against objects of type id<XCUIElementAttributes>. */
open func containing(_ predicate: NSPredicate) -> XCUIElementQuery

open func containing(_ elementType: XCUIElementType, identifier: String?) -> XCUIElementQuery

?而且如果你需要检查查询次数

示例:

 app.buttons.matching(NSPredicate(format: "label == [cd] %@", myVar)).count>1 ? doSmthWithQuery(): doSmthWithElement()