Basic Appium and Cucumber - BDD Framework

Automation that Made Everything Easier

For the Quality Engineering (QE) team, discussing Appium probably is not a new topic because they are always associated with software tools that help their work in the testing process.

As a software developer, it is a new topic for me. It is nothing more than a curiosity to learn and it is just an intention to help my QE team to understand Appium and be able to apply it in their daily testing process. With automation tests, it will help to improve the testing process more efficiently and effectively.

Knowing the Tools

Before starting, let’s talk about the definitions. Firstly, the definition of Appium. Appium is a mobile web, native, and hybrid application test automation tool. Appium can test Android applications and iOS applications. For more detail, we can access and read from the official site here http://appium.io.

Secondly, Cucumber is a software tool or framework supporting using behavior-driven development (BDD) in our software test process. The central or cucumber is used Gherkin, which is a language to define test cases. It is outlined to be non-technical and human-readable for business and product teams quickly to understand the test process. For more information about Cucumber and Gherkin, you can find it at the official site here https://cucumber.io.

The process of test automation using Appium.

To do testing using Appium and Cucumber, we must have several tools installed on our computers or laptops. This explanation will be much focused on macOS operation. Below is a list of tools or frameworks that has to be installed.

  1. Java (JDK 8 from Oracle or OpenJDK)
  2. IntelliJ Community or Ultimate
  3. Cucumber Plugin
  4. Gherkin Plugin
  5. Appium
  6. Maven 3
  7. Log4J2, TestNg, and JUnit
  8. Android SDK
Required tools for running mobile automation tests.

Before starting, let’s first explain the uses of the tools above one by one.

  • Java is a programming language that will be used to create automatic tests. (Appium supports all web driver-compatible programming languages such as Java, Objective-C, JavaScript with Node.js, PHP, Python, Ruby, C#, Clojure, and Perl)
  • IntelliJ IDEA is from JetBrain as an integrated development environment written in Java programming language for creating computer software. It is accessible as an Apache 2 Licensed community edition and in a proprietary commercial edition. Both can be used for commercial development.
  • Cucumber is a BDD (Behavior Driven Development) framework and reporting.
  • Gherkin is a specification format for cucumber. It is a domain-specific language that helps you describe business behavior without needing to go into detailed implementation.
  • Appium itself is a library for us to make automatic mobile application tests on both android or iOS.
  • Maven 3 is a build automation tool to build and compile java source code.
  • Log4J2 as the logging framework, JUnit as the unit test library, and TestNG is the functional test library.

Thus we will get the features and benefits of the tools above as follows.

  1. The test design uses the Page Object Model, a pattern commonly used in the automatic test process.
  2. Behavior-driven because it uses cucumber.
  3. Support CI/ CD.
  4. Support parallel execution using JUnit and TestNG, but this tool will be used in advance when using cucumber and mix with JUnit and TestNG.
  5. Support android and iOS automatic tests.
  6. Start and stop the appium service programmatically.
  7. Support HTML test reporting.
  8. Support CLI(Command-Line Interface).

Installation and Environment Configuration

All installation processes require brew to be installed on our laptop. So we assume brew is already installed.

  1. Java SDK installation,
brew cask install adoptopenjdk8

2. Check the version to make sure the Java version is installed correctly.

deni.rizal@Ralalis-MacBook-Pro ~ % java -version openjdk version "1.8.0_265" OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_265-b01) OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.265-b01, mixed mode) deni.rizal@Ralalis-MacBook-Pro ~

3. Download IntelliJ IDEA and install it. You can choose the paid (ultimate) or community version.

4. Install the Cucumber and Gherkin plugins.

5. Install Node.js and Appium

brew install node
deni.rizal@Ralalis-MacBook-Pro ~ % node -v
v14.10.1
eni.rizal@Ralalis-MacBook-Pro ~ % npm install -g appium
deni.rizal@Ralalis-MacBook-Pro ~ % appium -v
1.18.1

6. Install maven 3

brew install mavendeni.rizal@Ralalis-MacBook-Pro ~ % mvn -v Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: /usr/local/Cellar/maven/3.6.3_1/libexec Java version: 1.8.0_265, vendor: AdoptOpenJDK, runtime: /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre Default locale: en_GB, platform encoding: UTF-8 OS name: "mac os x", version: "10.15.6", arch: "x86_64", family: "mac" deni.rizal@Ralalis-MacBook-Pro ~ %

7. Download the Android SDK via IntelliJ

8. Perform environment settings via bash or zsh. If your system uses zsh, please edit the zshrc file.

export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home export ANDROID_HOME=/Users/deni.rizal/Library/Android/sdk export PATH="${ANDROID_HOME}/tools:${PATH}" export PATH="${ANDROID_HOME}/emulator:${PATH}" export PATH="${ANDROID_HOME}/platform-tools:${PATH}

9. Save and reboot your MacBook.

Testing Code Creation

Before creating code for automatic testing, I first disclaimer that the java code created is not the best practical java code for software development. This code is just for a demo to run a simple automatic test with Java, cucumber, and appium.

  1. Create Project Maven with the installed IDE.

2. Select the maven project, specify the project name, and storage location for the source code.

3. After the project is created, edit the POM of the project and add some libraries that will be used.

