Creating Automated Mobile Tests with Cucumber and AWS Device Farm

Automated mobile testing is critical to ensure robust and reliable applications. By leveraging tools like Cucumber for behavior-driven development (BDD) and AWS Device Farm for device testing, you can streamline the testing process and enhance the quality of your mobile apps. This article will guide you through setting up and executing automated mobile tests using Cucumber and AWS Device Farm.

Table of Contents

  1. Introduction to Cucumber and AWS Device Farm
  2. Setting Up the Environment
  3. Writing Cucumber Tests
  4. Configuring AWS Device Farm
  5. Integrating Cucumber with AWS Device Farm
  6. Executing Tests and Analyzing Results
  7. Best Practices
  8. Conclusion

1. Introduction to Cucumber and AWS Device Farm

Cucumber

Cucumber is a tool for running automated tests written in plain language. It enables teams to describe how software should behave in business-readable language, making it easier for all stakeholders to understand the functionality being tested.

AWS Device Farm

AWS Device Farm is a service that allows you to test your mobile apps on a wide variety of real devices in the AWS Cloud. It supports Android, iOS, and web apps, providing a comprehensive environment for testing across multiple device configurations and operating systems.

2. Setting Up the Environment

To get started, you need to have the following tools installed:

  • Java Development Kit (JDK)
  • Maven
  • Android SDK
  • Cucumber-JVM
  • Appium (for interacting with mobile apps)

Installing JDK

Ensure you have JDK installed. You can download it from Oracle’s website.

Installing Maven

Maven can be installed from Maven’s official website.

Setting Up Android SDK

Download and install the Android SDK from Android Developer’s site.

Installing Cucumber-JVM

Add the following dependencies to your pom.xml:

<dependencies>
    <!-- Cucumber dependencies -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>6.10.4</version>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>6.10.4</version>
    </dependency>
    <!-- Appium dependencies -->
    <dependency>
        <groupId>io.appium</groupId>
        <artifactId>java-client</artifactId>
        <version>7.3.0</version>
    </dependency>
    <!-- JUnit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.1</version>
    </dependency>
</dependencies>

3. Writing Cucumber Tests

Creating Feature Files

Create a feature file under src/test/resources/features. Let’s name it login.feature.

Feature: Login functionality

  Scenario: Successful login with valid credentials
    Given the user is on the login page
    When the user enters valid credentials
    And the user clicks the login button
    Then the user should be redirected to the home page

Writing Step Definitions

Create a step definition class under src/test/java/stepdefinitions. Let’s name it LoginSteps.java.

package stepdefinitions;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.cucumber.java.en.*;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.MalformedURLException;
import java.net.URL;

public class LoginSteps {
    private AppiumDriver<MobileElement> driver;

    @Given("the user is on the login page")
    public void the_user_is_on_the_login_page() throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("deviceName", "emulator-5554");
        capabilities.setCapability("app", "/path/to/your/app.apk");

        driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), capabilities);
    }

    @When("the user enters valid credentials")
    public void the_user_enters_valid_credentials() {
        MobileElement usernameField = driver.findElementById("com.example:id/username");
        MobileElement passwordField = driver.findElementById("com.example:id/password");

        usernameField.sendKeys("validUser");
        passwordField.sendKeys("validPassword");
    }

    @When("the user clicks the login button")
    public void the_user_clicks_the_login_button() {
        MobileElement loginButton = driver.findElementById("com.example:id/login_button");
        loginButton.click();
    }

    @Then("the user should be redirected to the home page")
    public void the_user_should_be_redirected_to_the_home_page() {
        MobileElement homePageElement = driver.findElementById("com.example:id/home");
        assert(homePageElement.isDisplayed());
    }
}

4. Configuring AWS Device Farm

Setting Up AWS Device Farm

  1. Create a Project: Go to the AWS Device Farm console and create a new project.
  2. Upload Your App: Upload the .apk or .ipa file of your application.
  3. Create a Test Suite: Choose “Create a new run” and select the type of test (Appium for this case).

Preparing Your Tests for AWS Device Farm

You need to package your tests into a single zip file. The structure should be as follows:

tests
└── src
    └── test
        ├── java
        │   └── stepdefinitions
        │       └── LoginSteps.java
        └── resources
            └── features
                └── login.feature
pom.xml

Zip this structure and upload it to AWS Device Farm.

5. Integrating Cucumber with AWS Device Farm

Setting Up the Device Pool

  1. Select Devices: Choose the devices you want to test on. AWS Device Farm provides a wide range of devices to select from.
  2. Configure Test Execution: Configure the test execution settings as per your requirements.

Running the Tests

  1. Start the Test: Initiate the test run from the AWS Device Farm console.
  2. Monitor the Execution: Monitor the test execution and ensure all tests are running as expected.

6. Executing Tests and Analyzing Results

Viewing Results

Once the tests are executed, you can view the results in the AWS Device Farm console. The results include detailed logs, screenshots, and videos of the test execution.

Analyzing Failures

AWS Device Farm provides comprehensive logs and reports to help you analyze test failures. Use these reports to identify and fix issues in your application or tests.

7. Best Practices

  • Keep Tests Independent: Ensure that each test case is independent and does not rely on the outcome of other tests.
  • Use Realistic Test Data: Use data that closely mimics real-world usage to uncover potential issues.
  • Regularly Update Test Devices: Regularly update the list of devices in your device pool to cover the latest devices and OS versions.
  • Continuous Integration: Integrate your automated tests with a CI/CD pipeline to run tests automatically on every code change.

8. Conclusion

Automated mobile testing with Cucumber and AWS Device Farm offers a robust solution for ensuring the quality and reliability of your mobile applications. By following the steps outlined in this article, you can set up and execute automated tests efficiently. Remember to leverage the comprehensive features of AWS Device Farm to test across a wide range of devices and operating systems, ensuring your app performs well for all users.

If you found this article helpful and are interested in integrating Cucumber Automation Framework with AWS Device Farm, I suggest you check out some of the other articles I’ve written for this series:

If you’re looking for a deeper dive into some of the concepts and specifics discussed in my article, feel free to reach out to me directly or as always you can checkout the official AWS Device Farm Developer Documentation for more information.

Related Posts