How to filter from a list of static texts through the name of a label

49 Views Asked by At

This:

let car = app.descendants(matching: .any)["car_view_81"]

Returns:

↪︎Find: Elements matching predicate '"car_view_81" IN identifiers'
  Output: {
    StaticText, 0x153216ec0, {{167.5, 316.7}, {58.0, 19.7}}, identifier: 'car_view_81', label: 'Car'
    Image, 0x153216fe0, {{60.0, 358.3}, {30.0, 30.0}}, identifier: 'car_view_81'
    StaticText, 0x153217100, {{306.8, 365.6}, {22.3, 15.3}}, identifier: 'car_view_81', label: '13%'
    StaticText, 0x153217220, {{59.8, 423.6}, {30.3, 15.3}}, identifier: 'car_view_81', label: 'Draw'
    StaticText, 0x153217340, {{305.5, 423.6}, {25.0, 15.3}}, identifier: 'car_view_81', label: '33%'
    Image, 0x153217460, {{60.0, 474.3}, {30.0, 30.0}}, identifier: 'car_view_81'
    StaticText, 0x153217580, {{305.5, 481.6}, {25.0, 15.3}}, identifier: 'car_view_81', label: '54%'
  }

There's multiple static text elements that's within this view.

I want to filter by label "Draw", to check that this static text exists.

I have tried:

car.staticTexts["Draw"].firstMatch

Which doesn't appear to filter.

I have also tried a predicate ("label == "Draw""), but that also doesn't seem to filter:

let predicate = NSPredicate(format: "label CONTAINS[c] 'Draw'")

let car_view = app.staticTexts["car_view_81"]

return car_view.staticTexts.containing(predicate).element

How can I filter from a list of identifiers that a .descendants function returns please?

Many thanks

2

There are 2 best solutions below

0
On BEST ANSWER
app.staticTexts.matching(identifier: "car_view_81")
               .containing(XCUIElement.ElementType.staticText, identifier: "Draw")
               .element

Solved my problem.

Using .matching for obtaining the XCUIElementQuery of the car_view_81 and then calling the containing filter to extract the "Draw" element.

0
On

car comes back to you as an XCUIElementQuery.

We want to find an element in that XCUIElementQuery.

car.element(...) is a good function for that.

I'd love to call this function:

func element(
  matching elementType: XCUIElement.ElementType,
  identifier: String?
) -> XCUIElement

But I'm a little rusty on XCUITest and don't have any test code with me so I can't know if it will fall back to trying a label if an identifier exists, but doesn't match.

So we're stuck (or at least safest) matching by predicate func element(matching predicate: NSPredicate)

let car = app.descendants(matching: .any)["car_view_81"]
let drawPredicate = NSPredicate(format: "type == 'staticText' AND label == 'Draw'")
return car.element(predicate: drawPredicate)

Ref: XCUIElementQuery docs