'Cannot access button in nib file (UI Testing iOS)

I am writing a UITest case for my view. The problem is I cannot access the button in my nib file view. I gave the accessibility identifier to the button as well but still cannot access it Here is the image

I just want to tap on that reject button but accessibility identifier is not working for this. Here is my code for this

app.buttons["CancelRequest"].tap()

CancelRequest is Accessibility identifier. I also tried the below code because button is in a container view but no luck

let ContainerView = app.otherElements["RejectContainerView"]
let RejectButton = ContainerView.buttons.element(boundBy: 0)  
RejectButton.tap()

I also tried to record the screen as well which generated this code

app.buttons["dismiss"].tap()

The weird thing is the recording screen is generating the same code for the cancel button as well as you can see the cancel button in the image I provided



Solution 1:[1]

It worked after disabling the accessibility identifier of button and enabling it again. I think this is a bug it's very weird behavior of XCTEST case

I tried to duplicate the same results with another xib and yes this is a problem in XCTEST case sometimes you cannot access xib buttons you have to disable and enable the accessibility identifier several times and sometimes if that doesn't work then simply do this

sleep(2)
app.buttons["submit"].tap()

Just put sleep before tapping the button this is apple's recommended way really weird

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1