<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ryan Paterson on Medium]]></title>
        <description><![CDATA[Stories by Ryan Paterson on Medium]]></description>
        <link>https://medium.com/@ryan-paterson?source=rss-416b03c902fe------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*XTDYhFMTTsz5wl1ndgU2vQ.jpeg</url>
            <title>Stories by Ryan Paterson on Medium</title>
            <link>https://medium.com/@ryan-paterson?source=rss-416b03c902fe------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 09:07:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ryan-paterson/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Clean waiting in XCUITest]]></title>
            <link>https://sourcediving.com/clean-waiting-in-xcuitest-43bab495230f?source=rss-416b03c902fe------2</link>
            <guid isPermaLink="false">https://medium.com/p/43bab495230f</guid>
            <dc:creator><![CDATA[Ryan Paterson]]></dc:creator>
            <pubDate>Mon, 15 Feb 2021 12:43:02 GMT</pubDate>
            <atom:updated>2021-02-15T12:43:02.787Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*nfTmmJvkxY9BzCj3D2chgA.png" /></figure><p>At Cookpad Global, we compliment our unit-tests with end-to-end UI-tests. For the iOS automation solution, we currently utilise the XCUITest framework.</p><p>In this post I will share about some of the problems we faced, and how we at Cookpad have been overcoming them to achieve a readable, reliable and maintainable end-to-end UI-test solution using XCUITest.</p><h3>Flaky tests</h3><p>Unreliable tests, also known as a ‘flaky tests’ are typically characterised by a test that can both pass and fail on subsequent executions without any changes to code. Such tests can introduce wastage in the software delivery life-cycle when verifying false positives, maintenance, and re-execution becomes required.</p><h4><strong>Network</strong></h4><p>Perhaps one of the most notable drawbacks of creating an automated end-to-end testing solution is the reliance on external dependencies, in our case for example, a live server environment. An extra flaky factor can be introduced when a test attempts to assert against or interact with UI that is reliant on server content which may arrive late, or not arrive at all.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/462/1*gZlZAGYMyActH4aLGZLYiA.png" /><figcaption>Test failed attempting to tap a recipe not yet returned from the server</figcaption></figure><h4>Animation and interaction delays</h4><p>Other flaky factors such as animations and interaction delays require consideration too. It’s not an uncommon sight when investigating failed tests to see that it was caused by an interaction or assertion that occurred when the app was not quite ready, for e.g. tapping while the element exists, but before it was hittable enough to register the tap.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/462/1*nDjI61Pxk71WhPGDpCPwnA.png" /><figcaption>Test failed attempting to tap an element before the app was properly idle (settled).</figcaption></figure><p>These are just a couple examples of common flaky factors you may encounter when UI-Testing, but they aren’t the only factors. Things such as general assertion timings, test execution order and even bugs in the test framework can also introduce flakiness. However, a shared concept for a range of timing based flaky factors exists that can be utilised to better improve the reliability of tests, waiting.</p><h3>A brief history of waiting in XCUITest</h3><p>There are a few approaches available to wait in XCUITest, I will briefly talk about some of the more common approaches I have seen utilised.</p><p><a href="https://developer.apple.com/documentation/xctest/xcuielement/2879412-waitforexistence">XCUIElement.waitForExistence(timeout:)</a> will wait explicitly to a given timeout, and return a boolean per the element’s existence property.</p><pre>if app.buttons[&quot;identifier&quot;].waitForExistence(timeout: 5) {<br>    // do some stuff <br>}</pre><p><a href="https://developer.apple.com/documentation/xctest/xctestcase/1500569-expectation">XCTestCase.expectation(for:evaluatedWith:handler:</a>) and <a href="https://developer.apple.com/documentation/xctest/xctestcase/1500748-waitforexpectations">XCTestCase.waitForExpectations(timeout:handler:)</a> can be used to wait explicitly for expectations using predicates for example, allowing for more flexibility, and even an optional completion handler to be invoked on success or timeout.</p><pre>expectation(<br>    for: NSPredicate(format: &quot;exists == true&quot;),<br>    evaluatedWith: app.buttons[&quot;identifier&quot;],<br>    handler: .none<br>)<br><br>waitForExpectations(timeout: 5)</pre><p>Xcode 8.3 introduced the <a href="https://developer.apple.com/documentation/xctest/xctwaiter">XCTWaiter</a>class, which expanded on the former and allows us to better handle wait results, and provided the ability to wait for multiple expectations.</p><pre>let expectation = expectation(<br>    for: NSPredicate(format: &quot;exists == true&quot;),<br>    evaluatedWith: app.buttons[&quot;identifier&quot;],<br>    handler: .none<br>)<br><br>let result = XCTWaiter.wait(for: [expectation], timeout: 5.0)<br><br>XCTAssertEqual(result, .completed)</pre><p>Additionally, and perhaps the least optimal solution in most cases, sleeping <a href="https://developer.apple.com/documentation/foundation/thread/1413673-sleep">Thread.sleep(forTimeInterval:)</a>.</p><pre>sleep(10)</pre><h3>Waiting at Cookpad</h3><p>At Cookpad, we wanted an extension method on <a href="https://developer.apple.com/documentation/xctest/xcuielement">XCUIElement</a> similar to <a href="https://developer.apple.com/documentation/xctest/xctestcase/1500748-waitforexpectations">XCTestCase.waitForExpectations(timeout:handler:)</a> to make tests readible, but we also have more expectations to wait for than just existence, and we didn’t want to create multiple methods to do very similar things e.g. waitUntilHittable, waitUntilLabelMatches etc.</p><p>Additionally, we didn’t want to sleep as an expectation might occur before the timeout and we waited too long, or the opposite, and we didnt wait long enough and spent time verifying false positives. As a result, we created a solution utilising take-aways from all of the aforementioned techniques.</p><h4>The base</h4><p>First we created a base <a href="https://developer.apple.com/documentation/xctest/xcuielement">XCUIElement</a> extension method called wait, which takes an expression block returning a boolean, with an <a href="https://developer.apple.com/documentation/xctest/xcuielement">XCUIElement</a> receiver, and we wait for the expression block to be true using <a href="https://developer.apple.com/documentation/xctest/xctwaiter">XCTWaiter</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/548bf947b3db4f8e13625a61a9de5b62/href">https://medium.com/media/548bf947b3db4f8e13625a61a9de5b62/href</a></iframe><p>This allows us to write statements such as:</p><pre>app.buttons[&quot;identifier&quot;].wait(until: { $0.exists })<br>app.buttons[&quot;identifier&quot;].wait(until: { $0.label == &quot;button_text&quot; }<br>app.buttons[&quot;identifier&quot;].wait(until: { button in<br>    button.exists &amp;&amp; button.label == &quot;button_text&quot;<br>})</pre><p>This is much more flexible, but not as readable as we would like.</p><h4>The magic of keypaths</h4><p>Now we have our base wait method, we can create a wrapper with a much cleaner declaration by forming the expression using an <a href="https://developer.apple.com/documentation/xctest/xcuielement">XCUIElement</a> keyPath, and an equatable match value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd577ce45748d41837593bc1f14ae266/href">https://medium.com/media/dd577ce45748d41837593bc1f14ae266/href</a></iframe><p>The keyPath in our case allows us to reference an instance property without neccessarily knowing which one, and using an equatable value allows us to conveniently create expectations against said property.</p><pre>app.buttons[&quot;identifier&quot;].wait(until: \.exists, matches: false)</pre><p>Now this is looking much better in regards to readibility and usability, however, there is still room for improvement!</p><h4>The final touches</h4><p>Implement a new wait method which calls the previous declaration, where matches is always true.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74fd1df600b44343782f9e6fbaedecdd/href">https://medium.com/media/74fd1df600b44343782f9e6fbaedecdd/href</a></iframe><p>This wrapper further improves the readability of our test cases for a use-case we find most common, waiting for an <a href="https://developer.apple.com/documentation/xctest/xcuielement">XCUIElement</a> property to be true.</p><pre>app.buttons[&quot;identifier&quot;].wait(until: \.isSelected)</pre><h3>Usage</h3><p>This approach can be utilised to explicitly wait for a dynamic range of expectations. The following examples are a handful of the more commonly used methods from our UI-test solution.</p><h4>isHittable</h4><pre>app.alerts.buttons.element(boundBy: 1)<br>     .wait(until: \.exists)<br>     .wait(until: \.isHittable)<br>     .tap()</pre><h4>isEnabled</h4><pre>app.button[&quot;identifier&quot;].wait(until: \.isEnabled)</pre><h4>isSelected</h4><pre>app.button[&quot;identifier&quot;].tap()<br>app.button[&quot;identifier&quot;].wait(until: \.isSelected)</pre><h4>Custom properties</h4><pre>app.button[&quot;identifier&quot;].wait(until: \.isDisplayed)</pre><h4>Miscellaneous</h4><pre>app.staticTexts[&quot;identifier&quot;].wait(until: \.label, matches: input)<br>app.tables[&quot;identifier&quot;].wait(until: \.cells.count, matches: 0)</pre><p>The key takeaway from the above examples that I am aiming to demonstrate is the variety. We believe this versitile approach allows us to cleanly and explicitly wait for a wide range of expectations.</p><h3>Preferences</h3><p>The solution we’ve come up with works great for our specific needs and allowed us to cleanly improve the reliability of our tests, but out of the box this may not be the be-all and end-all solution for everyone.</p><p>For example, we return self for the ability to chain methods, but perhaps this isn’t everyone’s cup of tea.</p><pre>app.buttons[&quot;identifier&quot;]<br>    .wait(until: \.isEnabled)<br>    .wait(until: \.label, matches: &quot;button_text&quot;)<br>    .tap()</pre><p>It could alternatively return nothing at all, or perhaps return a boolean per the wait result and have a behaviour more akin to <a href="https://developer.apple.com/documentation/xctest/xcuielement/2879412-waitforexistence">XCUIElement.waitForExistence(timeout:)</a>, for example:</p><pre>if app.buttons.element.wait(until: \.exists) {<br>    // do some stuff <br>}</pre><p>Furthermore, we haven’t concerned ourselves yet with handling the result of the wait beyond success or failure, and we haven’t implemented any completion handlers either, purely for lack of need at the moment. However, these are all within the realms of possibility per the needs of your app/tests.</p><h3>Conclusion</h3><p>We love the readability, flexibility and reliability that such methods provide. This post is just one part of how we achieved readable, reliable (flaky-free) and maintainable end-to-end ui-tests for the iOS platform at Cookpad Global, and we look forward to sharing more about our work in future posts.</p><p>Complete extension (with documentation) available <a href="https://gist.github.com/ryanpato/5f060c5877bce6138aab88759cadc0c8">here</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=43bab495230f" width="1" height="1" alt=""><hr><p><a href="https://sourcediving.com/clean-waiting-in-xcuitest-43bab495230f">Clean waiting in XCUITest</a> was originally published in <a href="https://sourcediving.com/">Source Diving</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>