I am new to this framework. Could you please help me to get the text of multiple elements matches the same matcher on UI.
Asked
Active
Viewed 209 times
0
-
1Hi welcome to SO. Your question is very broad at the moment. Could you post some example code of what you have already tried to do to solve this problem? – eponini Sep 21 '19 at 07:46
1 Answers
1
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)
}

Roman Zakharov
- 2,185
- 8
- 19
-
1A custom matcher is precisely what you need for doing this. However, please make sure that the test is robust as text changing can lead to failures. – gran_profaci Sep 23 '19 at 17:27