Automating UI Test Scripts - UI Testing - iOS 9 Swift Programming Cookbook (2015)

iOS 9 Swift Programming Cookbook (2015)

Chapter 9. UI Testing

9.2 Automating UI Test Scripts

Problem

You want Xcode to generate most, if not all, of your UI testing code. You can write more UI testing code Swift. but it’s useful to take advantage of what Xcode gives you for free.

Solution

Use the new record button in Xcode when you are in your UI testing target’s code (see the red circle near the upper left corner of Figure 9-5). This will really be handy if you want to automatically get all your UI test codes written for you (but sometimes you’ll still have to write some yourself).

Figure 9-5. The little circular record button on the debugger section of Xcode’s window automatically gets UI test codes

NOTE

You can write all your UI tests in pure Swift code. No more mocking around with JavaScript. Jeez, isn’t that a relief?!

Discussion

Let’s say that you have a UI that looks similar to that shown in Figure 9-6. In this UI, the user is allowed to enter some text in the text field at the top of the screen. Once she is done, she can just press the button and the code will translate her input into its equivalent capitalized string and place it in the label at the bottom.

Figure 9-6. Sample UI with text fields and button

I assume that you have arranged these UI components inside a storyboard. In the identity inspector in IB, set the accessibility label of your text field to “Full Name”, the label for your button to “Capitalize”, and your label to “Capitalized String”. Now hook up your text field and your label to your code under the names of “lbl” and “txtField” as I’ve done. It just makes understanding the code easier. Otherwise you can name them what you want. Then hook the action of your button to your code. I’ve named this action method capitalize(). Now when the user presses the button, we read the text and capitalize it:

@IBAction func capitalize() {

guard let txt = txtField.text where txt.characters.count > 0 else{

return

}

lbl.text = txt.uppercaseString

lbl.accessibilityValue = lbl.text

}

Now head over to the main Swift file for your UI tests and you should see a simple and empty method usually named testExample(). Put your cursor inside that method and then press the record button. Xcode will open your app and you will be able to interact with your app as you would normally. Acting as a user would be expected to act, select the text field by tapping on it and then type some text in it like “Hello, World!” Finally, press the capitalize button. Xcode will generate a test that looks more or less like:

let app = XCUIApplication()

let fullNameTextField = app.textFields["Full Name"]

fullNameTextField.tap()

fullNameTextField.typeText("Hello, World")

app.buttons["Capitalize"].tap()

We have a problem, Watson! We now need to make sure that the capitalized text inside our label is correctly capitalized. How can we do that in Xcode and get Xcode to generate the code for us? Well, the answer is: we can’t!. This is a logical task that you cannot automate with Xcode, so let’s do it ourselves. In the app object, there is a property called staticTexts, so let’s get our label from there:

let lbl = app.staticTexts["Capitalized String"]

This will give us an item of type XCUIElement. The app object is of type XCUIApplication, just so you know. Every element has a value property that is an optional value of type AnyObject. For our label, this is going to contain a string. So let’s read its value as a string and then compare it with the string that we expect it to be:

let enteredString = "Hello, World!"

let expectedString = enteredString.uppercaseString

let app = XCUIApplication()

let fullNameTextField = app.textFields["Full Name"]

fullNameTextField.tap()

fullNameTextField.typeText(enteredString)

app.buttons["Capitalize"].tap()

let lbl = app.staticTexts["Capitalized String"]

XCTAssert(lbl.value as! String == expectedString)

NOTE

I took the opportunity to put the entered and expected strings inside string objects so that we don’t have to write them multiple times.

Now press the little play button next to your test method and let Xcode do its thing. You should now see that the text has succeeded if everything went well.

See Also