I am working with UITableViews and I would like to find the cell that corresponds to a control or static text inside the cell.
More generally, a good way to find any parent or sibling of a given element would be great to know.
Right now I'm just looping through cells until I find the correct one, which I would like to avoid. I've tried using app.tables.cells.containingPredicate
, but haven't had any luck.
let pred = NSPredicate { (element, bindings: [String : AnyObject]?) -> Bool in
return element.staticTexts["My Text"].exists
}
let cells = app.tables.cells.containingPredicate(pred)
The element passed to the predicate block is an XCElementSnapshot which has no staticTexts.
EDIT
James is correct, the containingType:identifier: method works great.
In swift, it looks like this
let cell = app.tables.cells.containingType(.StaticText, identifier: "My Text")
Where the identifier in the method signature does NOT correspond to the element's identifier property, rather it is simply the regular way you would access an element by text in brackets.
app.cells.staticTexts["My Text"]
Here's an example of a way I found to get a sibling element. I was trying to get to a price element based on a specified name element where I had no IDs to work with.
I had an xml structured more or less like this:
<pre><code>Other
Other
Cell
Other
Other
StaticText
Other
StaticText, label: 'Name1'
StaticText, label: '$5.00'
Cell
Other
Other
StaticText
Other
StaticText, label: 'Name2'
StaticText, label: '$2.00' </code></pre>
I wanted to get the label text for the price of 'Name1' or 'Name2'.
I used the following to get to the element:
app.cells.containing(nameNSPredicate).children(matching: .staticText).element(matching: priceNSPredicate).firstMatch
Have you tried using containingType
instead of containingPredicate
? It seems to give you exactly what you're looking for. I'm not too familiar with Swift, but in Objective C, it will look like this:
[app.cells containingType:XCUIElementTypeStaticText identifier:@"My Text"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With