<?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 Tomas Camin on Medium]]></title>
        <description><![CDATA[Stories by Tomas Camin on Medium]]></description>
        <link>https://medium.com/@t.camin?source=rss-e7853fe548e2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*7fC2Spb8lg3UM4NgU2tzKg.jpeg</url>
            <title>Stories by Tomas Camin on Medium</title>
            <link>https://medium.com/@t.camin?source=rss-e7853fe548e2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 08:10:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@t.camin/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[Testing push notifications with UI tests]]></title>
            <link>https://medium.com/@t.camin/testing-push-notifications-with-ui-tests-e9c9ccd8aa9f?source=rss-e7853fe548e2------2</link>
            <guid isPermaLink="false">https://medium.com/p/e9c9ccd8aa9f</guid>
            <category><![CDATA[push-notification]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[xcuitest]]></category>
            <dc:creator><![CDATA[Tomas Camin]]></dc:creator>
            <pubDate>Fri, 07 Feb 2020 09:47:27 GMT</pubDate>
            <atom:updated>2020-02-13T08:56:10.247Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>My name is Tomas and I work for Subito.it the leading classified marketplace in Italy. We have millions of daily active users and we use UI tests extensively to guarantee quality of our app and frequent submissions to the AppStore.</blockquote><p>The number of new features listed in Xcode’s 11.4 release notes is significant, but there’s an area that seems to have received a healthy dose of love from Apple’s engineers: the XCTest framework and more specifically the UI testing part of it.</p><p>In this post I’ll cover one long overdue feature that is the ability to send push notifications to the simulator and how this can it be done directly from within your testing code using an additional tool.</p><p>Apple added the ability to send a push notification by invoking the following command from your terminal:</p><pre>$ xcrun simctl push &lt;device&gt; com.yourapp.bundleid SamplePush.json</pre><p>This is really nice but can’t be integrated directly in testing code since you can&#39;t invoke terminal command from there. To workaround this, and similar problems, we developed a library, <a href="https://github.com/Subito-it/SBTUITestTunnelHost">SBTUITestTunnelHost</a>, that allows to access the mac host from within UI testing code. The library integrates easily with CocoaPods and comes with a Mac companion app that enables among other things to execute shell commands. For the sake of the demo project we are interested in the <em>executeCommand </em>method which synchronously sends commands to the mac running the simulator.</p><p>The sample application does just one super fancy thing, it show an alert with the title, body and 2 custom keys taken from the push notification payload.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nU35-5c5jVXq5fRtWxq6sQ.png" /></figure><p>We will write a test that verified that the alert is properly shown.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mX7aNGamrBuvABYaaYV19A.png" /></figure><p>Let&#39;s see what we did here:</p><ol><li>Launch the app <em>[lines 6–7]</em></li><li>We request the push notification permission if needed <em>[lines 9–10]</em></li><li>Exit from the app (push notifications are shown only when the app is in background*), by activating the springboard (Apple&#39;s app that application that manages the home screen) <em>[lines 12–14]</em></li><li>Send the push <em>[lines 16–20]</em></li><li>Tap on the push when it arrives <em>[lines 22, 24]</em></li><li>Assert that we landed in our app with the alert <em>[lines 25, 27]</em></li></ol><h4>Springboard</h4><p>One powerful feature of UI tests is that you&#39;re allowed to interact with any app in the system provided that you know the <em>bundle identifier </em>of the app. You can interact with Safari, Mail.app, Settings and anything else.</p><p>In our test we&#39;ll need to interact with Springboard which is responsible to present the push notification alerts. Note how easy it is to wait for the existence of the push notification and tap on it.</p><h4>Push delivery</h4><p>Push delivery is done leveraging SBTUITestTunnelHost (⚠️ mac companion app must be running!) which will allow us to call <em>xcrun simctl push </em>from the mac host terminal.</p><p>The push command requires the device identifier of the simulator which can be extracted with a nifty trick from the application&#39;s main bundle url, as you can see in the <em>deviceIdentifier() </em>method below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FgCvZuy2S9R5GDBh-rAN2Q.png" /></figure><p>We also require a push json file we will generate that from a codable model.</p><h4>Push notification model</h4><p>This codable model represents a simple push notification payload which exactly matches the one you would send using Apple&#39;s APNs gateway</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_GXMZpyN3fxeKpNKq0c5Eg.png" /></figure><p>When delivering the push notification we instantiate a <em>PushNotification </em>serialise it to a json (we use a unique random filename to avoid collision should tests be run in parallel), send the push and clean up the file from filesystem.</p><h3>🎉 t<strong>hat&#39;s it!</strong></h3><p>This few code snippet show how easily it is possible to test push notifications with the exact same payload you would sent to Apple&#39;s APNs gateway. We also saw how useful the SBTUITestTunnelHost can be when combined with the functionalities offered by Xcode 11.4.</p><p>See the full code of the sample application here: <a href="https://github.com/tcamin/PushnotificationUITestSample">https://github.com/tcamin/PushnotificationUITestSample</a></p><h3>Additional UI Testing resources</h3><p>If you&#39;re into UI testing make sure to check the following open source resources:</p><ul><li><a href="https://github.com/Subito-it/SBTUITestTunnel">SBTUITestTunnel</a> a library to enhance UI testing by allowing network mocking and other sorceries</li><li><a href="https://github.com/Subito-it/SBTUITestTunnelHost">SBTUITestTunnelHost</a> a library allowing to access the mac host from within UI testing code</li><li><a href="https://github.com/Subito-it/Cachi">Cachi</a> present UI testing results in a web interface</li><li><a href="https://github.com/Subito-it/Mendoza">Mendoza</a> parallelise UI tests by dispatching them on multiple physical machine</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e9c9ccd8aa9f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Debugging prebuilt frameworks]]></title>
            <link>https://medium.com/@t.camin/debugging-prebuilt-frameworks-c9f52d42600b?source=rss-e7853fe548e2------2</link>
            <guid isPermaLink="false">https://medium.com/p/c9f52d42600b</guid>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[framework]]></category>
            <category><![CDATA[lldb]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Tomas Camin]]></dc:creator>
            <pubDate>Fri, 01 Jun 2018 20:30:34 GMT</pubDate>
            <atom:updated>2018-06-01T20:36:51.425Z</atom:updated>
            <content:encoded><![CDATA[<p>This is a short resume of how I solved a weird need we had and discovered the hidden powers of lldb’s advanced <a href="https://lldb.llvm.org/lldb-gdb.html">commands</a>.</p><p>When talking about frameworks in iOS/Mac development one usually thinks about a set of source code files that get built in either a static or dynamic library eventually embedding some additional resources. There are a lot of well known tools like <a href="https://github.com/CocoaPods/CocoaPods">CocoaPods</a> and <a href="https://github.com/Carthage/Carthage">Carthage</a> that do that for you, so nothing new here.</p><p>Recently we’ve been facing something that might be seen as the other way around: in our project there was a framework, we had the source files that generated that framework in another location (outside of our project) and we wanted to be able to step-into the framework classes using the debugger.</p><p>Why on earth is that?</p><p>To optimize build times of our project we have a particular flow where we build frameworks and commit those in our repo instead of the source files. While this has some drawback that might be worth discussing in another post, we’re really happy with the outcome because we cutted down build times down by more than a third, from about 7 minutes to approximately 2 minutes. Additionally splitting our app in frameworks forced us to to rethink the whole architecture clearly isolating our dependencies, a nice bonus!</p><p>The frameworks we build ship with <a href="http://www.dwarfstd.org/doc/DWARF4.pdf">DWARF debug informations</a> that can be parsed and converted to something human readable by using dwarfdump. The tool is really super fun to use and the amount of things that can be extracted is mind-blowing. The DWARF debug informations are structured in sections, one for every compilation unit, and by invoking dwarfdump --debug-info you will get all the informations stored in the <em>.debug_info </em>sections. These contain, among other things, the match between symbols and the precise location in the source code file like shown in the screenshot below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xzazk_AVTEEaobOi9W9Uuw.png" /><figcaption>example output of dwarfdump</figcaption></figure><p>Notice that paths to source files are absolute, meaning that the file might even not exists (think of the case where the framework was built on a different machine).</p><p>Luckily for us we‘re building frameworks on different machines but always from the same path <em>/tmp/SubitoFrameworksBuilder</em> and thanks to this assumption it’s just a matter of remapping paths. Can this be done? Yes, and it turns out that this is really easy using lldb’s following command:</p><p>settings set target.source-map &#39;&lt;path_at_compilation_time&gt;&#39; &#39;&lt;current_source_code_path&gt;&#39;</p><p>This is kind of tedious to write at every launch of the debugger, but the good news is that there is a way to automatically execute this every time we launch our app by adding the above line to ~/.lldbinit-Xcode. It would be nice to be able to do this per project and not per user but unfortunately there’s an <a href="https://openradar.appspot.com/23671344">open radar</a> sitting there for some time.</p><p>Each line in ~/.lldbinit-Xcode will be execute by Xcode allowing you to define aliases and custom commands. It’s worth checking out <a href="https://github.com/facebook/chisel">something really powerful</a> that has been published by Facebook.</p><p>So to recap: with a single lldb command we’re now able to use the debugger with our prebuilt frameworks, having source code files somewhere around on our filesystem.</p><p>😎</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c9f52d42600b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Was UI Testing meant for developers?]]></title>
            <link>https://medium.com/@t.camin/was-ui-testing-meant-for-developers-821626c0e166?source=rss-e7853fe548e2------2</link>
            <guid isPermaLink="false">https://medium.com/p/821626c0e166</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[ui-testing]]></category>
            <category><![CDATA[qa]]></category>
            <dc:creator><![CDATA[Tomas Camin]]></dc:creator>
            <pubDate>Sun, 27 May 2018 17:56:30 GMT</pubDate>
            <atom:updated>2018-05-30T13:01:25.449Z</atom:updated>
            <content:encoded><![CDATA[<p>When talking about Apple’s UI Testing one generally ends up discussing about its limitation and questioning its effectiveness. While doing exactly that with a colleague at a conference we were both attending, something really interesting came out. He shared a weird rejection on an app he recently submitted due to a completely unexpected crash that Apple’s reviewer were experiencing at launch. The app was clearly not crashing on his side, but upon further inspecting the crash logs it turned out that there was something related to XC(UI)Test classes. And indeed trying to run the app from a UI Testing target was making the app crash, bingo! The issue was easily fixed and was caused by some misbehaviour when interacting with UICollectionViews from a UI Testing target. He suggested that Apple might be using UI Testing to do some kind of automated pre-human test on the application to optimize the review process. And this of course would make totally sense.</p><p>Later that day I had this suggestive theory: what if the UI testing framework wasn’t actually meant for developers in the first place but instead was a tool for Apple’s internal needs? That would explain the choice for a pure black-box testing solution which works pretty well when it comes to perform high level verifications on an application without any knowledge of its underlying implementation (test any app), but makes it challenging to write test that precisely cover all the possible flows (test your own app).</p><p>Whether this is true or false being able to interact and test any application is probably one of the most powerful features which is available (also) to developers since Xcode 9.</p><p>You just need to instantiate a new XCUIApplication using init(bundleIdentifier:) to be able to interact with the application that matches the provided bundle identifier.</p><p>This allows for example to interact with Apple’s own apps like settings, mail, calendar or any other app installed on your device. You can push this even further and instantiate a XCUIApplication passing <em>springboard</em>’s bundle identifier. This will allow to interact with system alerts (as an alternative method to the more appropriate XCTestCase’s <a href="https://developer.apple.com/documentation/xctest/xctestcase/1496273-adduiinterruptionmonitor">addUIInterruptionMonitor(description:handler:)</a> and even push notifications.</p><p>For the sake of the example we’re leveraging local notifications, but this works with remote ones as well.</p><p>Here’s the code of the app:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b0f53672692763ae67f2ef1cdd306eb/href">https://medium.com/media/1b0f53672692763ae67f2ef1cdd306eb/href</a></iframe><p>We’re scheduling a simple local notification when we send the app to background. Nothing fancy.</p><p>Next let’s try to write a UI test to catch the notification when it occurs:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ea58fc60dbe2f1d265d9539cbf44b34/href">https://medium.com/media/0ea58fc60dbe2f1d265d9539cbf44b34/href</a></iframe><p>We’re using a NSPredicate to look for an XCUIElement.ElementType == .other that has a label beginning with <em>PUSHNOTIFICATIONS</em>. While investigating I was somehow expecting to find single XCUIElement.ElementType == .staticText matching the notification&#39;s title and body but it turns out that instead there’s a single .other element with a value that corresponds to what VoiceOver will read if active.</p><p>The above example is really an edge-case usage scenario but shows how powerful and simple Apple’s UI Testing framework actually is. In combination with <a href="https://github.com/Subito-it/SBTUITestTunnel">SBTUITestTunnel</a> you can even overcome the black-box limitations making XCTest the first choice for any of your tests.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=821626c0e166" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Parallelizing UI Tests]]></title>
            <link>https://medium.com/@t.camin/parallelizing-ui-tests-28c16000f141?source=rss-e7853fe548e2------2</link>
            <guid isPermaLink="false">https://medium.com/p/28c16000f141</guid>
            <category><![CDATA[ui-testing]]></category>
            <category><![CDATA[xcode-9]]></category>
            <category><![CDATA[xctest]]></category>
            <dc:creator><![CDATA[Tomas Camin]]></dc:creator>
            <pubDate>Fri, 09 Jun 2017 13:14:56 GMT</pubDate>
            <atom:updated>2017-06-09T16:31:18.523Z</atom:updated>
            <content:encoded><![CDATA[<p>With regards to testing, one of the most interesting new features presented at the WWDC is by far the possibility to run multiple concurrent simulators which in combination to the xcodebuild enhancements allows to run test sessions in parallel. There’s no need to remark that this is a turning point development when it comes to UI Tests, which suffer to become appealing especially because (<a href="https://medium.com/@t.camin/a-year-of-ui-testing-with-xctest-5e%E2%80%A6">among other things</a>) of their slowness.</p><p>By running in parallel you can not only execute the same tests on different devices simultaneously, but also different tests on the same device. Say you have 10 tests suites, you can split them running 5 on “iPhone 6 simulator 1” and 5 on “iPhone 6 simulator 2”.</p><p>If your wondering how many simulators you can run at once you’ll be amazed with what xcodebuild tells you in the command line logs:</p><pre>Running tests concurrently on multiple destinations. <strong>Maximum concurrent test device destinations is unlimited</strong></pre><p>Mind-blowing.</p><h3>Steps involved</h3><p>To see how much faster this really gets I tried parallelizing <a href="https://github.com/Subito-it/SBTUITestTunnel">SBTUITestTunnel</a>’s UI test.</p><p>The steps involved are pretty simple:</p><ul><li>Create N additional ui test target and associated scheme</li><li>Split your test cases among the N targets</li><li>Create N copies of the simulator you want your tests to be run</li><li>Pass the simulator’s ids to the destination parameter of xcodebuild</li></ul><h3><strong>An example</strong></h3><h4>Test targets and splitting</h4><p>For simplicity let’s start with N=2 and let’s say you have 4 files with UI tests cases:</p><pre>file1UITests.swift → UI test target 1<br>file2UITests.swift → UI test target 1<br>file3UITests.swift → UI test target 1<br>file4UITests.swift → UI test target 1</pre><p>You create a secon UI test target and split them evenly</p><pre>file1UITests.swift → UI test target 1<br>file2UITests.swift → UI test target 1<br>file3UITests.swift → UI test target 2<br>file4UITests.swift → UI test target 2</pre><h4>Simulators cloning</h4><p>Now, for each simulator you want the tests to run create a copy (<em>Window</em> → <em>Devices and Simulators </em>→ <em>Create a new Simulator</em>). So if you want to test on iPhone 6 you’ll create a second iPhone 6 simulator.</p><h4>xcodebuild</h4><p>Invoke xcodebuild using the new <em>-destination</em> parameter</p><pre>xcodebuild<br>  -workspace SBTUITestTunnel.xcworkspace<br>  -scheme SBTUITestTunnel_UITests<br>  -sdk iphonesimulator<br>  -destination &#39;platform=iOS Simulator,id=&lt;&lt;IPHONE_SIM_1&gt;&gt;&#39;<br>  -destination &#39;platform=iOS Simulator,id=&lt;&lt;IPAD_SIM_1&gt;&gt;&#39;<br>  test &amp;<br>xcodebuild<br>  -workspace SBTUITestTunnel.xcworkspace<br>  -scheme SBTUITestTunnel_UITests2<br>  -sdk iphonesimulator<br>  -destination &#39;platform=iOS Simulator,id=&lt;&lt;IPHONE_SIM_2&gt;&gt;&#39;<br>  -destination &#39;platform=iOS Simulator,id=&lt;&lt;IPAD_SIM_2&gt;&gt;&#39;<br>  test &amp;</pre><p>This will happen</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FRx_NukjC-bU%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DRx_NukjC-bU&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FRx_NukjC-bU%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/31eb0a6924ba0fc0831b787e46c24d7f/href">https://medium.com/media/31eb0a6924ba0fc0831b787e46c24d7f/href</a></iframe><h3>Benchmarks</h3><p>The initial results are very promising even with the test cases of the library which are all rather short.</p><p>SBTUITestTunnel tests on iPhone and iPad complete in <strong>32m</strong> with Xcode 8 and just a bit under <strong>&lt;10m</strong> with the new xcodebuild (splitting test over 2 targets) running parallel tasks.</p><h3>Further improvements</h3><p>Yes, leveraging XCTest’s new <a href="https://developer.apple.com/documentation/xctest/xcuielementtypequeryprovider/2902250-firstmatch">firstMatch</a> API we can expect even faster results. I’m gonna investigate this area soon and post a new blog with new benchmarks. Stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=28c16000f141" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A year of UI Testing with XCTest]]></title>
            <link>https://medium.com/@t.camin/a-year-of-ui-testing-with-xctest-5e20406b5411?source=rss-e7853fe548e2------2</link>
            <guid isPermaLink="false">https://medium.com/p/5e20406b5411</guid>
            <category><![CDATA[software-testing]]></category>
            <dc:creator><![CDATA[Tomas Camin]]></dc:creator>
            <pubDate>Fri, 19 May 2017 15:15:54 GMT</pubDate>
            <atom:updated>2017-05-24T16:08:49.735Z</atom:updated>
            <content:encoded><![CDATA[<p>If you ask around you’ll quickly realize that UI tests don’t have an enviable reputation in the iOS developer community. “Difficult to write”, “Slow”, “Hard to maintain”, “Don’t work” are complaints you hear a lot. While to some extent part of these are genuine, truth is that there’s no viable alternative when working on medium/large scale projects.<br>Do you submit your app to the AppStore relying solely on the result of unit tests? It is likely you don’t and instead precede submission with a set of tedious and time-consuming manual tests. This was our release flow for a rather long time to the point that manual testing became unsustainable, making some kind of automation mandatory.</p><p>We decided to give Apple’s XCTest framework a try and a year (and a lot of sweat) later we managed to reduce time wasted on manual tests to a bare minimum, getting pretty close to the continuous delivery dream.</p><p>Today we have 220 test cases on iPhone, 214 on iPad which run respectively in ~2h and ~2h30m (iPad execution is slower). The development team took in charge the task of writing UI tests (along with units) greatly increasing the quality of the first development iteration outcome. As a result we achieved a happier team that is able to submit updates quicker and with greater confidence.</p><h3>The good</h3><p>If you’re still skeptic focus for a moment on the bright sides of UI tests and you’ll realize there is indeed a lot of potential:</p><ul><li>Interact with your app exactly as the user will do (isn’t that exactly what you want?)</li><li>Forget about the application’s implementation, which allows thoughtless refactorings</li><li>Test a lot with short and readable code where acceptance criteria can be easily expressed</li><li>Lift yourself from the pain of manual testing (isn’t that alone enough?)</li></ul><h3>The bad</h3><p>There are of course some downsides and limitations but you’ll see how we worked them out</p><ul><li>Unflexible. UI tests run in a separate process that has no access to the testing application’s code. This makes it tricky to do things like mocking or data injection</li><li>Slow. You don’t run them locally but rather on a continuous integration environment, using xcodebuild / fastlane. However… ⤸</li><li>Results. It can get pretty difficult to understand what went wrong from the test logs</li></ul><h3>Making tests flexible</h3><p>By far the most discouraging issue when approaching UI testing is its apparent inflexibility. You quickly realize that the main issue is that the test process has no access to the application’s internal state. Doing something trivial like checking that a setting is properly stored in NSUserDefaults can get close to impossible.</p><p>We managed to workaround this by developing <a href="https://github.com/Subito-it/SBTUITestTunnel">SBTUITestTunnel</a> which adds the missing link between test and application target. The idea is very simple: when running tests, a web server is instantiated on the app which receives a set of requests from the testing target. The tool sits on top of XCTest extending its functionality in a neat and usable fashion allowing to stub and monitor network requests, inject data to NSUserDefaults and Keychain, download/upload files from/to the app’s sandbox and much more.</p><p>Let’s see for example how easy is it to stub a specific network request:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/02bab6bf1f88d97120c36cbcdc63014b/href">https://medium.com/media/02bab6bf1f88d97120c36cbcdc63014b/href</a></iframe><p>you can also retrieve and upload UserDefaults:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c35aac83efc86640db4efdd3057f02b/href">https://medium.com/media/8c35aac83efc86640db4efdd3057f02b/href</a></iframe><p>These are very simple examples that show some of the capabilities of the testing framework we developed. A lot more can be achieved making it possible to write very complex, yet readable, tests.</p><p>A real life example of what one of our ui tests look like is the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/523b24eba6d9a28b8dfae13bbd7f0564/href">https://medium.com/media/523b24eba6d9a28b8dfae13bbd7f0564/href</a></iframe><h3>Adapting workflow to UI testing</h3><p>It’s a fact that it takes time to execute a full UI test suite, this is however not a problem per se if you adapt your testing workflow appropriately. Our tests today take ~4h30m to execute running overnight (using fastlane / xcodebuild) on a single continuous integration machine. This daily outcome is all we need to get an effective feedback of our latest changes.</p><p>On the other hand we work locally when developing new tests, as running few of them takes an acceptable amount of time.</p><h3>xcodebuild, screencasting and finding what went south</h3><p>We chose xcodebuild for running tests since it allows greater flexibility of integration with our development pipeline and opens up for unique features which would be not possible otherwise. Among others we were able to add a screencasting job 😎 to the whole testing session as shown below.</p><p>The downside of using xcodebuild is the massive amount of hard-to-read logging output that is produced which makes it challenging to understand why a particular test is failing. A similar problem can be experienced in Xcode where there is a lot of clicking involved before having the picture of what eventually failed.</p><p>Rethinking how we would want tests results to be summarized we built another custom tool, <a href="https://github.com/Subito-it/sbtuitestbrowser">sbtuitestbrowser</a>, that parses and presents results in a simple (yet effective) web interface. Written in Swift, of course 😉.</p><p>The home shows the latest testing sessions, testing classes and single tests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y2s6a-XV1EhvQFtnZAso5A.gif" /></figure><p>Each test has the list of actions that were performed, the same you’re used in Xcode. You can enable/disable screenshots or rely on the screencast that is synched with the actions: tap on an action and the video jumps to that point of the test.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FJxCfsvwjHZU&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DJxCfsvwjHZU&amp;image=http%3A%2F%2Fi.ytimg.com%2Fvi%2FJxCfsvwjHZU%2Fhqdefault.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/a176c43b10912815898778ca1e90895b/href">https://medium.com/media/a176c43b10912815898778ca1e90895b/href</a></iframe><h3><strong>Specifying environments/devices</strong></h3><p>Along with the tools described so far we’ve been exploring other ways to further enhance our testing experience. Having different backend environments (development, integration, pre-production, production) we needed a way to easily specify which tests should run on which environments. We don’t want certain tests to run in production, i.e. replying to a real user with a test message.</p><p>By swizzling the XCTest framework and by defining a set of protocols used to «decorate» XCTest classes we made it possible to specify on which devices and environment a test cases should run. Protocols are divided in two categories: environments and devices.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cdbdece6cdd8acdd4e48f1364cd787e8/href">https://medium.com/media/cdbdece6cdd8acdd4e48f1364cd787e8/href</a></iframe><p>All it takes is adding conformance to your XCTestCase class, like in the example below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fec6a17b3aa716c29b0782090ba4a937/href">https://medium.com/media/fec6a17b3aa716c29b0782090ba4a937/href</a></iframe><p>These 3 test cases will be executed independently from the device (<em>SBTTestOnAnyDevice</em>) and environment (<em>SBTTestOnAnyEnvironment</em>) the app is running.</p><p>The implementation of this functionality has been tailored to our specific app configuration (with regards to how we are specifying the running environment), but we are planning to make something reusable available soon.</p><h3>What‘s next?</h3><p>There are scenarios, like for example a broad refactoring (i.e. changing the network layer), where you want to verify the entire app’s behaviour before pushing your changes to the main branch. In these cases a quicker feedback might be required which you can achieve running tests in parallel. We’ll work on expanding our continuous integration environment allowing to run tests in parallel that will provide a quicker feedback.</p><h3>Conclusions</h3><p>We expanded the UI testing framework provided by Apple building additional tools that helped us to reduce manual testing and increase confidence in our code.</p><ul><li><a href="https://github.com/Subito-it/SBTUITestTunnel">SBTUITestTunnel</a>: add a link between app and test target allowing to write much more sophisticated tests</li><li><a href="https://github.com/Subito-it/sbtuitestbrowser">sbtuitestbrowser</a>: parse xcodebuilds test results and make them accessible via web</li><li>A way of marking test cases run only on specific environment/device</li></ul><p>It’s definitely worth giving UI tests a try if you didn’t yet. If you did, check out our tools, we would really appreciate your feedback!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5e20406b5411" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Preprocessor macros in Swift]]></title>
            <link>https://medium.com/@t.camin/preprocessor-macros-in-swift-d73ac65ac11c?source=rss-e7853fe548e2------2</link>
            <guid isPermaLink="false">https://medium.com/p/d73ac65ac11c</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Tomas Camin]]></dc:creator>
            <pubDate>Tue, 29 Nov 2016 15:28:02 GMT</pubDate>
            <atom:updated>2016-11-29T16:03:24.601Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Hang on. There is no such thing, the Swift compiler has no preprocessor.</em></p><p>While that’s true, if you’re working on a Xcode project started in the ancient Objective-C era, there’s a good chance that you’ve been relying on <strong>Preprocessor Macros</strong> to manage build configuration specific flags that conditionally compile blocks of code. This works so nice that you probably more than once started naively typing #if MY_FLAG in you shiny new Swift code. Well that works (in the sense that it compiles) but it does not what you would expect because MY_FLAG is not defined in Swift!</p><p>🔥🔥🔥 (guess what happens?)</p><p>You probably <a href="https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-ID31">read</a> that Swift does include its own build-time configuration options, however keeping them manually aligned with the preprocessor macros is kind of tedious. I wrote a very simple Ruby script to copy preprocessor defines in Swift’s <em>Other Swift Flags </em>that we launch as a <em>Run Script</em> in the target’s <em>Build Phases</em>. It’s bare bone so it might not apply to all scenarios, but I guess it could be useful for others out there.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d90fa5a517d9d7e774b1c85f4acb3ae7/href">https://medium.com/media/d90fa5a517d9d7e774b1c85f4acb3ae7/href</a></iframe><p>The script uses the awesome <a href="https://github.com/CocoaPods/Xcodeproj">CocoaPods/Xcodeproj</a> to edit Xcode’s .pbxproj and simply rewrites the OTHER_SWIFT_FLAGS. We added a sentinel define, THESEWILLBEOVERWRITTENBYRUNSCRIPT, to reminds us that the value will be overwritten everytime you run the project (actually when the .pbxproj changes).</p><p>For now it works.</p><h3>How do I add the script to Xcode?</h3><p>Click on to the <em>Build Phases </em>tab in your app’s target, then add a new <em>Run Script</em> by clicking on the + button.</p><p>In the <em>Shell </em>field type /usr/bin/ruby, and below paste the entire script.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d73ac65ac11c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Apple’s UI Test gotchas — UITableViewControllers]]></title>
            <link>https://medium.com/@t.camin/apples-ui-test-gotchas-uitableviewcontrollers-52a00ac2a8d8?source=rss-e7853fe548e2------2</link>
            <guid isPermaLink="false">https://medium.com/p/52a00ac2a8d8</guid>
            <category><![CDATA[ui-testing]]></category>
            <category><![CDATA[uitablew]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Tomas Camin]]></dc:creator>
            <pubDate>Sun, 23 Oct 2016 15:30:15 GMT</pubDate>
            <atom:updated>2016-11-25T22:53:32.669Z</atom:updated>
            <content:encoded><![CDATA[<p>After lots of head scratches, frustation and endless trial and error sessions we finally managed having a significant number of UI Tests reliably running on both an iPhone and an iPad. In combination with <a href="https://medium.com/@t.camin/stubbing-apples-ui-tests-13e1f1f0b88e#.3sngj0vkb">SBTUITestTunnel</a> Apple’s UI Tests became an effective tool not only to protect against future regression but an essential companion during development allowing to catch unexpected behaviors with the power of dynamic mocking.</p><p>While adding a new test to verify the pagination of one very simple UITableViewController we faced a weird issue where more pages than expected were loaded.</p><p>For pagination we use a very simple technique: as long as we know there are additional items to load we add one cell to the dataSource that is associated with a TableViewLoadingCell which is the used to trigger a new batch of items in tableView(_:willDisplay:forRowAt:)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5aa87bf60a0bce2de21d3b2430c86ef/href">https://medium.com/media/c5aa87bf60a0bce2de21d3b2430c86ef/href</a></iframe><p>So far, so easy. Surprisingly this doesn’t work as expected when running UI Tests. For no apparent reasons a lot of new items get fetched all at once. Some debugging later <strong>reveals that the </strong><strong>tableView(_:willDisplay:forRowAt:) is being called repeatedly while running UI Tests, even for offscreen cells. </strong>With hindsight this kinda makes sense because the testing framework has to build the cell hierarchy for the tableview, in order for example to properly scroll to an offscreen cell.</p><p>A demo project with a simple UITableViewController confirms this behaviour</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/910a1ca355fc391b0b710917d3fd83cc/href">https://medium.com/media/910a1ca355fc391b0b710917d3fd83cc/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34fc00661d9aca785257aebb8abe1d54/href">https://medium.com/media/34fc00661d9aca785257aebb8abe1d54/href</a></iframe><p>The following log is produced on the app side:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a06c4b2948f74bc326e0f2713a4bb0e7/href">https://medium.com/media/a06c4b2948f74bc326e0f2713a4bb0e7/href</a></iframe><p><strong>Now that’s a lot of will display cells!</strong></p><p>The number of visible cells is 17. The log shows that the UI Test framework does something unusual for offscreen items.</p><p>From index 0 to 16 (visible items) everything looks normal with 1 tableView(_:cellForRowAt:) and 1 tableView(_:willDisplay:forRowAt:) for every visible cells. The UI Test framework goes on calling the aforementioned methods for every cell in the datasource (ok, it needs to build the entire cell hierarchy), however now its making 2 tableView(_:willDisplay:forRowAt:) for every offscreen cell, 🤔.</p><p>But what comes after in the log is what’s really weird: there’s a first loop with 23 tableView(_:willDisplay:forRowAt:) for every offscreen cell, and a second one with 19 tableView(_:willDisplay:forRowAt:) per cell.</p><p>There are probably good reasons for the guys at Cupertino of doing this (gonna post a radar anyways), <strong>but for the time a workaround that seems to be working if you need to do anything else other than customizing the cell appearance</strong> in tableView(_:willDisplay:forRowAt:) is to check if the cell belongs to the currently visible index paths</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc21d6d752eac203f7a7bbfece129fd2/href">https://medium.com/media/cc21d6d752eac203f7a7bbfece129fd2/href</a></iframe><p>Which produces the following log that looks right (or at least works for us)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4a344637428c127e31f8131fad83c97a/href">https://medium.com/media/4a344637428c127e31f8131fad83c97a/href</a></iframe><p>From now on writing testable code has one extra rule.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=52a00ac2a8d8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Stubbing Apple’s UI Tests]]></title>
            <link>https://medium.com/@t.camin/stubbing-apples-ui-tests-13e1f1f0b88e?source=rss-e7853fe548e2------2</link>
            <guid isPermaLink="false">https://medium.com/p/13e1f1f0b88e</guid>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[ui-testing]]></category>
            <dc:creator><![CDATA[Tomas Camin]]></dc:creator>
            <pubDate>Mon, 25 Jul 2016 20:40:46 GMT</pubDate>
            <atom:updated>2016-08-12T07:28:35.824Z</atom:updated>
            <content:encoded><![CDATA[<p>A couple of months ago we decided to revisit the automatic user interface test suites of our iOS app trying to approach the subject with a fresh set of mind. The existing code base we had consisted on a fairly large number of test cases that used the popular <a href="http://goo.gl/suDRzA">KIF testing framework</a> which, at the time we adopted it, was de facto the only available option. The tool is great but to be honest we experienced some problems with it, the biggest being that we struggled to achieve consistent results. There was always one test randomly failing which, over time, made us trust the test results less and less.</p><h4>Ever tried UI Tests?</h4><p>We decided to start from scratch using Apple’s latest interface testing technology, <a href="http://goo.gl/cwfYR6">UI Tests</a>. This framework comes with all the benefits of being an official tool: it’s well integrated with the development tools, it will be always up to date and last but not least one would expect it should just work. And indeed, a part from a rough start, it does not just work but it’s rock solid.</p><p>At first glance it seems that Apple’s framework offers similar functionalities to KIF, however there is one significant difference with how tests are executed. Testing code is not embedded inside your application, instead it’s part of a separate app that is installed on the device and is in charge to execute the user interface automation. If you google around you’ll find that this is seen as being the major flaw in UI Test because test code and app code live in different processes, preventing the testing code to access the app’s code. This means that there’s no way to stub network requests with <a href="https://github.com/AliSoftware/OHHTTPStubs">OHHTPStubs</a> as we were used to.</p><h4>When that’s not enough</h4><p>Before moving on adopting UI Tests we had to restore some of the basic functionalities we were used with KIF, most importantly network request stubbing. The solution we found was pretty simple: an http server is instantiated inside the app receiving requests from the testing code. This allows to dynamically send requests during the execution of the tests, giving great flexibility in how tests are designed. The result is a library, <a href="http://goo.gl/6nHJLL">SBTUITestTunnel</a>, that wraps all the complexities of its implementation and is easy to use and integrate in any project.</p><p>With the library in place, in your test target, you’ll be able to easily:</p><ul><li>stub network calls</li><li>interact with NSUserDefaults and Keychain</li><li>download/upload files from/to the app’s sandbox</li><li>monitor network calls</li><li>define custom blocks of codes executed in the application target</li></ul><h4>Really? (for TL;DRers: Installation)</h4><p>Integrating SBTUITestTunnel with <a href="https://github.com/CocoaPods/CocoaPods">CocoaPods</a> is the most simple way to get you started. You’ll need to add two pods to the <em>Podfile</em>, one for the UI Test (XCTestsCase subclasses and other boiler code) and the other for the application’s target (the http server that receives and handles the requests).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5bd1e7a72dc9d7a7590008fb0f0d49f8/href">https://medium.com/media/5bd1e7a72dc9d7a7590008fb0f0d49f8/href</a></iframe><p><strong>🔥🔥🔥 If you’re using CocoaPods v1.0 and your UI Tests fail to start, you may need to add $(FRAMEWORK_SEARCH_PATHS) to your <em>Runpath Search Paths</em> in the <em>Build Settings</em> of the UI Test target!</strong></p><p>Then modify your <em>AppDelegate.swift</em> importing the <em>SBTUITestTunnel</em> framework and let the <em>takeOff()</em> method do the rest. It should look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c74a65837d84151fbd280142a51b6a2d/href">https://medium.com/media/c74a65837d84151fbd280142a51b6a2d/href</a></iframe><blockquote><strong>🔥</strong> The entire code of the library is wrapped aroun<em>d DEB</em>UG preprocessor macros so that SBTUITestTunnel won’t be included in your production code!</blockquote><blockquote>❓ Why the <em>initialize()</em> method? Launching SBTUITestTunnel there allows us to synchronously execute code a tiny bit before the app starts, allowing for example to conveniently set user preferences.</blockquote><p>That’s it, now you’re ready to write the first test.</p><h4>Show me the code! (for TL;DRers: Setup)</h4><p>On the test target you’ll be using our <em>XCUIApplication</em> sublass <em>SBTUIApplication</em> which has the methods to leverage the extra features we added.</p><p>When launching the test target you can provide options to perform certain actions at startup and a block which will be executed right before the app starts:</p><ul><li><em>SBTUITunneledApplicationLaunchOptionResetFilesystem </em>resets the entire app sandbox before start.</li><li><em>SBTUITunneledApplicationLaunchOptionDisableUITextFieldAutocomplete</em> will disable UITextFields autocomplete feature that often interferes when text is typed in.</li><li>The startup block is the right place to prepare the app status with additional setups (inject files, modify <em>NSUserDefaults</em>, etc).</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f4f6e88b530adfd97589a7438278052/href">https://medium.com/media/0f4f6e88b530adfd97589a7438278052/href</a></iframe><blockquote>👀 no more XCUIApplication here and we’re using launchTunnelWithOptions() instead of the usual launch() method</blockquote><p>Still having doubts? SBTUITestTunnel headers are well commented making the library’s functionality self explanatory, so make sure to check them out. Alternatively you can also take a look at the example project that we’re hosting on our <a href="http://goo.gl/6nHJLL">GitHub repo</a>.</p><h4>Stubbing (for TL;DRers: Stubbing)</h4><p>You can stub network calls, conveniently faking the response, using a regex matching a certain request or by checking the queries containing certain parameters. You can optionally remove the stub during execution, although this is not necessary if you need the stub until the end of the test.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c73fc2b1b713bc28bc8b4bff7b508d7f/href">https://medium.com/media/c73fc2b1b713bc28bc8b4bff7b508d7f/href</a></iframe><h4>Preferences (for TL;DRers: NSUserDefaults and Keychain)</h4><p>Modifying <em>NSUserDefaults / Keychain</em> is easy. You could for example write a test that verifies that a counter you’re using to track the app’s usage gets properly updated. Or you could “login” a test user by conveniently writing the app’s keychain.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/407d09b643c555d2dd356eb318e2ca01/href">https://medium.com/media/407d09b643c555d2dd356eb318e2ca01/href</a></iframe><h4>Filesystem (for TL;DRers: Filesystem)</h4><p>If needed you can upload or download files from the test bundle to the apps bundle. This might come in handy.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9271b3b818ef6568487dc5ffbd17b08d/href">https://medium.com/media/9271b3b818ef6568487dc5ffbd17b08d/href</a></iframe><h4>Monitoring (for TL;DRers: Monitoring)</h4><p>Often you need to verify that certain network calls are made after interacting with the UI. Similarly to stubbing the library offers a simple way to do so.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/76046e0349fb37202a65fc1a33fd88c0/href">https://medium.com/media/76046e0349fb37202a65fc1a33fd88c0/href</a></iframe><h4>Last but not least (for TL;DRers: custom block of code)</h4><p>The set of commands offered by the library should widely cover the testing needs of most of us. There’s however always an edge case that needs special treatment. The library allows you to invoke from your test’s target (in the example below <em>CustomBlockTest.swift</em>) a block of code in the application’s target (in the example below <em>ViewController.swift</em>). We advise using this as a last resort because you’ll be going to pollute your application target with test code, but it’s good to know it’s there.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/73be23a575282418c00c09cd53453903/href">https://medium.com/media/73be23a575282418c00c09cd53453903/href</a></iframe><h4>Conclusions</h4><p>Let’s face it: testing is no easy matter. It can get tricky but once you get it right it’s an irreplaceable tool for a happy development team! We’re enjoying and are proud of how our UI Testing are shaping up and hope that <em>SBTUITestTunnel</em> will help you too.</p><p><em>This post was originally posted on </em><a href="https://tech.subito.it/2016/07/19/stubbing-apples-ui-tests/"><em>https://tech.subito.it/2016/07/19/stubbing-apples-ui-tests/</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=13e1f1f0b88e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>