Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"] 
like image 393
Alex Avatar asked Dec 03 '15 13:12

Alex


Video Answer


2 Answers

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

like image 170
Kyle Avatar answered Oct 17 '22 06:10

Kyle


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"];
like image 23
James Goe Avatar answered Oct 17 '22 08:10

James Goe