A Comprehensive Guide to Conducting Test Automation Using Appium & Cucumber on a Cloud-based Infrastructure
Tags: devops, testing
Published: 2023-07-30 08:38:46

The widespread use of mobile applications in our daily lives has created a demand for seamless integration and reliable performance. Modern tech-savvy users expect mobile apps to solve their problems seamlessly and without fail. However, with bugs, navigation issues, and security breaches posing significant challenges, app developers must ensure rigorous mobile app testing protocols before launching their products. This is where the power of test automation comes in. By leveraging open-source frameworks like Appium and Cucumber, developers can ensure their apps are tested thoroughly, delivering a superior user experience across multiple devices. With [Gartner](https://www.gartner.com/account/signin?method=initialize&TARGET=http%3A%2F%2Fwww.gartner.com%2Fdocument%2F3975493%3Fref%3DsolrAll%26refval%3D364167970) predicting a significant rise in large enterprises to leverage AI-enabled test automation tools that support continuous testing across the entire DevOps life cycle by 2024, there has never been a better time to invest in mobile app testing solutions that can scale alongside your business.

In this blog, we explore the intricacies of automation testing using the dynamic duo of Appium and Cucumber on a cloud platform. The process is streamlined and hassle-free - all you require is your mobile application, the necessary infrastructure set up using Appium and Cucumber, and a reliable cloud testing platform.

**What Is Appium and How Does It Simplify Test Automation?**

Appium is an entirely open-source framework that allows developers to test various mobile applications written in Kotlin, Java, Objective-C, Swift, React Native, and Flutter. Appium's cross-platform capability means that it can be used to test native, hybrid, and web applications on both Android and iOS platforms. Additionally, it goes well with all operating systems, making it a reliable choice for developers across different environments.

Writing test cases in Appium is a breeze, supporting several programming languages such as Java, Python, JavaScript, Ruby, and more if they can speak W3C WebDriver Protocol. The application will run automatically on the connected device upon executing the tests, accurately reflecting the user interactions specified in the test cases. With Appium automation testing, developers can rest assured that their mobile applications will be thoroughly tested and optimized for top-notch performance, thereby meeting the expectations of modern users.

**An Overview of Cucumber and Its Role in Streamlining Test Automation**

Test automation with Cucumber is a game-changer for software testers, project managers, and stakeholders who struggle to comprehend complex programming languages. The revolutionary behavioral-driven development (BDD) framework enables testers to write test cases in plain languages such as English, Japanese, etc., making it easy to understand and interpret.

At the heart of Cucumber lies its BDD approach, which leverages the Gherkin language to express test cases as simple, readable statements. This robust framework is regarded as one of the top tools to implement BDD, enabling developers to test their software applications with greater accuracy, efficiency, and speed. By combining the strengths of Cucumber with other advanced testing tools, developers can ensure that their mobile applications are thoroughly tested and optimized for maximum performance and user satisfaction.

Cucumber is a powerful [tool for automation testing](https://www.headspin.io/blog/what-is-test-automation-a-comprehensive-guide-on-automated-testing) when used with other frameworks. Appium, the leading automation testing framework for Web/mobile apps, integrates seamlessly with Cucumber.

To write test scenarios using Cucumber, testers use the Gherkin language and its associated keywords, which include:

- Feature: This describes a software feature.
- Scenario: This defines how a system should behave under certain circumstances.
- Given: This sets the context where the scenario happens.
- When: This is used to interact with the system.
- Then: This keyword is used to check the outcome of the interaction.
- But: This keyword adds more steps to the scenario's Given, When, or Then section.

Cucumber reads test specifications from plain-language text files called features, which must follow a basic syntax rule described in Gherkin. Cucumber associates the feature file with step definitions, mapping the business-readable language of each step into code.

When test cases written in Gherkin are run on a browser, they mimic the actions of a real user, checking that the system behaves as expected under various circumstances. Cucumber executes each scenario as a list of steps, making it easy for testers to track progress and troubleshoot any issues.

**A sample Feature File Code used in Cucumber:**

Feature: Verify LogIn to the website

  Scenario Outline: Verify user can log into the shopping site
    Given User is on our website
    When User select the username field
    And User enters the name <name>
    And User selects the password field
    And User enters the password <password>
    And User clicks on the log-in button
    Then User is on the welcome page

    Examples:
      | name|password |
      | headspin | headspin-password |

**A sample Code used in the Step Definition File:**

public class LogInPage {
    private final WebDriver driver = new ChromeDriver();
    @Given("^User is on the website$")
    public void userIsOnTheWebsite() {
        Assert.assertEquals("<expected url>", driver.getCurrentUrl());
    }
 
    @When("^User enters the name ([^\"]*)$")
    public void userEntersTheNameName(String name) {
        driver.findElement(By.xpath(".//*[@id='username-field']/a")).sendkeys(name);
    }
 
    @And("^User enters the password ([^\"]*)$")
    public void userEntersTheNameName(String password) {
        driver.findElement(By.xpath(".//*[@id='password-field']/a")).sendkeys(password);
    }
 

    @And("^User clicks on the log-in button$")
    public void userClicksOnTheLetSShopButton() {
        driver.findElement(By.xpath(".//*[@id='login']/a")).click();
    }
 
    @Then("^User is on the welcome page$")
    public void userIsAbleToRedirectsToTheProductsPage() {
        Assert.assertEquals("<expected url after logged in>", driver.getCurrentUrl());
    }
}

**How Do You Use Cucumber for Mobile Testing, and What Are the Necessary Requirements?**

For mobile testing, Cucumber can also be used in conjunction with a mobile automation framework such as Appium. Before testing with Appium and Cucumber, we must install several tools on our computer or laptop. 

The following example is a list of frameworks or tools that must be installed. This explanation will be primarily focused on the macOS operating system:

- Java
- IntelliJ
- Cucumber for Java Plugin
- Gherkin Plugin
- Appium
- Android SDK

To ensure a smooth start, let us first delve into the functions of each tool listed below:

- Java, a widely used programming language, serves as the foundation for creating automated tests. It is worth noting that Appium supports all WebDriver Protocol-compatible programming languages, including Java, JavaScript with Node.js, Python, Ruby, C#, etc.
- IntelliJ IDEA, an integrated development environment (IDE) developed by JetBrains, provides developers with a comprehensive toolkit for software development. It is available in a proprietary commercial edition as well as an Apache 2 Licensed community edition, both of which can be used for commercial development.
- Cucumber, a Behavior Driven Development (BDD) framework, simplifies the development process and provides detailed reports for the team.
- Gherkin, a specification format used for Cucumber, is a domain-specific language that helps describe business behavior without needing detailed implementation.
- Appium, a versatile library, enables developers to create automated mobile application tests for both Android and iOS devices.

The tools above provide an array of features and benefits that aid in the automated testing process, including:

- Adopting the Page Object Model for test design, a widely used pattern in automated testing.
- Utilizing Cucumber for behavior-driven testing.
- Supporting CI/CD workflows.
- Enabling parallel execution. 
- Supporting automated testing for both Android and iOS platforms.
- Programmatically starting and stopping the Appium service.
- Generating HTML test reports.
- Supporting CLI for ease of use.

**Setting Up and Configuring the Environment for Appium Cucumber Installation**

To set up and configure the environment for Appium Cucumber testing, you'll need to follow these steps:

**Step 1: Install IntelliJ IDEA**

● Download and install the latest version of IntelliJ IDEA from the official website: https://www.jetbrains.com/idea/

**Step 2: Create a new Gradle project**

● Launch IntelliJ IDEA and select "Create New Project" from the welcome screen.

● Choose "Gradle" as the project type and click "Next."

● Select "Java" as the project SDK and click "Next."

● Enter a name for your project and choose a location for it. Click "Finish" to create the project.

**Step 3: Configure Gradle dependencies**

● Open the build.gradle file in your project.

● Add the following dependencies to the dependencies block:

dependencies {
            testImplementation 'io.cucumber:cucumber-java:6.11.0'
            testImplementation 'io.cucumber:cucumber-junit:6.11.0'
            testImplementation 'io.appium:java-client:8.5.1'
        }

**Step 4: Sync Gradle and download dependencies**

● Click on the "Sync" button or use the Gradle tool window to sync the project and download the specified dependencies.

**Step 5: Create a new feature file**

● Right-click on the src/test/resources directory and select "New" -> "File."

● Enter a name for your feature file with the .feature extension, e.g., example.feature.

● Write your BDD-style scenarios in the feature file.

**Step 6: Create step definitions**

● Right-click on the src/test/java directory and select "New" -> "Java Class."

● Enter a name for your step definition class, e.g., ExampleSteps.

● Add the necessary import statements and write your step definitions for Cucumber.

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
 
public class ExampleSteps {
 
    @Given("^I have a precondition$")
    public void iHaveAPrecondition() {
        // Implement your given step
    }
 
    @When("^I perform an action$")
    public void iPerformAnAction() {
        // Implement your when step
    }
 
    @Then("^I should see the expected result$")
    public void iShouldSeeTheExpectedResult() {
        // Implement your then step
    }
}

**Step 7: Implement the Appium test code**

● Add Appium related test code into the class added in the previous step

● Inside the class, write the necessary code to set up the Appium driver, desired capabilities, and perform the required test actions. Here's an example:

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import org.junit.Assert;
import java.net.URL;
 
public class ExampleTest {
    final private AppiumDriver<MobileElement> driver;
 
    public void setUp() throws Exception {
        UiAutomator2Options options = new UiAutomator2Options()
                .setPlatformVersion("12.0")
                .setUdid("device udid")
                .setAppPackage("your.app.package")
                .setAppActivity("your.app.activity");
        URL appiumServerURL = new URL("http://localhost:4723");
        driver = new AndroidDriver(appiumServerURL, options);
    }

    // Cucumber code exist
 
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

**Step 8: Update code to match for the Cucumber syntax**

Update Before/After instead of setUp/tearDown

@RunWith(Cucumber.class)
@CucumberOptions()
public class ExampleTest {
    final private AppiumDriver<MobileElement> driver;

    @Before({"@LogInTest"})
    public void setup()throws Exception{
        UiAutomator2Options options = new UiAutomator2Options()
                .setPlatformVersion("12.0")
                .setUdid("device udid")
                .setAppPackage("your.app.package")
                .setAppActivity("your.app.activity");
        URL appiumServerURL = new URL("http://localhost:4723/wd/hub");
        driver = new AndroidDriver(appiumServerURL, options);
    }

    @Given("^I have a precondition$")
    public void iHaveAPrecondition() {
        // Implement your given step with driver
    }
 
    @When("^I perform an action$")
    public void iPerformAnAction() {
        // Implement your given step with driver
    }
 
    @Then("^I should see the expected result$")
    public void iShouldSeeTheExpectedResult() {
        // Implement your given step with driver
    }

    @After({"@LogInTest"})
    public void stop(){
        if (driver != null) {
            driver.quit();
        }
    }
}

**Step 9: Run the test**

● Right-click on the ExampleTest class and select "Run ExampleTest."

These are the basic steps to set up and configure the environment for Appium Cucumber testing. You'll need to customize the code according to your specific requirements and modify the feature files and step definitions accordingly.

**How Can Cloud Testing Platforms Benefit Test Automation with Appium and Cucumber?**

Appium and Cucumber are a powerful combination for test automation of iOS and Android mobile applications, and using cloud testing platforms can take it to the next level. Testing mobile applications on real devices can ensure that the application behaves as expected for end-users, but with so many devices and operating system combinations, traditional testing methods can be challenging and time-consuming. A cloud testing platform provides access to a range of real devices on the cloud at scale, which can increase test coverage and accelerate test execution time. A cloud testing platform can also provide access to the latest set of devices for testing, ensuring that the application is tested on the latest hardware and software. Combining Appium and Cucumber with a cloud testing platform can help achieve comprehensive and efficient mobile application testing.

**How Can Enterprises Utilize HeadSpin's Real Device Cloud For Efficient Automation Testing Using Appium and Cucumber?**

HeadSpin offers a real device cloud called Global Device Infrastructure, providing access to thousands of real mobile devices worldwide. Its rich features support effective mobile app testing, including testing distributed edge nodes.

By integrating Appium and Cucumber with HeadSpin, enterprises can enhance their test automation capabilities and gain additional functionality to monitor their apps on the cloud.

The HeadSpin Platform offers various benefits, including:

**1. Global Remote Testing**

HeadSpin allows developers to globally test their mobile apps in real-time across multiple networks, including 3G, 4G, 5G, and Wi-Fi, ensuring proper functionality in diverse settings for a global user base.

**‍2. Multiple Deployment Models**

HeadSpin provides multiple deployment models (on-prem, cloud-single/multi-tenant, create-your-own-lab) for secure test automation and data storage on the cloud.

**‍3. Seamless Integration**

HeadSpin integrates smoothly with renowned testing frameworks such as Appium and Cucumber for optimized app testing and monitoring. Developers can automate tests through Appium or Cucumber and then use HeadSpin to monitor the app's real-time performance.

**‍4. Test automation**

HeadSpin provides effortless CI/CD integration for automated app testing, supporting various open test frameworks. With actionable insights into app performance, bug identification, and quicker time to market, HeadSpin is the go-to option for thorough app testing.

**What Steps Are Involved in Executing Appium Cucumber Tests On the Cloud?**

To execute app testing with Appium and Cucumber on the HeadSpin Platform, you’ll need to provide WebDriver URL to send Appium commands to the HeadSpin Platform as below:


   UiAutomator2Options options = new UiAutomator2Options()
                .setPlatformVersion("12.0")
                .setUdid("device udid")
                .setAppPackage("your.app.package")
                .setAppActivity("your.app.activity");
        URL appiumServerURL = new URL("https://<HeadSpin WebDriver Url>");
        driver = new AndroidDriver(appiumServerURL, options);

This is a general step to execute app testing with Appium and Cucumber on the HeadSpin Platform. The code examples provided are simplified and may require additional customization based on your specific application and test requirements. Remember to replace placeholder code with actual implementation logic as needed.

**What's Next?**

We have explored the Cucumber testing framework, which, when paired with Appium, enables us to write automated test cases using English statements in a language known as Gherkin. We also established a Java project in IntelliJ and executed these test cases on an Android device. While the process may seem complex, it provides a comprehensive solution for automated testing.

Accelerate your test automation journey with HeadSpin's AI-based Platform and global device infrastructure. Run Appium Cucumber tests on thousands of real devices across various locations and achieve faster time-to-market.

_**Originally Published At:** https://www.headspin.io/blog/step-by-step-guide-to-run-appium-cucumber-tests-on-cloud_