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"]
app.cells.staticTexts["My Text"]
didn't work for you. If the string "My Text" belongs to any cell, the snapshot of your app's accessibility hierarchy would find the string and match it. ThecontainingType:identifier:
method creates anXCUIElementQuery
, and this does not mean theXCUIElement
from this query actually exists in your app. – Strongwilledlet cell = app.tables.cells.containingType(.StaticText, identifier: "My Text")
is creating a XCUIElementQuery, not an XCUIElement, so you're not actually instantiating an element in your app, just creating a query to an element that might not even exist. If on console you dopo app.tables.cells.containingType(.StaticText, identifier: "blah blah")
it will always return an object even if it doesn't exist, and you can't run any assertions on an XCUIElementQuery, or even the .exist or .hittable methods to see if your element is real. – Strongwilled.element
to get the element for the query, and then assertions on that – Vibrioapp.tables.cells.containingType(.StaticText, identifier: "My Text").element.exists
for example. Thank you for helping me understand =) – Strongwilled