Overview
Agile development projects are adopting practices such as automated acceptance testing, BDD, continuous integration, and others. There is a lot written about these approaches for developing software, their benefits and limitations etc. However, one of the fundamental benefits these practices offers is enhanced communication between project stakeholders including users, product owner, and the development team, a shorter feedback loop and agility. These practices requires all the project participants to come together, discuss, and elicit the behavior of the application in agreed upon format of features, user stories and acceptance criteria and shared definition of “Done!”. Read more about these in Wikipedia
Mobile application development project can leverage on these advantages in building mobile apps in shorter cycles and feedback loops to build a successful applications. In this series of posts I will explain how you can apply automated acceptance testing to iOS native applications using Appium and Cucumber-JVM. You will also see how these applications can be tested in cloud using Sauce Labs platform.
Cucumber-JVM
Cucumber-JVM is a pure Java implementation of original Cucumber BDD/ATDD framework. It supports the most popular programming languages on the JVM. It’s already been used by various teams along with Selenium WebDriver for testing Web applications. Cucumber support creating features files which are written in a ubiquitous language understood by the whole team. These feature file describe the expected behavior of the application and are used as tests to run against the application. Cucumber can be used for API, integration and functional testing of the application.
In this example we will see functional testing of a sample native iOS App using Cucumber-JVM and Appium.
The Sample App
This example is based on a sample BMI Calculator application which is used by Health & Nutrition specialists to calculate the Body Mass Index of patients by submitting Height and Weight values to the App.
Let’s work on one of the main feature of this App as described below
Feature: Calculating Body Mass Index
As a health specialist
I want a BMI Calculator
So that I can calculate patient's Body Mass Index
Setting up Test Project
Let’s setup a new project using with IntelliJ IDEA using Maven & Cucumber-JVM with following steps:
Create a new Project as Maven module, provide appropriate values for <bGroupId and ArtifactId. In this example GroupId is set with org.bmicalc.test value and ArtifactId as bmicalculator.test
Once Intellij IDEA creates project with appropriate folder structure, locate and modify pom.xml file. Add below highlighted dependencies to pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.bmicalc.test</groupId>
<artifactId>bmicalculator.test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>LATEST</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.0.14</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.0.14</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
This will add Cucumber-JVM and Selenium dependencies to the project.
Writing Feature File
In Cucumber-JVM specifications or requirements are expressed in a plain text, Given/When/Then kind of syntax known as Gherkin language (github.com/cucumber/cucumber/wiki/Gherkin) which is understood by the whole team. So let’s create a feature file for above feature in the project.
Add a new package bmicalculator.test under src/test/resources as shown in below screenshot and add a new file with name bmi_calculator.feature
Feature: Calculating Body Mass Index
As a health specialist
I want a BMI Calculator
So that I can calculate patient's Body Mass Index
BmiCalculator
@ios-sauce
Scenario Outline: Calculate Body Mass Index
Given I enter "<Height>" as height
And I enter "<Weight>" as weight
And I press the Calculate button
Then I should see "<BMI>" as bmi and "<Category>" as category
Examples:
|Height |Weight |BMI |Category |
|170 |50 |17.30|Underweight|
|181 |80 |24.42|Normal |
|180 |90 |27.78|Overweight |
|175 |100 |32.65|Obese |
Every feature file contains a single feature. A feature usually contains a list of scenarios. Every scenario consists of a list of steps, which must start with one of the keywords Given, When, Then, But or And. Scenarios express expected behavior of the system under given conditions.
In addition to a scenario, a feature may contain a background, scenario outline and examples. Our example scenario contains Scenario background and examples for number of BMI calculations representing each category.
This scenario outlines allow us to more concisely express these examples through use of a template with placeholders. In this example Calculate Body Mass Index is run once for each row in the Examples section beneath it (not counting the first row which is a header). This is similar to data driven testing.
Source: unmesh.me