nieschumi
nieschumi

Reputation: 561

iOS UI Test: How to get a list of element

I'm testing on iOS with XCUITest, in a search test case, I'd like to verify that all search suggestions begins with the letter a user enter, say if I enter "p", search suggestion should have "ps4", "phone", "ps3" etc.

By looking around I feel like most element query methods will try to return a single element, eg: let predicate = NSPredicate(format: "label BEGINSWITH[cd] 'p'") app.buttons.elementMatchingPredicate(predicate)

while in some cases, a list of elements matching the same condition can also be important, is there any way to achieve this?

Upvotes: 2

Views: 1709

Answers (1)

Che
Che

Reputation: 495

@nieschumi first of all looks like this task is not for ui. so, why can't u use one of functions:

 /*! 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

? And than check count of query if u need

Example:

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

Upvotes: 1

Related Questions