Wait for a text field to have focus during an XCUITest
I was recently working on some XCUITests and attempting to get them working inside a CI/CD pipeline (Bitrise). It turns out that the virtual machines on Bitrise are a bit underpowered, so many things like building a project & running the simulator can be fairly slow. In one part of my UI test, I tap a text field & begin entering text. The test kept failing on Bitrise but running just fine on my MacBook - that seemed odd.Â
After downloading the xcresult.zip file from Bitrise & opening up several screenshots (did you know that XCUITests automatically save screenshots on failed tests?!) I noticed that the text field never seemed to receive focus after a tap when the test failed. I also read in the documentation for XCUIElement.typeText() that âThe element or a descendant must have keyboard focus; otherwise an error is raisedâ, and I began to realize that the test was attempting to type into the text field when it wasnât active, causing the error. But why did the field not activate like it did on my laptop? Remember that the Bitrise machines are slow đą
After all this, I decided to make some helpers that wait for a text field to be focused before continuing. Hereâs the code:
With those extensions in place, I was able to do something like this in the XCUITest:
The tests began to work perfectly in Bitrise as well as on my MacBook - success at last.Â
Hopefully this was helpful if youâve run into similar UI test issues on slower processors (a typical issue when using cloud virtual machines). Also, if youâre curious about that waitUntilExists() method, Iâll include its code here. The method is extremely helpful when you have elements that take a few seconds to display after network calls, etc, and the chaining syntax is nice too:
See you next time!