5

How to build a query to get an element that contains another element by static text.

Example: Get the first table cell that contains a label with text "cool cell"

I need it because I have different cells and I want to get a cell that contains a specific text. Note that I'm interested to get the cell because I need to make sure that the cell contains another elements. The goal is to make sure that the cell with the title "I'm a cell" has also a label with text "cool"

iOSGeek
  • 5,115
  • 9
  • 46
  • 74
  • you might have to perform a loop on view's subViews and check its type let say if it contains UITextFields Or label or buttons Or textViews and they have any text add this text and UIObject into array and return. – Abu Ul Hassan Feb 22 '18 at 12:13

3 Answers3

1

Get the first table cell that contains a label with text "cool cell"

How about:

let app = XCUIApplication()
app.cells.containing(.staticText, identifier: "cool cell")

If that's not enough, there's a version of containing that takes NSPredicate:

func containing(_ predicate: NSPredicate) -> XCUIElementQuery
func containing(_ elementType: XCUIElement.ElementType, identifier: String?) -> XCUIElementQuery

Documentation:

Returns a new query for finding elements that contain a descendant matching the specification" which is what you need. [...]

Rudolf Adamkovič
  • 31,030
  • 13
  • 103
  • 118
0

If you are interested in testing Maybe this library can help you.

Durdu
  • 4,649
  • 2
  • 27
  • 47
  • Thank you for your suggestion, but I want to reduce the dependencies my project and use the native testing classes. – iOSGeek Feb 19 '18 at 11:30
0

If you don't want to use external libraries is there anything blocking you form adding a new UI test target and implement a testing class where you create a dummy data source and just initialise an array of cells (made from nibs) and then validate them there?

Durdu
  • 4,649
  • 2
  • 27
  • 47