Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through multiple XCUIElements

Perhaps I misunderstand how the queries and xcuielements work, but I'd like to iterate through the results of a query.

In my app I have the text number sent / number received twice on a screen of my app. I'd like to check that both numbers are the same. For example the following text is on my screen.

10/10
0/0

I use the following code to find these two static text elements

XCUIElement *staticTexts = [self.app.staticTexts elementMatchingPredicate:[NSPredicate predicateWithFormat:@"label contains '/'"]];

This fetches me the two static text elements but I have no way to iterate through the two to check the labels. If there were only one element retrieved there wouldn't be a problem unfortunately there are two. If there's a better way to do this please tell me. I'm new to UI Testing.

like image 664
Biclops Avatar asked Dec 29 '25 20:12

Biclops


1 Answers

The method you are calling, -elementMatchingPredicate: only returns on element. Note that the method name is singular. Trying to call any method on the returned object will result in an exception being thrown.

I suggest taking a slightly different approach to your tests. Since you are writing the test you decide what the first number should be. You can then use that to verify that the second number is the same.

To do so, just set different -accessibilityIdentifiers on each of the labels. Then you can assert each one individually.

XCUIApplication *app = [[XCUIApplication alloc] init];
XCTAssertEqual(app.staticTexts[@"Number Input"].value, @"867-5309");
XCTAssertEqual(app.staticTexts[@"Number Confirm"].value, @"867-5309");
like image 95
Joe Masilotti Avatar answered Jan 01 '26 09:01

Joe Masilotti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!