Grey Matcher to get text of multiple elements matches the same grey matcher

221 Views Asked by At

I am new to this framework. Could you please help me to get the text of multiple elements matches the same matcher on UI.

1

There are 1 best solutions below

1
Roman Zakharov On

You can get the text of the element using the following function

open class GreyElement {
    var text = ""
}

func grey_getText(_ elementCopy: GreyElement) -> GREYActionBlock {
    return GREYActionBlock.action(withName: "get text",
                              constraints: grey_respondsToSelector(#selector(getter: UILabel.text))) { element,
                                errorOrNil -> Bool in
                                let elementObject = element as? NSObject
                                let text = elementObject?.perform(#selector(getter: UILabel.text),
                                                                  with: nil)?.takeRetainedValue() as? String
                                elementCopy.text = text ?? ""
                                return true
    }
}

And then in your test code:

var label = GreyElement()

for i in 0..<100 {
    EarlGrey.selectElement(...).perform(grey_getText(text))
    XCTAssert(label.count > 10)
}

The XCTest version:

for element in app.staticText[...].allElementsBoundByIndex {
    XCTAssert(element.label.count > 10)
}