Search code examples
iosswiftswift2xcode-ui-testing

Find parent or sibling with Xcode UI testing


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"] 

Solution

  • 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"];