Reputation:
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
Reputation: 3926
I don't know what is the exact problem with your assertion. But this steps may help you.
XCTAssertTrue(app.otherElements["ChildViewController"].exists)
po XCUIApplication().buttons.debugDescription
then tap the enter buttoncloseButton
" exists in that list"closeButton"
(Identifier is case sensitive, also spaces in the identifiers matters)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