0

There is viewController with some HITTABLE text "exampleText". When you tap it a modal opens with the same "exampleText"(or even 2 cells with the same staticTexts "exampleText") My intent is to count those "exampleTexts" via countHittableElements

But the problem is that my method does elements search on modal as well as on viewController(which is under that modal) So if a have 2 "exampleTexts" on modal and try to use countHittableElements(name: "exampleTexts"), I always get an extra 1 element and my count fails.

func countHittableElements(name: String) -> Int {
    return filterHittableElements(name: name).count
}

func getElements(name: String) -> XCUIElementQuery {
    let predicate = NSPredicate(format: "label CONTAINS[c] %@", name)
    return application.cells.staticTexts.containing(predicate)
}

func filterHittableElements(name: String) -> [XCUIElement] {
    let all = getElements(name: name).allElementsBoundByAccessibilityElement
    let onlyfilt = all.filter { $0.isHittable }
    return onlyfilt
}

Is there a way to somehow ignore viewController content when a modal is opened above it?

Send
  • 1
  • 1
  • Is this specifically for testing to do with being hittable, or is it just about being visible, or even loaded? – flanker Oct 25 '19 at 14:42
  • being visible or hittable - doesn't matter in this specific test scope – Send Oct 25 '19 at 14:44
  • Never tried from a unit test, but I assume the modal's subViews array would be accessible? Then you could filter by labels and then the specific text and count those? – flanker Oct 25 '19 at 14:47
  • No elements on the modal show when I attempt to query them, I'm surprised you see them. – John Jun 08 '22 at 19:06

1 Answers1

0

If you have access to the code of an app you're testing, you can try to count those element in specific table, like this (first table in the tree):

func getElements(name: String) -> XCUIElementQuery {
    let predicate = NSPredicate(format: "label CONTAINS[c] %@", name)
    return application.tables.element(boundBy: 0).cells.staticTexts.containing(predicate)
}

or

func getElements(name: String) -> XCUIElementQuery {
    let predicate = NSPredicate(format: "label CONTAINS[c] %@", name)
    return application.tables[<table identifier goes here>]cells.staticTexts.containing(predicate)
}

You can check UI tree structure by putting a break point some place in the test and then type in XCode console:

po print(XCUIApplication().debugDescription)
Bacterial
  • 3
  • 3