Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Top 5 iOS Test Automation Frameworks With Code Examples

DZone's Guide to

Top 5 iOS Test Automation Frameworks With Code Examples

This comparison of the features of five popular Android app testing frameworks will help you decide which is best for your mobile app.

· Mobile Zone
Free Resource

Though iOS is still a more closed operating system compared to Android, many open source test automation frameworks and tools are out there to help iOS developers lessen the pain of creating robust and reliable test cases for functionality and UI test automation. By running automated tests on a scalable cloud-based device testing farm, it enables teams to decrease the test cycle and publish high-quality iOS apps faster and more frequent.

Let's go through five widely used iOS testing frameworks with simple code samples to give you a basic understanding of iOS testing. 

1. Appium

Appium is a popular open source framework thanks to its flexibility and usability on both Android and iOS and it works for native, hybrid and web applications. For iOS testing, it uses JSONWireProtocol to engage with iOS applications using Selenium WebDriver. Thanks for that, Appium does support mobile web testing very well and its use cases are very similar as if Selenium would be used for web testing. Below is the code example to help you understand how to find elements with Appium automation engine.

Appium code sample:

driver.findElement(By.id("com.example.app:id/radio0")).click();
driver.findElement(By.id("com.example.app:id/radio1")).click();
driver.findElement(By.id("com.example.app:id/radio2")).click();
driver.findElement(By.id("com.example.app:id/editText1")).click();
driver.findElement(By.id("com.example.app:id/editText1")).sendKeys("Simple Test");
driver.findElement(By.name("Answer")).click();
// or alternatively with
driver.findElement(By.id("com.example.app:id/button1")).click();

2. XCTest/XCUITest

XCTest and XCUITest are two integral test automation frameworks for iOS app testing. XCTest allows developers to write tests for components at any level and also provides the framework for UI testing capabilities. XCTest tests are grouped into subclasses of XCTestCase. Typically XCUITest is used for functional testing, automating tests of common workflows, demo sequences or behavior of customer views. One can also use XCUITest Recorder for recording the first steps of the automated test. To find elements, their properties and navigating through the element tree of the application, one can also make use of the interface builder. 

As for programming languages, XCTest/XCUITest are fully compatible with both Objective-C and Swift.

Code sample with Objective-C:

- (void) testClicksOnRadioButtons { [tester tapViewWithAccessibilityLabel: @”Radio1”];
[tester tapViewWithAccessibilityLabel: @”Radio2”];
[tester tapViewWithAccessibilityLabel: @”Radio3”];
[tester enterText: @”Simple Test”
intoViewWithAccessibilityLabel: @”editText1”];
[tester tapViewWithAccessibilityLabel: @”Answer”];
}

Code sample with Swift:

testClicksOnRadioButtons() {
let app = XCUIApplication()
app.radiobutton[0].tap()
app.radiobutton[1].tap()
app.radiobutton[2].tap()
app.staticTexts[“Simple Test”]
app.button[0].tap()
}

3. Calabash

Calabash is another great cross-platform framework that works perfectly with Android and iOS apps. One of the major differences to other frameworks is that Calabash tests are written in Cucumber. That means the test is written like a specification and is simple and easy to read even for non-tech people, but still executable by the automation system. 

Calabash code sample:

Feature: Answer the Question feature
Scenario: As a valid user I want to answer app question
I wait
for text "What is the best way to test application on hundred devices?"
Then I press Radio button 0
Then I press Radio button 1
Then I press Radio button 2
Then I enter text "Simple Test"
into field with id "editText1"
Then I press view with id "Button1"

4. EarlGrey

To some degree, EarlGrey is the "Espresso for iOS." It’s also developed and open sourced by Google. Google uses this test framework to test many iOS native apps including Google Calendar, YouTube, etc. As the codename goes, lots of similarities can be found between Espresso and EarlGrey. For example, EarlGrey tests will automatically wait for events (animations, network requests etc.) before trying to interact with the UI.

EarlGrey code sample:

- (void) testBasicSelectionAndAction { [[EarlGrey selectElementWithMatcher::grey_accessibilityID(@"ClickHere")]
performAction: grey_tap()];

// Example of long press with EarlGrey matchers
- (void) testLongPress { [[EarlGrey selectElementWithMatcher::grey_accessibilityLabel(@"Box")]
performAction: grey_longPressWithDuration(0.5f)];
[[EarlGrey selectElementWithMatcher::grey_accessibilityLabel(@"One Long Press")]
assertWithMatcher: grey_sufficientlyVisible()];

// Example of multi-select, visible click on items
- (void) testCollectionMatchers {
id visibleSendButtonMatcher = grey_allOf(grey_accessibilityID(@"Box"), grey_sufficientlyVisible(), nil);
[[EarlGrey selectElementWithMatcher: visibleSendButtonMatcher]
performAction: grey_tap()];
}

5. Jest/Jasmine

Jest uses Jasmine behavior-driven framework as the basis for testing JavaScript code. Every test case starts from describe() function call, similar to how JUnit uses TestCase class. The describe() function takes 2 parameters - the description/title of the test case and the function to be executed. The it() function includes all the test steps and provides (similar to JUnit) series of expect() functions.

Jasmine code sample:

describe("Player",
function() {
var player;
var song;
beforeEach(function() {
player = new Player();
song = new Song();
});
it("should be able to play a Song",
function() {
player.play(song);
expect(player.currentlyPlayingSong).toEqual(song);

//demonstrates use of custom matcher
expect(player).toBePlaying(song);
});
describe("when song has been paused",
function() {
beforeEach(function() {
player.play(song);
player.pause();
});

it("should indicate the song is paused",
function() {
expect(player.isPlaying).toBeFalsy();

// demonstrates use of 'not' with a custom matcher
expect(player).not.toBePlaying(song);
});
it("should be possible to resume",
function() {
player.resume();
expect(player.isPlaying).toBeTruthy();
expect(player.currentlyPlayingSong).toEqual(song);
});
});

// demonstrates use of spies to intercept and test method calls
it("tells the current song if the user has made it a favorite",
function() {
spyOn(song, 'persistFavoriteStatus');
player.play(song);
player.makeFavorite();
expect(song.persistFavoriteStatus).toHaveBeenCalledWith(true);
});

//demonstrates use of expected exceptions
describe("#resume",
function() {
it("should throw an exception if song is already playing",
function() {
player.play(song);
expect(function() {
player.resume();
}).toThrow("song is already playing");
});
});
});

To Sum Up

Every test automation framework has its advantages. If you are working with Android side, for example, Appium and Calabash are two good choices for cross-platform app testing to save scripting efforts to some degree. As an integral part of Xcode, XCTest/XCUITest are getting popular among iOS developers. While you're suggested to do some research on what works the best for your testing needs, it's also recommended to rely on a flexible mobile device cloud that provides full compatibility with all of these iOS frameworks and helps you improve your iOS app testing.

Topics:
test automation ,appium ,calabash ,xctest ,mobile testing ,ios ,mobile

Published at DZone with permission of Lingkai Shao. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}