XCUITest Tutorial – iOS Testing Framework Guide

In the XCUITest tutorial, we will learn what is XCUITest and we will do an iOS test automation example with swift. Let’s get started!

XCUITest is an iOS testing framework developed by Apple in 2015 for its native UI suite. It is developed on top of the XCTest framework which is the main test framework that is integrated within Apple’s Xcode. The XCUITest tests can be written on XCode, using Swift or Objective-C. XCUITest will only work for native iOS & macOS applications. It will not work for cross-platform or hybrid applications such as React Native, Flutter, etc.

To make the long story short, let’s learn step by step how to test an iOS application. I’m using a simple tab bar application that is on the Swift Tab Example page, if you want, you can use it as well.

Step by Step XCUITest Automation with Swift

Now, we will do a sample step-by-step test automation example with the XCUITest. 

Step-1

Open Project in Xcode, then File > New > Target

xcuitest tutorial

Step-2

Select “UI Testing Bundle” option.

getting started to xcuitest with swift

Step-3

Fill the required fields then click “Finish”.

ios testing framework

All done! Our test target is set up and ready to go!

Step-4

Let’s look up the example test code is generated by XCode.

XCUITest automation code in XCode

As you can see, the “setUpWithError” function is called beginning of the every test. In addition, the “tearDownWithError” function is called the end of the every test.

How to Run XCUI Tests on XCode

To run the XCUITests on XCode, you can click the highlighted icon below to see your newly created UI Test targets.

test targets in xcode

You can hover on the “testExample()” test case and click the “Play” icon to run that specific test to see if everything was set up properly. You should see your app being launched in a simulator and closing immediately after because your test case is empty.

running xcui tests on xcode

If you want to run all tests in the test suite then you can hover on the class name.

How to Write XCUI Tests for iOS Automation

Up until now, the basic setup of XCUITest is complete, we can start coding of tests. First of all, let’s explain the below function called “testExample()”.

how to write xcuitest automation

The XCUIApplication is basically a proxy for an app that can be launched and terminated. When you click inside the “testExample()” method, you will see the red-dot button in the lower-left corner.

ios test automation tutorial in swift

This button provides Record & Play feature. When you start to record, your activities will be recorded. According to your record, it generates a bunch of code for testing.

The XCUIElement is the actual UI element in the iOS application. It provides all the basic symbols and functions for UI element interactions. For example, clicking UI elements (single, double, pressing), interacting with the screen (swipe, pinch, zoom, rotate, etc.).

//Click-based functions 
tap()
doubleTap()
twoFingerTap()
tap(withNumberOfTaps: UInt, numberOfTouches: UInt)
press(forDuration: TimeInterval)
press(forDuration: TimeInterval, thenDragTo: XCUIElement)

// Generic UI interactions
swipeLeft()
swipeRight()
swipeUp()
swipeDown()
pinch(withScale: CGFloat, velocity: CGFloat)
rotate(CGFloat, withVelocity: CGFloat)

iOS Test Automation Example for XCUITest Tutorial

In the iOS test automation scenario below, you will see the basic flow and we will implement our automation code in swift based on this scenario.

The Test Scenario

  • Launch the app
  • Click the First tab
  • Click the Second tab
  • Print all StaticTexts
  • Assert that Profile Tab Exists

And here is the swift implementation of this scenario.

The XCUITest Test Automation Implementation

XCUITest test automation implementation in swift

Finally, XCTAssert functions are used for the verification of tests. The most used functions are below.

XCTAssert()
XCTAssertEqual()
XCTAssertFalse()
XCTAssertNil()
XCTAssertThrowsError()

Thank you, for reading. You can also check that link for Xcode UI Testing Cheat Sheet.

Best regards,
Amil Uslu

1 thought on “XCUITest Tutorial – iOS Testing Framework Guide”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.