user2876886
user2876886

Reputation:

XCUITest: Failed to find Matching Element

I have a close button on a ViewController that is being presented as a ChildViewController. I have this button's accessibility turned on and the accessibility identifier is closeButton. This button does not have any text, it just has an image.

I am attempting to test the presentation and dismissal of this view, but am running into an issue, the XCUITest cannot find the element. My test code is:

app.buttons["PresentChild"].tap()
XCTAssertTrue(app.otherElements["ChildViewController"].exists)
XCTAssertTrue(app.buttons["closeButton"].exists)

The first assert passes, but the second assert fails.

I tried to do the "Record UI Test" to select the button to have it be automatically generated, but then I just get an error saying Failed to find Matching Element. I tried erasing all the content on the simulator and deleting all derived data, and that didn't work either.

Does anyone know a workaround to this obvious bug in Xcode? I can't access the element through the recording, I can't access the element through the accessibility feature, I have zero idea how else to access this button.

Upvotes: 5

Views: 4893

Answers (1)

Confused
Confused

Reputation: 3926

I don't know what is the exact problem with your assertion. But this steps may help you.

  • Put a breakpoint on this line XCTAssertTrue(app.otherElements["ChildViewController"].exists)
  • Now run the test case. When the control reaches this line, type this line in the debugger po XCUIApplication().buttons.debugDescription then tap the enter button
  • It will print all buttons in your current view. Now check whether the "closeButton" exists in that list
  • If it exists, check the identifier name. Make sure that it is "closeButton" (Identifier is case sensitive, also spaces in the identifiers matters)
  • If the identifier is right, try to find out the closeButton's exact view hierarchy. Means, instead of just checking like app.buttons["closeButton"].exists, try like app.navigationBars.otherElements["someName"].buttons["closeButton"].exists

(This seems funny, but giving the exact location path of the element is a good practice, and it will work sometimes)

Note: This error "Failed to find Matching Element" on UI recording will happen if the system can not locate the element with proper identifier while doing action on it. So my guess is, your closeButton does not have proper identifier or it is not added to the current window.

Cheers!!

Upvotes: 3

Related Questions