Work with SFSafariViewController or WKWebView in Xcode UI Tests
Disclaimer: check comments to this article before reading. Ilya Puchka found the better solution as a workaround.
You can note, that Xcode 9.3 crashes when you record UI Test with its recording tool and tap on SFSafariViewController content.
caught "NSInternalInconsistencyException", "Invalid parameter not satisfying: [descendant _isDescendantOfElement:self]"
That occurs because of bug in Xcode and is very annoying. Because we need to interract with web view, for example, if we want to test Facebook or other social login.
It’s interesting, that during debug, you can debugPrint
all buttons and fields of a web view without any problem. But you cannot tap
on button or typeText
into text field because of NSInternalInconsistencyException.
For successful testing I needed few things from web view elements:
- check if element exists
- wait for element to appear
- tap on button
- clear text in text field
- type text into text field
Having in mind, that we can debugPrint
elements, we can try this crazy solution: parse debugDescription
of element, take its frame from that and tap XCUIApplication
by coordinate. Lets code that:
The next problem is that we cannot typeText
into text field. But that cannot stop us, right? We can tap on location, so we can paste a text via copy/paste menu. Also we can clear current text in a text field with “Select All” and “Cut”.
Let’s try.
That’s all. Now we can test many things inside the web views. That should work for SFSafariViewController
and, I hope, with WKWebView
or UIWebView
(I didn’t test that).
All code in one file: https://gist.github.com/pilot34/09d692f74d4052670f3bae77dd745889