Xcode UI test - UI Testing Failure - Failed to scroll to visible (by AX action) when tap on Search field "Cancel' button
I guess here "Cancel" button returns false
for hittable
property, that is preventing it from tapping.
If you see tap()
in documentation it says
/*!
* Sends a tap event to a hittable point computed for the element.
*/
- (void)tap;
It seems things are broken with XCode 7.1.To keep myself (and u too ;)) unblocked from these issues I wrote a extension on XCUIElement
that allows tap on element even if it is not hittable. Following can help you.
/*Sends a tap event to a hittable/unhittable element.*/
extension XCUIElement {
func forceTapElement() {
if self.hittable {
self.tap()
}
else {
let coordinate: XCUICoordinate = self.coordinateWithNormalizedOffset(CGVectorMake(0.0, 0.0))
coordinate.tap()
}
}
}
Now you can call as
button.forceTapElement()
Update - For swift 3 use following:
extension XCUIElement {
func forceTapElement() {
if self.isHittable {
self.tap()
}
else {
let coordinate: XCUICoordinate = self.coordinate(withNormalizedOffset: CGVector(dx:0.0, dy:0.0))
coordinate.tap()
}
}
}
For me, the root cause was that the objects I wanted to tap
- have been set to hidden (and back)
- have been removed and re-attached
In both cases the isAccessibilityElement
property was false
afterwards. Setting it back to true
fixed it.
This question ranks well for Google queries around the term "Failed to scroll to visible (by AX action) Button". Given the age of the question I was inclined to think this was no longer an issue with the XCUITest framework as the accepted answer suggests.
I found this issue was due to the XCElement
existing, but being hidden behind the software keyboard. The error is emitted by the framework since it is unable to scroll a view that exists into view to be tappable. In my case the button in question was behind the software keyboard sometimes.
I found the iOS Simulator's software keyboard may be toggled off in some cases (eg: on your machine) and toggled on in others (eg: on your CI). In my case I had toggled the software keyboard off on one machine, and by default it was toggled on on others.
Solution: Dismiss the keyboard before attempting to tap buttons that may be behind it.
I found tapping somewhere that explicitly dismissed the keyboard before tapping on the button solved my problem in all environments.
I added add some actions to get the current responder to resignFirstResponder. The views behind my text views will force the first responder to resign, so I tap somewhere just underneath the last text area.
/// The keyboard may be up, dismiss it by tapping just below the password field
let pointBelowPassword = passwordSecureTextField.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 1))
pointBelowPassword.press(forDuration: 0.1)