A Use Case: E-commerce Testing with Cucumber and Java

Online shopping has revolutionized the way we purchase goods, emphasizing the need for reliable and efficient e-commerce websites. In this article, we explore a hypothetical use case for automating the testing of an online web store using the Cucumber automated testing framework with Java. We will delve into the features to test, provide examples of feature files, and detail Java step definitions, showcasing the power and versatility of Cucumber in ensuring the quality of e-commerce platforms.

Table of Contents:

  1. Introduction
  2. The E-commerce Web Store
  3. The Need for Automated Testing
  4. Feature 1: User Registration
  5. Feature 2: Product Search and Selection
  6. Feature 3: Shopping Cart Management
  7. Feature 4: Checkout Process
  8. Conclusion

1. Introduction

E-commerce is booming, and it’s critical to ensure that e-commerce websites operate smoothly, efficiently, and without bugs. Automated testing is essential for achieving this goal.

Cucumber, a widely-used tool for behavior-driven development, is an excellent choice for testing e-commerce websites. It uses plain text feature files that enable non-technical stakeholders to understand and contribute to the testing process. In this article, we will present a use case for automated testing in a hypothetical online web store named “ShopEz.” We’ll focus on essential features such as user registration, product search, shopping cart management, and the checkout process.

2. The E-commerce Web Store

Our hypothetical web store, “ShopEz,” offers a diverse range of products, including electronics, clothing, and accessories. ShopEz is designed for both desktop and mobile users to ensure a seamless shopping experience across devices.

3. The Need for Automated Testing

To remain competitive and meet customer expectations, ShopEz must ensure the website’s functionality, usability, and security. Manual testing is time-consuming and error-prone, making it unsuitable for a dynamic web store. Automated testing with Cucumber provides a robust solution.

4. Feature 1 – User Registration

Feature Description

User registration is a fundamental aspect of ShopEz. Users need to create an account to make purchases and access personalized services. This feature encompasses the following scenarios:

  1. Registration with valid user credentials.
  2. Registration with invalid email addresses.
  3. Registration with an existing email address.

Feature File (user_registration.feature)

Feature: User Registration
  As a potential customer
  I want to register for a ShopEz account
  So I can shop conveniently

  Scenario: Successful User Registration
    Given I am on the registration page
    When I fill in valid user credentials
    And I click the "Register" button
    Then I should see a "Welcome" message

  Scenario: Registration with Invalid Email
    Given I am on the registration page
    When I fill in an invalid email address
    And I click the "Register" button
    Then I should see an error message

  Scenario: Registration with an Existing Email
    Given I am on the registration page
    When I fill in an existing email address
    And I click the "Register" button
    Then I should see an error message

Step Definitions (UserRegistrationSteps.java)

import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import static org.junit.Assert.*;

public class UserRegistrationSteps {
    private WebDriver driver;

    public UserRegistrationSteps() {
        // Initialize your WebDriver instance here (e.g., Selenium, Appium).
    }

    @Given("I am on the registration page")
    public void i_am_on_the_registration_page() {
        driver.get("https://shopez.com/register");
    }

    @When("I fill in valid user credentials")
    public void i_fill_in_valid_user_credentials() {
        WebElement emailField = driver.findElement(By.id("email"));
        WebElement passwordField = driver.findElement(By.id("password"));
        WebElement confirmField = driver.findElement(By.id("confirmPassword"));

        emailField.sendKeys("testuser@example.com");
        passwordField.sendKeys("P@ssw0rd");
        confirmField.sendKeys("P@ssw0rd");
    }

    @When("I fill in an invalid email address")
    public void i_fill_in_an_invalid_email_address() {
        WebElement emailField = driver.findElement(By.id("email"));
        WebElement passwordField = driver.findElement(By.id("password"));
        WebElement confirmField = driver.findElement(By.id("confirmPassword"));

        emailField.sendKeys("invalid-email");
        passwordField.sendKeys("P@ssw0rd");
        confirmField.sendKeys("P@ssw0rd");
    }

