Register for Prevent bottlenecks during mobile development on September 13
Register
White arrow point right
Testing

Appium With Cucumber: Using Them Together

Nabendu Biswas
Nabendu Biswas
Appium With Cucumber: Using Them Together
December 6, 2022
6
min read

Appium is a completely free-to-use mobile app automation framework, and Cucumber is a tool that uses the BDD (behavioral-driven development) approach to write test cases.

In this post, we'll touch on Appium and Cucumber, including how to use them together. Then, we'll create a project using both of them.

Appium is a completely open-source mobile app automation framework that you can use to test all types of mobile applications written using Kotlin, Java, Objective-C, Swift, React Native and Flutter

What Is Appium?

Appium is a completely open-source mobile app automation framework that you can use to test all types of mobile applications written using Kotlin, Java, Objective-C, Swift, React Native and Flutter.

Test cases in Appium can be written in languages like Java, Python, JavaScript, and PHP. After executing the tests, the app will run automatically on the connected device and show the user interactions as per the test cases.

What Is Cucumber?

Cucumber is a tool that uses the BDD (behavioral-driven development) approach to write test cases. The test cases are written like English statements in a language called Gherkin.

We use Given, When, and Then in it, as shown in the screenshot below, which is taken from the official site.

cucumber homepage

What Is the BDD Framework?

The behavioral-driven development framework enables software testers to write test cases in plain English. It's liked by testers, managers, and other stakeholders who have a problem understanding complicated programming languages.

Cucumber is one of the top frameworks to implement BDD. It uses a language called Gherkin, in which we can write test cases.

Is Cucumber Used for Automation Testing?

Cucumber can be used for automation testing with the help of other frameworks.

Selenium is the top framework for automation testing in web apps. Furthermore, it works very well with Cucumber.

We can write test cases in Cucumber using Gherkin for automated testing. Then, these test cases will be run on the browser as if a real user is interacting.

Cucumber also can be used for mobile testing but, again, it needs to be used with a mobile automation framework like Appium

Can Cucumber be Used for Mobile Testing?

Cucumber also can be used for mobile testing but, again, it needs to be used with a mobile automation framework like Appium.

So, can you use Cucumber with Appium? Yes, we can use Cucumber with Appium. We're going to learn to do that in this post.

Requirement for Test Cases with Cucumber

To start, we need to have JDK 8 installed on our system. The steps for the same can be found in our earlier post here.

Aside from this, we also need the Appium server to be installed on our system.

Additionally, we need Android Studio if we're opening our app through an emulator. So, download Android Studio from this official link. Open the exe(Windows)/dmg(Mac) file and follow the instructions to install Android Studio.

Alternatively, you can choose to connect to a real Android device.

Project Setup in IDE

We'll write our test cases in Java. So, we need any Java-based IDE (integrated development environment) like Eclipse or IntelliJ.

In this post, we'll use Eclipse to write our test cases. Open Eclipse, and then click on File > New > Other.

Project Setup in IDE

Then, write maven in the Wizards search box. After that, select "Maven Project" and click on the "Next" button.

write maven in search box

Make sure that the first checkbox for "Create a simple project" is selected. After that, click on the "Next" button.

Create a simple project

Now, give the Group ID and Artifact ID, which should be the same. Also, give a nice description.

give the Group ID and Artifact ID

Now, our project will be opened. Here, we'll update our pom.xml file to add the dependencies for the project.


<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>
cucumber code

Cucumber Code in Feature File

We'll write the Cucumber code in a file ending with the .feature extension. This is needed because Cucumber uses the language of Gherkin.

We'll first create a new folder called features inside src/main/resources.

first create a new folder called features

Next, create a file called calcScenario.feature inside the features folder.

create a file called calcScenario.feature

In this post, we'll test the built-in calculator app on a connected Android phone.

Now, in the calcScenario.feature file, we have to give a feature. It's Adding two numbers in the Calculator in our case.

Next, the scenario will define the test. Here, the Given, When, and Then functions define the different features in the test.

Generally, there will be more scenarios, and they will be written by testers.

adding code on calcscenario feature

Writing Test Cases With Java

Now, the steps in the feature file need to be converted into real test cases. Most of the time, this step is done by the developer.

We'll write test cases in Java. So, right-click on src/test/java, and then click on "New." After that, select "Class" to create a new class.

So, right-click on src/test/java, and then click on "New."

In the next pop-up, give the test file a name. We've named it CucumburCalcTest.

In the next pop-up, give the test file a name

In the CucumburCalcTest.java, we've imported the required packages first.


    package testcucumberappium;
    import java.net.URL;
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.remote.DesiredCapabilities;
    
    import cucumber.api.java.en.Given;
    import cucumber.api.java.en.Then;
    import cucumber.api.java.en.When;
    import io.appium.java_client.AppiumDriver;
we've imported the required packages

Next, in CucumburCalcTest.java, we imported our Appium driver first. Then, in the @Given method called user_open_calculator_app(), we have to give the desired capabilities for Appium.

Notice that it's the same Given statement from the calcScenario.feature file. Here, we've given the device name—it's UDID—and also the app name.

Now, we've given the URL for the Appium server. Let's connect to the Appium driver by passing the URL and capabilities.

Before going to the next method, we need to find the IDs of some of the calculator app.

Next, in the @When method called user_adds_two_numbers(), we'll first find the 5, 3, +, and = buttons by their IDs before assigning them to respective variables. Again, the same When statement comes from the calcScenario.feature file.

Finally, we'll do the @Then method called user_closed_calculator_app(); here, we're just quitting the test.

This is the Then statement from the calcScenario.feature file.

Then statement from the calcScenario.feature file.

Automation Testing

To start our automated testing, make sure that our Android device is connected to our Mac system. Run the command adb devices, which will show our connected device.

Run the command adb devices, which will show our connected device

We also need to install the package of maven on our Mac through the brew command. This is required for the Java project. So, give the command below from a Mac terminal.


brew install maven
brew install maven

Now, from the project folder, give the command below. This will install all the packages in our project from the pom.xml file.


    mvn clean install
mvn clean install

Make sure to start the Appium server. We can do this from the installed GUI server application. After opening it, click on the startServer button.

Make sure to start the Appium server

This will start a locally running Appium server on port 4723.

start a locally running Appium server on port 4723

Now, back in the project, right-click on the CucumburCalcTest.java file. Then, click on Run As > Maven test.

right-click on the CucumburCalcTest.java file

It'll show the test cases succeeded in the console.

test cases succeeded in the console

In our connected Android device, 5 will be pressed first, followed by +, then 3, and finally =. This will be done automatically. And it seems as if the user is typing it.

calculator app

Conclusion

In this post, we've learned about the Cucumber testing framework. It allows us to write automated test cases when used with Appium. In Cucumber, we can write English statements in a language called Gherkin.

Additionally, we set up a Java project in Eclipse. After that, we learned to write test cases with Cucumber. Finally, we were able to run these test cases on the attached Android device.

However, this all seems like a complicated process. And here, also, we have to write test cases.

So, instead, we can just get the APK or IPA file and try Waldo. With this testing tool, we can avoid writing test cases. So, start a free trial today.

Automate testing for your mobile app.Start building your first test for free

Subscribe to our newsletter for the latest news and resources

Thank you for subscribing to our blog!