1

I am getting started with UI testing with Xcode and I am trying to check for the value of a UITextField. I can easily do this by

XCTAssertTrue(app.staticTexts[@"value of string"].exists);

I do, however, want to do the very same thing with dynamic content. Ideally, I would need something like

NSString* requiredValue = "computed string";
XCTAssertTrue(app.staticTexts[requiredValue].exists);

How can I implement this? I would greatly appreciated any hint or link to good resources that might answer this question.

1 Answer 1

1

You can reference the label via its accessibilityIdentifier instead of its content/value.

First set the identifier in your production code. This will also only show up during testing, not even for users with accessibility enabled.

UILabel *label = [[UILabel alloc] init];
label.accessibilityIdentifier = @"Username";
label.text = @"can be anything"; // just to prove the point

Then in your tests you can reference it.

XCTAssert(app.staticTexts[@"Username"].exists);

Here are a few references I've written for help with UI Testing:

3
  • That's pretty cool! How can I then access the text from the XCUIElementQuery? Jan 15, 2016 at 14:16
  • @SebastianHojas: XCTAssertEqual(app.staticTexts[@"Username"].label, @"can be anything"); Jan 15, 2016 at 14:32
  • Ah perfect, that's what I was exactly looking for. Thanks! Jan 15, 2016 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.