<?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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>BasicAppiumTest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.3.0</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm</artifactId>
<version>1.2.5</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>gherkin</artifactId>
<version>2.12.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>3.141.59</version>
</dependency>
</dependencies>
</project>

4. Do a clean install to download the libraries that are required.

mvn clean install

5. Create a folder to store the scenarios under resources, give name features, and two packages under test with name setups and steps.

6. Create a test scenario with the name scenarioTest.feature like the following and make sure the cucumber symbol is on.

@RaraliQATeam 
Feature: AppiumTest
@DoTest
Scenario: Input and Calculate Price
Given Input Data and Do Calculate Price
Given Input Wrong Data and Do Calculate Price

7. Create a java class called Capabilities.java in the setup package.

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import java.net.URL;
import java.util.concurrent.TimeUnit;
public class Capabilities extends DesiredCapabilities {
protected AndroidDriver androidDriver;
private AppiumDriverLocalService service;
protected void preparation() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "10");
capabilities.setCapability("deviceName", "25e2e78c130d7ece");
capabilities.setCapability("appPackage", "com.appium");
capabilities.setCapability("appActivity", "com.appium.MainActivity");
service = AppiumDriverLocalService.buildDefaultService();
service.start();
String service_url = service.getUrl().toString();
System.out.println("Appium Service Address: " + service_url);
androidDriver = new AndroidDriver(new URL(service_url), capabilities);
androidDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
public void stopServer() {
service.stop();
}
}

8. Do check the device name with the following command. Note that your environment settings are correct.

deni.rizal@Ralalis-MacBook-Pro ~ % adb devices 
List of devices
attached 25e2e78c130d7ece device

9. Create two test files named DoPositiveTest.java and DoNegativeTest.java

DoPositiveTest.java

package steps;import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import org.openqa.selenium.WebElement;
import setups.Capabilities;
public class DoPositiveTest extends Capabilities {@Before({"@RaraliQATeam"})
public void setup()throws Exception{
preparation();
}
@Given("^Input Data and Do Calculate Price$")
public void doPositifTest() throws Throwable {
androidDriver.findElementByAccessibilityId("homeButton").click();
WebElement hargaFromApp = androidDriver.findElementByAccessibilityId("priceInput");
WebElement disconFromApp = androidDriver.findElementByAccessibilityId("discountInput");
hargaFromApp.sendKeys("80000");
disconFromApp.sendKeys("20");
String dataInput = hargaFromApp.getText();
Double dataInputDouble = Double.parseDouble(dataInput);
Double dataInpoutDiskon = Double.parseDouble(disconFromApp.getText());
androidDriver.findElementByAccessibilityId("calculationButton").click();
WebElement totalhargaFromApp = androidDriver.findElementByAccessibilityId("totalInfo");
Double totalHarfaFromAp = Double.parseDouble(totalhargaFromApp.getText());
Double totalharga = dataInputDouble - (dataInputDouble * (dataInpoutDiskon / 100));
if (!totalHarfaFromAp.equals(totalharga)) {
throw new Exception(" Error ");
}
}
}

DoNegativeTest.java

package steps;import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import org.openqa.selenium.WebElement;
import setups.Capabilities;
public class DoNegativeTest extends Capabilities {@Before({"@RaraliQATeam"})
public void setup()throws Exception{
preparation();
}
@Given("^Input Wrong Data and Do Calculate Price$")
public void doNegativefTest() throws Throwable {
androidDriver.findElementByAccessibilityId("priceInput").clear();
androidDriver.findElementByAccessibilityId("discountInput").clear();
androidDriver.findElementByAccessibilityId("totalInfo").clear();
androidDriver.findElementByAccessibilityId("homeButton").click();
WebElement hargaFromApp = androidDriver.findElementByAccessibilityId("priceInput");
WebElement disconFromApp = androidDriver.findElementByAccessibilityId("discountInput");
hargaFromApp.sendKeys("Not Numnber");
disconFromApp.sendKeys("20");
String dataInput = hargaFromApp.getText();
androidDriver.findElementByAccessibilityId("calculationButton").click();
WebElement popupInfo = androidDriver.findElementByAccessibilityId("NaNInfo");
WebElement infoTotal = androidDriver.findElementByAccessibilityId("totalInfo");;
if(popupInfo.isDisplayed()&& infoTotal.getText().equalsIgnoreCase("NaN")){
System.out.println("Negative Test Pass");
}else {
throw new Exception("Neagtive Test Not Pass");
}
}
@After({"@RaraliQATeam"})
public void stop(){
stopServer();
}
}

10. Build the project using mvn clean install.

11. Install Demo.apk, which we will test on our mobile device, and don't forget to activate debugging mode.

12. Lest test and see the result here. Demo on mobile and desktop console run.

All of the above is a beginning. I hope that we will start making mobile automatic tests for all Mobile Ralali Products in the coming quarter. Next, our flow implementation will use like below.

CI/CD pipeline and fully supported by device farm for mobile automation test

Let’s make an impact to implement a world-class application test! If you are interested to join our Quality Engineering team sign yourself up to Ralali.com Career https://ralali.freshteam.com/jobs.

Stay tuned for our Medium channel and wait for further articles to find more advanced topics on Appium and Cucumber.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
D. Husni Fahri Rizal

D. Husni Fahri Rizal

Engineering Leader | Start-Up Advisor | Agile Coach | Microservice Expert | Professional Trainer