Work with SFSafariViewController or WKWebView in Xcode UI Tests

Gleb Tarasov
2 min readApr 24, 2018

--

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.

Crash in Xcode after tap on SFSafariViewController
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:

Waiting for element to appear
Tap on element and check if element exists

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.

Type text into text field

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).

--

--