UI Testing in Xcode with Swift – part II

Hello there… I thought of writing the second part of the UI Testing tips blog post without waiting so long. For those who are wondering where is the first blog post, just follow this link 🙂 In this post also I’ll provide you some helpful tips that I’ve learnt while doing UI testing in Xcode with Swift language. 

For the demonstration purpose, I created a small UI, which has several widgets that will help through out this blog post.



Testing View

Like I mentioned in my previous post, the easiest way to write a test case is recording it. What I’m trying to do with this interface is, write something on the TextField, toggle the Switch and finally tap on the Tap Me button.

Once I record that what I got when I tap on the TextFiled is something like below.

let app = XCUIApplication()
let textField = app.otherElements.containing(.switch, identifier:"1").children(matching: .textField).element

Focus on the second line. How big that is? Having lengthier code is bad for readability. Then, how can we shorten that code line. We have missed one important thing while we creating the UI. That is providing an Accessibility Identifier for this UITextField. You can do it on your storyboard or .XIB file by selecting the UITextFiled and opening Identity Inspector on your Xcode. It will be look something like below.

UITextField with Accessibility enabled with an ID

UITextField with Accessibility enabled with an ID

After that I recorded again and that line has shorten like this.

let toptextfieldTextField = app.textFields["topTextField"]

See, how nice and clear your code is now 🙂 To type some text inside the TextField it records following line where you have nothing to do.


And for hide the keyboard it will record following line.


You can get use of the Xcode debug area to find extra information like below.

p print()

p print()

What I have done was put a break point in the code that will pause the application debugging and when there is a value initialized for myswitchSwitch variable I printed out the full description of that variable on the console. With the help of that description I modified the code and added extra assertion to validate the status of the Switch like this.

        let myswitchSwitch = app.switches["mySwitch"]
        XCTAssertEqual(myswitchSwitch.value as! String, "1")
        XCTAssertEqual(myswitchSwitch.value as! String, "0")

Now I will give you some tips that you can play with the TableView. Yes, that is not in the UI that I have provided. But it is not difficult to imagine the structure of the TableView with the following explanation. First we try to validate a section header of a table view.

let firstSectionHeader = app.tables.otherElements["First Section"]

Just like you thought, the section header title is First Section and what I tried to do is to check such a header exists when running the test case. Next we will try to validate the number of cells that a table is having.

let myfavoritesTable = app.scrollViews.otherElements.tables["MyFavoritesTable"]
XCTAssertEqual(myfavoritesTable.cells.count, 2)

In the above code what I have done was validate the number of cells the table with the accessibility identifier MyFavorites is having. Finally I will show you how to organize your test code which will reflect it even in your test report. Let’s take the following test case.

    func testWardTables() {


        let myPatientHeader = app.tables.otherElements["MY PATIENTS"]
        let wardsHeader = app.tables.otherElements["WARDS"]


        XCTContext.runActivity(named: "My Patients table elements test") { _ in

            let mypatientstableTable = app.scrollViews.otherElements.tables["MyPatientsTable"]
            XCTAssertEqual(mypatientstableTable.cells.count, 2)

            mypatientstableTable/*@START_MENU_TOKEN@*/.staticTexts["Favorite Patients"]/*[[".cells.staticTexts[\"Favorite Patients\"]",".staticTexts[\"Favorite Patients\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/.tap()

            app.navigationBars["Favorite Patients"].buttons["Wards"].tap()

            mypatientstableTable/*@START_MENU_TOKEN@*/.staticTexts["My Own Patients"]/*[[".cells.staticTexts[\"My Own Patients\"]",".staticTexts[\"My Own Patients\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/.tap()

        let wardlisttableTable = app.scrollViews.otherElements.tables["WardListTable"]
        let cellQuery = wardlisttableTable.cells.containing(.staticText, identifier: "Akutt - Barn")
        XCTAssertEqual(cellQuery.count, 1)

Once you wrap a block of code with Activity name it will show in the test report like below.



Let me explain how you can see your test report first. What you have to do is Control + Click on the diamond icon on a test case, which will pop up a dialog and from that, select Jump to Report option. In the above test report you can see the events triggered inside the Activity that we have declared, are displayed under that Activity name.

I think the tips I’ve given is enough for this blog post. So I’ll wrap up the things and we will meet another exiting blog post to learn and share our knowledge and experiences. Until then, have a nice Testing 🙂





About AnujAroshA

Working as a Technical Lead. Specialized in iOS application development. A simple person :)
This entry was posted in iOS, Swift and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s