Skip to main content

How to pass data dynamically to your test scenarios or step definition using Tables in cucumber BDD framework with playwright 

By October 18, 2023November 21st, 2023Blogs, QA

Table Of Contents:

 

    1. Introduction

 

    1. Benefits of test parameterization

 

    1. Examples of parameterization

 

    1. How to fetch data from Examples of Scenario Outline

 

    1. How to fetch data from the Cucumber feature file Data Tables.

Introduction:

Test Parameterization is a type of data-driven testing that allows you to execute the same test, multiple times using different parameters.

In the Playwright Cucumber BDD framework, parameterization refers to the ability to pass data or variables dynamically to your test scenarios or steps. This allows you to reuse scenarios with different inputs or test different variations of the same scenario.

Benefits of data-driver-testing and test Parameterization:

 

    • Tests are simple to comprehend, maintain and administer.
    • Execute the same test many times with various inputs to reduce the number of test cases required.
    • Execution time is cut in half.
    • Time-saving by avoiding the creation of repetitive test cases.
    • Tests and outcomes are better organised.
    • Reduces the amount of testing that is done twice.

Examples of parameterisation:

There are few examples where we are parameterising our data to reuse the scenarios with different inputs.

Example 1.

Simple parameterization:

In the below example we are parameterising the URL of the application which is to be tested

Feature file: Here we are parameterising the URL of the application in the feature file.

 

Step Definition file: Here we are passing that parameter as ‘url’ in the function. Need to keep string as it is in the curly brackets but have to change parameters in the function from string url (whatever you wanted to give the name of string). Here we kept that as url.

 

Page file: Here we are getting that parameter from step definition, passing it into the page file method and passing that in the statements of the method.

 

Example 2. In the below screenshot we can pass email Id and password values from feature file or from step definition file to fill the text input fields.

 

We can also pass value in the locators also using the parametrisation techniques.

Like in the below example we passed country value from feature to step file, from step file to page file. So that we can dynamically select whichever country we want.

 

Example 3. 

Scenario Outline in Playwright using Cucumber BDD Framework:

Scenario Outlines in Cucumber are used to run the same scenario multiple times with different sets of data. To use Scenario Outlines with Playwright in a Cucumber test, you can follow these steps:

 

    1. Define your feature file: Create a. feature file where you define your scenario outline using the Scenario Outline keyword. Map the placeholders in your step definitions: In your step definition files, you need to map the placeholders (e.g., <value>, <result>) to variables that will receive the values from the Examples table.

 

Implement step definitions: Create a step file where you implement the step definitions for your feature file. In these step definitions, you can capture the values and perform the desired actions. Use the parameter values in your step definitions: Inside the step definitions, you can use the received parameter values (value, result) to perform the desired actions or assertions.

 

In the above example we are having two example outlines.

1st set will execute with 10 as value and verify result as Success

2nd set will execute with 20 as value and verify result as Failure

When you run the tests, Cucumber will generate separate test runs for each set of data defined in the Examples section of the Scenario Outline. The step definitions will be executed with the corresponding values, allowing you to perform the necessary actions or validations using Playwright.

Example 4. 

Parameterising the Data from Tables:

To retrieve data from tables in playwright using cucumber BDD framework

Step 1. Define data tables in the feature file

In Your feature file, define a Data table using the pipe-separated values or any other format you prefer.

For Example:

 

 

Step 2. Create step definition in your step definition file (usually a JavaScript file). In these step definitions, you can capture the values and perform the desired actions by implementing the code. Use the parameter values in your step definitions: Inside the step definitions, you can use the received parameter values to perform the desired actions or assertions.

For example:

 

Step 3: Run your Cucumber tests Ensure you have the necessary dependencies installed, such as Cucumber, Playwright, and any other required libraries. Then, run your Cucumber tests using the command appropriate for your setup.

During the execution, the Data Table values will be passed as arguments to the corresponding step definition function. You can access and utilize the data within the step definition as needed.

Remember to customize the step definition code to fit your specific application and actions required.

Methods to retrieve data from tables of feature scenarios:

We used raw (), rows () and hashes () to retrieve data from table in our project.

Let’s see how to use raw (), rows () and hashes ()

 

    1. raw () function: raw () and rows () function is not a built-in function for retrieving data from tables. raw () functions do not exclude the header row from the returned data.

Here’s an example of how to use the rows () function in a Cucumber step definition:

Example of raw ()

 

 

    1. rows () function: rows () function is not a built-in function for retrieving data from tables. The rows () function is used to retrieve the data from a data table as an array of arrays, similar to the raw () function. However, unlike raw (), rows () exclude the header row from the returned data.

Example of rows ()

 

 

    1. hashes () function: The hashes () function is used to retrieve the data from a data table in a Gherkin scenario. It returns data as an array of objects, where each object represents a row in the data table.

The keys of the object are derived from the header row of the data table, and the values are taken from the corresponding cells. similar to the hashes () function.

Here’s an example of how to use the hashes () function in a Cucumber step definition:

Example of hashes ()

 

Conclusion: 

These functions provide flexibility in accessing and manipulating data from data tables in Cucumber scenarios using Playwright, allowing you to handle various table structures and perform operations based on the test data provided in the scenarios.
Note: The above steps assume you have already set up your Cucumber framework with Playwright and have the necessary configurations in place.

We at Varseno, provide Software Development Services for all business websites to improve their reliability, scalability and stability. Reach out to us for any web app services.

Sales Inquiry
close slider