This series was originally posted on Sauce Labs Blog This is a reblog with revisions.
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 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
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt; &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt; &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt; &lt;groupId&gt;org.bmicalc.test&lt;/groupId&gt; &lt;artifactId&gt;bmicalculator.test&lt;/artifactId&gt; &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt; &lt;dependencies&gt; &lt;dependency&gt; &lt;groupId&gt;junit&lt;/groupId&gt; &lt;artifactId&gt;junit&lt;/artifactId&gt; &lt;version&gt;4.11&lt;/version&gt; &lt;scope&gt;test&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt; &lt;artifactId&gt;selenium-java&lt;/artifactId&gt; &lt;version&gt;LATEST&lt;/version&gt; &lt;scope&gt;test&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;info.cukes&lt;/groupId&gt; &lt;artifactId&gt;cucumber-java&lt;/artifactId&gt; &lt;version&gt;1.0.14&lt;/version&gt; &lt;scope&gt;test&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;info.cukes&lt;/groupId&gt; &lt;artifactId&gt;cucumber-junit&lt;/artifactId&gt; &lt;version&gt;1.0.14&lt;/version&gt; &lt;scope&gt;test&lt;/scope&gt; &lt;/dependency&gt; &lt;/dependencies&gt; &lt;/project&gt;
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 (https://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
Copy following feature text to bmi_calculator.feature file
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 &quot;&lt;Height&gt;&quot; as height And I enter &quot;&lt;Weight&gt;&quot; as weight And I press the Calculate button Then I should see &quot;&lt;BMI&gt;&quot; as bmi and &quot;&lt;Category&gt;&quot; 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.
In next part, we’ll see how to run features with Maven, and enter step definitions.