    @When("I fill in an existing email address")
    public void i_fill_in_an_existing_email_address() {
        WebElement emailField = driver.findElement(By.id("email"));
        WebElement passwordField = driver.findElement(By.id("password"));
        WebElement confirmField = driver.findElement(By.id("confirmPassword"));

        emailField.sendKeys("existinguser@example.com");
        passwordField.sendKeys("P@ssw0rd");
        confirmField.sendKeys("P@ssw0rd");
    }

    @When("I click the {string} button")
    public void i_click_the_button(String buttonText) {
        WebElement button = driver.findElement(By.xpath("//button[contains(text(), '" + buttonText + "')]"));
        button.click();
    }

    @Then("I should see a {string} message")
    public void i_should_see_a_message(String message) {
        WebElement messageElement = driver.findElement(By.xpath("//div[contains(text(), '" + message + "')]"));
        assertNotNull(messageElement);
    }
}

In these Java-based step definitions, we interact with the registration page of the online store. We’ve covered scenarios for successful registration, registration with an invalid email address, and registration with an existing email address.

5. Feature 2 – Product Search and Selection

Feature Description

Allowing customers to search for and add products to their cart is essential for any e-commerce website. This feature includes the following scenarios:

  1. Searching for a product.
  2. Adding a product to the cart.
  3. Removing a product from the cart.
  4. Adjusting the quantity of a product in the cart.

Feature File (product_search.feature)

Feature: Product Search and Selection
  As a customer
  I want to search for products and manage my shopping cart
  So I can make purchases

  Scenario: Searching for a Product
    Given I am on the homepage
    When I enter "Laptop" in the search bar
    And I click the "Search" button
    Then I should see search results for "Laptop"

  Scenario: Adding a Product to the Cart
    Given I am viewing a product
    When I click the "Add to Cart" button
    Then I should see a confirmation message

  Scenario: Removing a Product from the Cart
    Given I have items in my cart
    When I click the "Remove" button next to a product
    Then I should see my cart without the removed product

  Scenario: Adjusting Quantity in the Cart
    Given I have items in my cart
    When I increase the quantity of a product
    And I click the "Update" button
    Then I should see the updated quantity in my cart

Step Definitions (ProductSearchSteps.java)

import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import static org.junit.Assert.*;

public class ProductSearchSteps {
    private WebDriver driver;

    public ProductSearchSteps() {
        // Initialize your WebDriver instance here (e.g., Selenium, Appium).
    }

    @Given("I am on the homepage")
    public void i_am_on_the_homepage() {
        driver.get("https://shopez.com/");
    }

    @When("I enter {string} in the search bar")
    public void i_enter_in_the_search_bar(String searchQuery) {
        WebElement searchField = driver.findElement(By.id("search-bar"));
        searchField.sendKeys(searchQuery);
    }

    @When("I click the {string} button")
    public void i_click_the_button(String buttonText) {
        WebElement button = driver.findElement(By.xpath("//button[contains(text(), '" + buttonText + "')]"));
        button.click();
    }

    @Then("I should see search results for {string}")
    public void i_should_see_search_results_for(String searchQuery) {
        WebElement results = driver.findElement(By.xpath("//div[contains(text(), 'Results for " + searchQuery + "')]"));
        assertNotNull(results);
    }

    @Given("I am viewing a product")
    public void i_am_viewing_a_product() {
        driver.get("https://shopez.com/product/12345"); // Replace with a valid product URL
    }

    @When("I click the {string} button")
    public void i_click_the_button(String buttonText) {
        WebElement button = driver.findElement(By.xpath("//button[contains(text(), '" + buttonText + "')]"));
        button.click();
    }

    @Then("I should see a confirmation message")
    public void i_should_see_a_confirmation_message() {
        WebElement confirmation = driver.findElement(By.xpath("//div[contains(text(), 'Product added to the cart.')]"));
        assertNotNull(confirmation);
    }

    @Given("I have items in my cart")
    public void i_have_items_in_my_cart() {
        driver.get("https://shopez.com/cart"); // Navigate to the cart page
        // Ensure the cart is not empty
        // Add products to the cart if necessary
    }

