swiftxcuitestxcuielement

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


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


Solution

  • 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.