Top iOS Testing Frameworks

Did you know that the total number of iOS devices sold in the
market is 250 Million+?
11% of the iOS users remove the app after crashes
45% of iOS users would remove the paid apps after crashes
69% of iOS users would will stop playing and remove the games after first crash
6-7 iOS devices are sold every second

With the explosion of iOS devices year on year, the verge of iOS success is increasing higher than ever before.

More start-ups and SMEs don’t find it financially sustainable to acquire every piece of iOS devices with different OS versions and hardware specs.

On the other hand, testing apps or games manually is not anymore an ideal option for QA process due to low efficiency and scalability issue.

Though iOS is still a more closed operating system compared to Android, many open source test automation frameworks and tools can be utilized to create robust automated tests.

Along with the help of a cloud-based testing solution, it enables developers/testers to verify app quality more efficient in a scalable way and makes iOS testing tasks easier.

Indium follows a process-oriented approach for the successful deployment of Test Automation

Read More

Today we’d like to go through 5 widely used iOS testing frameworks nowadays with simple code samples to give you a basic understanding of getting started with your iOS testing.

If you are also interested in Android test automation frameworks, do not forget to check out Top 5 Android Testing Frameworks, where code samples are provided as well.

APPIUM

Appium is popular due to its flexibility and usability on both Android and iOS, and it works on 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.

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();

XCTEST / KIF

XCTest is tightly coupled with Xcode but is still usable with both real iOS devices and simulators.

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. Writing any tests with XCTest should be trivial to iOS developers as XCTest is fully compatible with both Objective-C and Swift.

KIF (Keep It Functional) is an iOS integration test framework that is closely related to and uses XCTest test targets. 

KIF tests can be executed directly in XCTestCase or any subclass. KIF allows for easy automation of iOS applications by leveraging the accessibility attributes that the OS system makes available for those with visual disabilities.

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()
}

Learn and get started with KIF by reading our blog about this: How to get started with KIF?

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 the 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”

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()];
}

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”);
});
});
});

BONUS: UI AUTOMATION

UI Automation is a tool developed by Apple that automates the testing of iOS applications. Put in another way, UI Automation to iOS is similar with UI Automator to Android.

UI Automation tests are written in JavaScript, adhering to an API defined by Apple. Below is the code written on Apple’s Developer portal.

Across Various devices and Platforms, Indium is your one stop solution. Click here to learn more about our

Mobile app Testing

NOTE! UI Automation still works fine if you are some using older version of Xcode, but with the latest Xcode, the entire UI Automation framework is deprecated by Apple.

UI AUTOMATION CODE SAMPLE

UIATarget.localTarget().frontMostApp().mainWindow().buttons()[0]
If Edit button is the first button in your code, you can also have the code as below:
UIATarget.localTarget().frontMostApp().mainWindow().buttons()[“Edit”]
Then you can use any of these three ways to call an action to tap that button:
UIATarget.localTarget().frontMostApp().mainWindow().buttons()[0].tap();
UIATarget.localTarget().frontMostApp().mainWindow().buttons()[“Edit”].tap();
var editButton=UIATarget.localTarget().frontMostApp().mainWindow().buttons()[0];
editButton.tap();
The Automation instrument has a complete element hierarchy that represents your app’s user interface. To view that hierarchy, use the logElementTree method to write an outline of it to the log:
UIATarget.localTarget().frontMostApp().logElementTree()

CONCLUSION

Though iOS app testing is totally different from Android app testing, you can use either Appium or Calabash to create test scripts that can be used to do cross-platform testing.

Is Your Application Secure? We’re here to help. Talk to our experts Now

Inquire Now

That being said, every framework has its advantages and requirements are unique for each project. It’s recommended to do some further research, learn more about each framework and choose the one you are comfortable with. Happy iOS testing.