    @When("I click the {string} button next to a product")
    public void i_click_the_button_next_to_a_product(String buttonText) {
        WebElement button = driver.findElement(By.xpath("//button[contains(text(), '" + buttonText + "')]"));
        button.click();
    }

    @Then("I should see my cart without the removed product")
    public void i_should_see_my_cart_without_the_removed_product() {
        WebElement removedProduct = driver.findElement(By.xpath("//div[contains(text(), 'Removed Product Name')]"));
        assertNull(removedProduct);
    }

    @When("I increase the quantity of a product")
    public void i_increase_the_quantity_of_a_product() {
        WebElement quantityField = driver.findElement(By.id("quantity"));
        quantityField.sendKeys("2"); // Adjust the quantity as needed
    }

    @When("I click the {string} button")
    public void i_click_the_button(String buttonText) {
        WebElement button = driver.findElement(By.xpath("//button[contains(text(), '" + buttonText + "')]"));
        button.click();
    }

    @Then("I should see the updated quantity in my cart")
    public void i_should_see_the_updated_quantity_in_my_cart() {
        WebElement updatedQuantity = driver.findElement(By.xpath("//div[contains(text(), 'Product Quantity: 2')]")); // Adjust as needed
        assertNotNull(updatedQuantity);
    }
}

These Java-based step definitions interact with the product search and selection features. They cover scenarios for searching products, adding them to the cart, removing items, and adjusting quantities.

6. Feature 3 – Shopping Cart Management

Feature Description

Effective shopping cart management is essential for customer satisfaction. This feature includes the following scenarios:

  1. Emptying the shopping cart.
  2. Checking out with items in the cart.
  3. Applying coupon codes.

Feature File (cart_management.feature)

Feature: Shopping Cart Management
  As a customer
  I want to manage the items in my shopping cart
  So I can complete my purchase

  Scenario: Emptying the Shopping Cart
    Given I have items in my cart
    When I click the "Empty Cart" button
    Then I should see an empty cart message

  Scenario: Checking Out with Items in the Cart
    Given I have items in my cart
    When I click the "Checkout" button
    Then I should be redirected to the checkout page

  Scenario: Applying Coupon Codes
    Given I have items in my cart
    When I enter a valid coupon code
    And I click the "Apply" button
    Then I should see a discount in the cart total

Step Definitions (CartManagementSteps.java)

import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import static org.junit.Assert.*;

public class CartManagementSteps {
    private WebDriver driver;

    public CartManagementSteps() {
        // Initialize your WebDriver instance here (e.g., Selenium, Appium).
    }

    @When("I click the {string} button")
    public void i_click_the_button(String buttonText) {
        WebElement button = driver.findElement(By.xpath("//button[contains(text(), '" + buttonText + "')]"));
        button.click();
    }

    @Then("I should see an empty cart message")
    public void i_should_see_an_empty_cart_message() {
        WebElement emptyCartMessage = driver.findElement(By.xpath("//div[contains(text(), 'Your cart is empty.')]"));
        assertNotNull(emptyCartMessage);
    }

    @Then("I should be redirected to the checkout page")
    public void i_should_be_redirected_to_the_checkout_page() {
        String currentUrl = driver.getCurrentUrl();
        assertTrue(currentUrl.endsWith("/checkout"));
    }

    @When("I enter a valid coupon code")
    public void i_enter_a_valid_coupon_code() {
        WebElement couponField = driver.findElement(By.id("coupon-code"));
        couponField.sendKeys("SAVE10"); // Replace with a valid coupon code
    }

    @When("I click the {string} button")
    public void i_click_the_button(String buttonText) {
        WebElement button = driver.findElement(By.xpath("//button[contains(text(), '" + buttonText + "')]"));
        button.click();
    }

    @Then("I should see a discount in the cart total")
    public void i_should_see_a_discount_in_the_cart_total() {
        WebElement totalAmount = driver.findElement(By.id("cart-total"));
        assertEquals("$90.00", totalAmount.getText()); // Adjust the expected total as needed
    }
}

These Java-based step definitions provide interaction with the shopping cart management features, covering scenarios for emptying the cart, initiating the checkout process, and applying coupon codes.

7. Feature 4 – Checkout Process

Feature Description

A seamless checkout process is critical for customer satisfaction. This feature encompasses the following scenarios:

  1. Entering shipping and payment information.
  2. Confirming the order.
  3. Receiving an order confirmation.

Feature File (checkout_process.feature)

Feature: Checkout Process
  As a customer
  I want to complete my purchase
  So I can receive my order

  Scenario: Entering Shipping and Payment Information
    Given I am on the checkout page
    When I fill in shipping information
    And I enter payment information
    And I click the "Place Order" button
    Then I should see an order confirmation message

  Scenario: Confirming the Order
    Given I have completed the checkout process
    When I click the "View Order" button
    Then I should see the order details

  Scenario: Receiving an Order Confirmation
    Given I have placed an order
    Then I should receive an order confirmation email

Step Definitions (CheckoutProcessSteps.java)

import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import static org.junit.Assert.*;

public class CheckoutProcessSteps {
    private WebDriver driver;

    public CheckoutProcessSteps() {
        // Initialize your WebDriver instance here (e.g., Selenium, Appium).
    }

    @Given("I am on the checkout page")
    public void i_am_on_the_checkout_page() {
        driver.get("https://shopez.com/checkout");
    }

    @When("I fill in shipping information")
    public void i_fill_in_shipping_information() {
        WebElement firstNameField = driver.findElement(By.id("first-name"));
        WebElement lastNameField = driver.findElement(By.id("last-name"));
        WebElement addressField = driver.findElement(By.id("address"));

        firstNameField.sendKeys("John");
        lastNameField.sendKeys("Doe");
        addressField.sendKeys("123 Main St");
        // Fill in other shipping information fields
    }

    @When("I enter payment information")
    public void i_enter_payment_information() {
        WebElement cardNumberField = driver.findElement(By.id("card-number"));
        WebElement expirationField = driver.findElement(By.id("expiration-date"));
        WebElement cvvField = driver.findElement(By.id("cvv"));

        cardNumberField.sendKeys("1234 5678 9012 3456");
        expirationField.sendKeys("12/23");
        cvvField.sendKeys("123");
        // Fill in other payment information fields
    }

    @When("I click the {string} button")
    public void i_click_the_button(String buttonText) {
        WebElement button = driver.findElement(By.xpath("//button[contains(text(), '" + buttonText + "')]"));
        button.click();
    }

    @Then("I should see an order confirmation message")
    public void i_should_see_an_order_confirmation_message() {
        WebElement confirmationMessage = driver.findElement(By.xpath("//div[contains(text(), 'Your order has been placed.')]"));
        assertNotNull(confirmationMessage);
    }

    @When("I have completed the checkout process")
    public void i_have_completed_the_checkout_process() {
        // Simulate a completed checkout process
    }

    @When("I click the {string} button")
    public void i_click_the_button(String buttonText) {
        WebElement button = driver.findElement(By.xpath("//button[contains(text(), '" + buttonText + "')]"));
        button.click();
    }

    @Then("I should see the order details")
    public void i_should_see_the_order_details() {
        WebElement orderDetails = driver.findElement(By.xpath("//div[contains(text(), 'Order #12345')]"));
        assertNotNull(orderDetails);
    }

    @Given("I have placed an order")
    public void i_have_placed_an_order() {
        // Simulate an order placement
    }

    @Then("I should receive an order confirmation email")
    public void i_should_receive_an_order_confirmation_email() {
        // Check for the order confirmation email in your email system
    }
}

These Java-based step definitions cover the entire checkout process, including entering shipping and payment information, order confirmation, and email confirmation for placed orders.

Conclusion

In conclusion, automating the testing of e-commerce websites using Cucumber and Java provides an efficient means to ensure the quality of the online store. With well-defined features, feature files, and Java-based step definitions, you can uncover issues early in the development cycle, leading to an improved shopping experience for customers and increased website reliability.

Automated testing with Cucumber not only saves time but also enhances collaboration among development and quality assurance teams, making it an invaluable tool in the ever-evolving world of e-commerce.

Related Posts