Conducting Test Automation Using Appium & Cucumber on a Cloud-based Infrastructure

Piotr Siuta

Introduction

In today’s digital age, mobile applications have become indispensable tools driving business innovation across diverse sectors worldwide. Recent studies reveal a staggering count of 8.93 million mobile apps, with an estimated 255 billion downloads globally. This sheer volume underscores the pivotal role mobile apps play in facilitating myriad business operations. From banking transactions to online shopping, from securing insurance policies to scheduling medical appointments, and from accessing healthcare advice to settling utility bills—mobile apps serve as versatile companions for users in their daily tasks. Amidst this dynamic landscape, Mobile App Test Automation emerges as a crucial mechanism for meticulously testing and refining these indispensable apps.

This article explores the nuanced realm of automation testing, merging the prowess of Appium and Cucumber on a cloud-based framework. The methodology is simplified and accessible—just your mobile app, the essential setup with Appium and Cucumber, and a reliable cloud testing infrastructure are all that’s required to embark on this journey.

What is Appium?

Appium is an open-source automation tool used for the testing mobile applications. It allows developers and testers to automate testing of mobile apps across different platforms like iOS, Android, and Windows using a single API. Appium supports multiple programming languages such as Java, Python, JavaScript, Ruby, and more, making it flexible for developers to write tests in their preferred language.

appium

Key features of Appium include:

  1. Cross-platform compatibility: Appium supports testing applications across different platforms like iOS, Android, and Windows, eliminating the need for separate test scripts for each platform.
  2. Supports multiple programming languages: Developers can write test scripts in their preferred programming language using Appium’s client libraries for Java, Python, JavaScript, Ruby, and other languages.
  3. No need for app modification: Appium allows testing of native, hybrid, and mobile web applications without requiring any modifications to the app code.
  4. Supports multiple automation frameworks: Appium supports popular automation frameworks like Selenium WebDriver, making it easier for testers already familiar with these frameworks to transition to mobile app testing.
  5. Seamless integration with testing frameworks: Appium can be easily integrated with popular testing frameworks like TestNG, JUnit, XCTest, and others, allowing testers to leverage existing frameworks and tools.

Overall, Appium simplifies the process of automating mobile app testing by providing a unified platform that supports multiple platforms, programming languages, and automation frameworks.

Understanding Cucumber and Its Role in Optimizing Test Automation

Test automation with Cucumber involves using Cucumber, a popular behavior-driven development (BDD) tool, to automate testing scenarios in a human-readable format. Cucumber allows testers and developers to write test cases in plain text using a simple, English-like syntax known as Gherkin. These test cases are then translated into executable code using step definitions written in programming languages such as Java, Ruby, JavaScript, etc.

Here’s a basic overview of how test automation with Cucumber works:

  1. Writing Feature Files: Test scenarios are written in feature files using the Gherkin syntax. Feature files describe the behavior of the system in a structured format that is easy to read and understand. Each feature file typically contains multiple scenarios, each representing a test case.

An example feature file (login.feature):

Feature: User Login
  As a registered user
  I want to be able to log in to my account
  So that I can access my personalized content

  Scenario: Successful login
    Given I am on the login page
    When I enter my username and password
    And I click the login button
    Then I should be logged in successfully

Test automation with Cucumber offers several benefits, including improved collaboration between stakeholders, easier maintenance of test scripts, and enhanced readability of test cases. Additionally, it promotes a behavior-driven approach to testing, where tests are written from the perspective of end-users, focusing on the application’s behavior rather than its implementation details.

Example step definitions (login_steps.java):

import io.cucumber.java.en.*;

public class LoginSteps {
    @Given("^I am on the login page$")
    public void navigateToLoginPage() {
        // Code to navigate to the login page
    }

    @When("^I enter my username and password$")
    public void enterCredentials() {
        // Code to enter username and password
    }

        @And("^I click the login button$")
    public void clickLoginButton() {
        // Code to click the login button
    }

    @Then("^I should be logged in successfully$")
    public void verifySuccessfulLogin() {
        // Code to verify successful login
    }
}

An Example of Feature File Code Utilized in Cucumber

Feature: Verify Product Search Functionality

  Scenario Outline: Verify user can search for products on the website
    Given User is on the homepage of the shopping site
    When User enters the search keyword <keyword>
    And User clicks on the search button
    Then User should see search results for the entered keyword

    Examples:
      | keyword    |
      | headphones |

An Example of Code Used in the Step Definition File

public class ProductSearch {
    private final WebDriver driver = new ChromeDriver();

    @Given("^User is on the homepage of the shopping site$")
    public void userIsOnHomePage() {
        // Implementation to navigate to the homepage
    }

    @When("^User enters the search keyword ([^\"]*)$")
    public void userEntersSearchKeyword(String keyword) {
        WebElement searchField = driver.findElement(By.id("search-field"));
        searchField.sendKeys(keyword);
    }

    @And("^User clicks on the search button$")
    public void userClicksOnSearchButton() {
        WebElement searchButton = driver.findElement(By.id("search-button"));
        searchButton.click();
    }

    @Then("^User should see search results for the entered keyword$")
    public void verifySearchResults() {
        // Implementation to verify search results
    }
}

In this example, we’re testing the product search functionality on the website. The feature file outlines the steps for searching for a product using different keywords, and the step definitions correspond to each step in the feature file, implementing the actions to be performed during the test.

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

Cucumber can be used for mobile testing by integrating it with mobile automation frameworks such as Appium or Calabash. Here’s a general overview of how to use Cucumber for mobile testing and the necessary requirements:

Necessary Requirements:

  1. Mobile Automation Framework – Appium
    Choose a mobile automation framework compatible with Cucumber for writing and executing test scripts
  2. Programming Language and Libraries:
    Have a programming language runtime installed (e.g., Java, Ruby) along with the necessary libraries and dependencies for mobile testing
  3. Mobile Devices or Emulators/Simulators:
    Have access to physical mobile devices or emulators/simulators for testing.
    Ensure that the mobile devices or emulators/simulators are properly configured and accessible from the testing environment.
  4. App Under Test:
    Have the mobile application (APK for Android or IPA for iOS) available for testing.
    Ensure that the mobile application is compatible with the chosen mobile automation framework.
  5. Development Environment:
    Set up the development environment with IDEs (Integrated Development Environments) or text editors for writing and editing test scripts.
    Set up the environment for mobile automation by configuring the desired capabilities for the mobile device or emulator/simulator in your test scripts.
  6. Writing Feature Files:
    Define the test scenarios for mobile testing in feature files using the Gherkin syntax. Feature files should describe the behavior of the mobile application from a user’s perspective.
  7. Implementing Step Definitions:
    Write step definitions in the programming language of choice (e.g., Java, Ruby) to define the actions corresponding to each step in the feature files.
    Use the Appium APIs to interact with the mobile application during testing. This includes actions such as tapping on elements, entering text, swiping, etc.
  8. Running Tests:
    Execute the Cucumber tests by running the test suite using the chosen programming language’s test runner (e.g., JUnit for Java, RSpec for Ruby).
    Ensure that the mobile device or emulator/simulator is connected and accessible during test execution.

The aforementioned tools offer a diverse set of features and advantages that enhance the automated testing process. These include:

  1. Page Object Model (POM)
  2. Cucumber for Behavior-Driven Testing
  3. CI/CD Workflow Support
  4. Parallel Execution
  5. Cross-Platform Testing
  6. Dynamic Appium Service Control
  7. HTML Test Reports
  8. Command-Line Interface (CLI) Support

By meeting these requirements and following the outlined steps, you can effectively use Cucumber for mobile testing in conjunction with a mobile automation framework.

Setting Up and Configuring the Environment for Installing Appium and Cucumber

  1. Install the Java Development Kit (JDK):
    Download and install the Java Development Kit (JDK) from the official Oracle website: Java SE Downloads.
    Follow the installation instructions provided for your operating system.
  2. Install Node.js:
    Download and install Node.js from the official website: Node.js.
    Follow the installation instructions provided for your operating system.
  3. Install Appium:
    Open a terminal or command prompt.
    Install Appium globally using npm (Node Package Manager) by running the following command:
npm install -g appium

Install Appium Doctor, a utility to verify Appium installation and its dependencies:

npm install -g appium-doctor
  1. Verify Appium Installation:
    Run Appium Doctor to verify Appium installation and its dependencies:
appium-doctor

Address any issues reported by the Appium Doctor before proceeding further.

  1. Install Cucumber with Java:
    Create a new directory for your Cucumber project.
    Navigate to the directory using the terminal or command prompt.
    Initialize a new Maven project by running:
mvn archetype:generate -DgroupId=com.example -DartifactId=my-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Add Cucumber dependencies to your pom.xml file:

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>6.12.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>6.12.2</version>
    <scope>test</scope>
</dependency>
  1. Create the Project Structure:
    Inside your project directory, create the following structure:
|-- src
|   |-- test
|       |-- java
|           |-- *.java
|       |-- resources
|           |-- *.feature
  1. Write Feature Files:
    Create feature files in the src/test/resources directory using the Gherkin syntax to describe test scenarios.
  2. Implement Step Definitions:
    Write step definitions in Java files in the src/test/java directory to define the actions corresponding to each step in the feature files.
  3. Configure Appium:
    Configure Appium to start the Appium server programmatically or via a command line before running tests.
    Ensure your Appium server is running and accessible.
  4. Run Tests:
    Execute your Cucumber tests by running the JUnit test runner class.
    Alternatively, you can use the Maven Surefire Plugin to run tests from the command line:
mvn test
  1. Analyze Results:
    After test execution, review the test results and address any failures or issues.
    By following these steps, you can set up and configure the environment for installing Appium and Cucumber with Java and start writing and executing automated tests for your mobile applications.

How Cloud Testing Platforms Enhance Test Automation with Appium and Cucumber

  • Scalability: Cloud testing platforms provide scalable infrastructure, allowing you to run tests on a large number of real devices and emulators/simulators simultaneously. This scalability ensures that you can execute tests across various device configurations, operating systems, and screen sizes, enabling comprehensive test coverage.
  • Device Coverage: Cloud testing platforms offer access to a vast array of real devices and emulators/simulators. This extensive device coverage allows you to test your mobile applications on a wide range of devices, including the latest models and operating system versions, ensuring compatibility and optimal performance across different devices.
  • Parallel Execution: Cloud testing platforms support parallel test execution, enabling you to run multiple test suites concurrently across multiple devices. This parallel execution significantly reduces the overall test execution time, accelerating the feedback loop and enabling faster delivery of high-quality software.
  • On-Demand Provisioning: Cloud testing platforms provide on-demand provisioning of testing environments, eliminating the need for manual setup and configuration of devices and infrastructure. With on-demand provisioning, you can quickly spin up testing environments as needed, increasing efficiency and reducing the setup time.
  • Remote Access: Cloud testing platforms offer remote access to real devices and emulators/simulators, allowing testers and developers to interact with the devices remotely for debugging and troubleshooting purposes. Remote access enables collaboration among team members located in different geographical locations, facilitating efficient testing and problem resolution.
  • Integration with CI/CD Pipelines: Cloud testing platforms seamlessly integrate with continuous integration and continuous delivery (CI/CD) pipelines, enabling automated execution of tests as part of the software development lifecycle. Integration with CI/CD pipelines ensures that tests are executed automatically whenever there is a code change, helping detect and address issues early in the development process.
  • Reporting and Analytics: Cloud testing platforms provide comprehensive reporting and analytics capabilities, allowing you to track test results, monitor test execution metrics, and analyze test coverage. Detailed reports and analytics enable stakeholders to gain insights into the quality of the application and make informed decisions about further development and release.

Overall, cloud testing platforms enhance test automation with Appium and Cucumber by providing scalable infrastructure, extensive device coverage, parallel execution, on-demand provisioning, remote access, seamless integration with CI/CD pipelines, and robust reporting and analytics capabilities. These advantages contribute to faster, more efficient, and higher-quality mobile application testing.

Summary

In the Appium Cucumber guide provided earlier, we explored a comprehensive tutorial outlining the installation steps for Appium and Cucumber. Additionally, we delved into leveraging these tools to conduct mobile testing seamlessly on a cloud-based testing platform. The advantages were highlighted, with emphasis on cloud testing platforms, emphasizing their cost-efficiency and expedited testing processes across diverse platforms.

References

  • https://medium.com

Meet the geek-tastic people, and allow us to amaze you with what it's like to work with j‑labs!

Contact us