2015-08-21T07:57:44+02:00http://yomajkel.github.io/Octopress2015-08-16T20:28:30+02:00http://yomajkel.github.io/blog/2015/08/16/automating-xcode-7-ui-tests-with-charles-and-apimockerXcode 7 brings automated UI testing on board. It doesn’t seem perfectly stable yet, nor does it support all of the UI elements (as of beta 4), but it makes adding UI tests to the app fairly easy. Instead of setting up the environment for Appium, Calabash, or other frameworks, we only have to tap a checkbox to have a project with UI tests target set up.

To make UI tests self contained we can use HTTP mocking library (OHHTTPStub for a good example) and stub all the requests. This is pretty good solution already, but what always bothered me is that the app needs to modified for that, and the more we modify the app the farther away it goes from the binary we will are going to release.

The other solution is to use mock server outside of the app. This lets our binary send HTTP requests like it would in production environment and all the magic happens outside of our binary. I set this up using apimocker together with Charles HTTP Proxy. Apimocker is simple to set up API mocking tool (it also sports many useful features). Charles Proxy is actually the only tool I found so far that is capable of mapping requests.

The test

For simplicity I will only write one test that will check for the number of forecast days. In Rain Shield app each day is represented by table cell, so to test if correct days number is displayed I only need to count table cells.

At the start Rain Shield makes asynchronous request and only updates it’s views after receiving response. If the line above will execute immediately the test will fail. Here is where expectations come in handy. I expect for at least one cell to be present in order to execute the rest of the test.