Recording UI tests

Before you can record UI tests, you must add a UI testing target to the project. Follow the same steps as before to add a new testing target, but pick the iOS UI Testing Bundle this time around. If you look inside the newly-created group in your project, the structure for your UI tests looks very similar to the structure for Unit tests.

One significant difference between UI test targets and Unit test targets is that your UI tests do not have access to any code that's inside your app. A UI test can only test the interface of your app and make assertions based on that.

If you open the MovieTriviaUITest.swift file, you'll notice the setUp() and tearDown() methods. Also, all of the tests that must be executed are methods with the test prefix. This is all similar to what you've already seen for XCUITest.

One big difference is that the app is launched explicitly in the setup stage. This is because the UI test target is essentially just a different app that can interact with your main app's interface. This limitation is very interesting, and it's also the reason why it's important to make your app accessible.

To start recording a UI test in Xcode, you must start a recording session. If you're editing code in a UI test target, a new interface element is visible in the bottom-left corner of your code editor area:

Place your typing cursor inside the testExample() method and click the red dot. Your app is launched and anything you do is recorded as a UI test and played back when you run your tests. If you tap on the label and the activity indicator on the loading screen, Xcode produces the following Swift code in the testing method:

let app = XCUIApplication()
app.staticTexts["Loading trivia questions..."].tap()
app.otherElements.containing(.activityIndicator, identifier:"In progress").element.tap()

The UI test you recorded is a set of instructions that are sent to the app. In this sample, the test looks for a certain element in the app's UI and calls tap() on it. This test doesn't do a lot, so it's not particularly useful. To make the test more useful, we should let the app know that it should run in a special test mode so it can load questions from the JSON file instead of trying to load it from the network. To do this, you can send launch arguments to the app. Launch arguments can be used by the app to enable or disable certain functionalities. You can think of them as variables that are sent to the app when it launches.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset