top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Enhancing E-commerce Automation with Cucumber Expressions

Welcome to my blog! Today, we’re embarking on a journey to optimize your e-commerce test automation using Cucumber expressions within the framework of Behavior-Driven Development (BDD). This guide aims to shed light on the transformative power of Cucumber expressions, demonstrating how they alleviate code duplication and elevate the maintainability of your project.


Introduction to Behavior-Driven Development (BDD)

Behavior-Driven Development (BDD) is a collaborative approach to software development that bridges the gap between business stakeholders and technical teams. By focusing on the desired behavior of the application from the user's perspective, BDD encourages clear communication and shared understanding. This is achieved through the use of Gherkin language, which allows the creation of human-readable feature files describing the expected behavior of the software.

Understanding the Essence of Step Definitions

Step Definitions act as the glue between human-readable Gherkin scenarios and executable code. These definitions bridge the gap, translating the language of stakeholders into actionable automation steps.


Illuminating with an E-commerce Login Scenario

Lets Take an E-commerce Login Feature File

Feature: E-commerce user login

 

Feature: Login Feature

Scenario: User should able to Signin

Given The user is in the Sign in page

When User enters Valid "username" and "password"

Then User will able to navigate to homepage


@Given("The user is in the Sign in page")

public void the_user_is_in_the_sign_in_page() throws InterruptedException {

navigateToLoginPage();

  console.log('Navigating to the login page');

}

@When("User enters Valid {string} and {string}")

public void user_enters_valid_and(String username, String password) throws InterruptedException {

enterCredentials(username, password);

}

@Then("User will able to navigate to homepage")

public void user_will_able_to_navigate_to_homepage() {

  console.log('Navigating to the Home page');

}


Reflecting on My Journey Without Cucumber Expressions


Looking back, I realize that my approach lacked the clarity and flexibility that Cucumber expressions offer. Let me take you through an example of how I used to write scenarios without leveraging the power of Cucumber expressions. Back then, I often skipped them without knowing their full potential:


Feature: Login Feature

Scenario: User should able to Signin

Given The user is in the Sign in page

When User enters Valid username and password

Then User will able to navigate to homepage


Shortcomings of My Previous Approach


1.Hard to Understand: The steps weren't very clear, which made it tough for others to know what each scenario was trying to do. Ambiguity in the steps could lead to misunderstandings and misinterpretations, resulting in ineffective collaboration between team members.

2.Not Flexible Enough: The lack of flexibility in the previous approach hindered the ability to accommodate changes easily. Any modifications to the test scenarios or underlying application logic often required significant rework, impacting the efficiency of the testing process. Without parameterization and dynamic handling of inputs, adapting to evolving requirements became challenging.

3.Tough to Keep Up: Making changes to the tests was a time-consuming job. The rigid structure of the test scenarios meant that even minor alterations necessitated extensive modifications to the steps, leading to increased maintenance overhead. This time-consuming process impeded the agility of the testing efforts and delayed the delivery of software updates.

4. Limited Reusability: The lack of reusable components or modularization in the test scenarios restricted their reusability across multiple features or use cases. Duplication of steps across scenarios resulted in redundant test suites making maintenance and evolution of the test automation framework cumbersome.

5.Poor Scalability: The scalability of the test automation framework was compromised due to the monolithic nature of the test scenarios. As the complexity of the application grew or new features were introduced, scaling up the test automation efforts became daunting, resulting in decreased test coverage and effectiveness.


These issues made my testing process a lot slower and more confusing than it needed to be.

Feature: Product Management

Now, let's introduce a new feature for product management. Consider the following scenario:

Feature: Product Management


Scenario: Admin adds a new product

    Given The admin is logged in

    When Admin adds a new product with the following details

        | Name       | Description          | Price | Quantity |

        | New Product| A brand new product  | $50   | 100      |

    Then The new product should be added successfully.


@Given("The admin is logged in")

    public void the_admin_is_logged_in() throws InterruptedException {

        loginAsAdmin();

        System.out.println("Admin logged in");

    }


    @When("Admin adds a new product with the following details")

    public void admin_adds_new_product_with_details(Map<String, String> productDetails) {

        String name = productDetails.get("Name");

        String description = productDetails.get("Description");

        String price = productDetails.get("Price");

        String quantity = productDetails.get("Quantity");

        

        addNewProduct(name, description, price, quantity);

    }


    @Then("The new product should be added successfully")

    public void new_product_should_be_added_successfully() {

        System.out.println("New product added successfully");

    }


In this scenario, the admin is adding a new product to the e-commerce platform. The table provides details such as the product name, description, price, and quantity. By using `{string}`, `{float}`, and `{int}` placeholders, we can dynamically test various product details without modifying the step definitions

Parameter Types in Cucumber Expressions

To better understand how Cucumber expressions work, let's look at the different parameter types we can use:

Parameter Type

Description

{int}

Matches integers, for example, 71 or -19. Converts to a 32-bit signed integer if the platform supports it.

{float}

Matches floats, for example, 3.6, .8, or -9.2. Converts to a 32-bit float if the platform supports it

{string}

Matches single-quoted or double-quoted strings, for example, "banana split" or 'banana split' (but not banana split). Only the text between the quotes will be extracted. The quotes themselves are discarded. Empty pairs of quotes are valid and will be matched and passed to step code as empty strings


To know more about Cucumber Expressions. Please go through https://github.com/cucumber/cucumber-expressions#readme


Embracing the Enlightenment of Cucumber Expression

With Cucumber expressions, we overcome the limitations of traditional step definitions and unlock a new level of flexibility and readability in our test scenarios. By embracing Cucumber expressions, we empower ourselves to create more robust and maintainable automated tests, paving the way for efficient and reliable software delivery.

Conclusion

As we continue to refine our approach to test automation, the adoption of Cucumber expressions stands out as a game-changer. By reducing code duplication, enhancing flexibility, and improving readability, Cucumber expressions enable us to build more resilient and scalable test suites. Whether you’re working on a simple login feature or a complex product management system, embracing Cucumber expressions will elevate your testing practices and drive the success of your projects.


Happy testing!!!!!!!

19 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page