โ๏ธPlaywright + Cucumber
โPlaywright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API. Playwright is built to enable cross-browser web automation that is ever-green, capable, reliable and fast.
Cucumber is a tool that supports Behaviour-Driven Development(BDD), If youโre new to Behaviour-Driven Development read BDD introduction first.โ
How to Setup:
Run the command in the terminal:
Install playwright:
This command will install the playwright library and browser dependencies required for testing.
This command will install the modules required for validations with built-in automatic waits.
Install cucumber:
This command will install the Cucumber, a tool for running automated tests written in plain language.
Install typescript:
This command will install the typescript dependencies to support with node.
After installing, go to package.json and your package.json must look similar to the below picture:
Features:
Features files are the test case files written in Gherkin language, which explains the motive of the tests in plain English, making it easier for non-technical people to understand the context of the test.
Letโs create a sample feature using Gherkin keywords and derive the logic using Playwright.
Create a Feature folder
A feature file should always end with a .feature extension.
Add feature and scenario details as per the below image, based on the demo siteโs functionality.
World
The word โWorldโ describes the steps where we can declare and invoke the objects/variables that can be used globally. The Java/C# version of Cucumber is said to be Hooks.
Hooks are primarily used to specify pre and post-conditions globally across the features. Here, we will create two global variables, one for the browser and another for the page.
The โBeforeโ and โAfterโ functions launch the app before each scenario and then kill it. The goal is to make the scenarios independent. The Before function captures the browser and page variables from the initializer and exports.
Create a file with the name โworld.tsโ under the steps folder.
Initialize the browser and page variables.
Set the default timeout at 60 seconds (default waits till promise gets resolved).
Create a โBeforeโ function to launch the chromium browser and assign the session id to the browser object.
Create a browser context and assign it to the page variable.
Navigating the URL is specified here as part of the precondition.
Add this code to world.ts
Steps:
Steps from the feature file have to be implemented as definitions to specify your business logic.
Gherkinโs steps from the features are initially considered undefined by Cucumber, and when running the script command defined in package.json, Cucumber generates the undefined snippets which could be used in the steps file instead of writing them, which saves time for us.
Firstly, Cucumber needs to know where to look for the steps to notify us with the snippets of unimplemented ones (In case already implemented, it will directly run the matching step).
The below procedure helps you to run the script and get the status from Cucumber.
Create a file named cucumber.js to define the Cucumber options where the path to the feature and step are defined.
Declare the options and export them as a module with the name โtest_runner.โ
Add the code below to cucumber.js
Now navigate to package.json and add this to script > test:
Now run the โnpm testโ
Notice that you will see this result:
It shows undefined because thereโs no steps yet for the signup.feature.
Add this to the signup.step.ts under step folder:
Steps execution:
We are now done installing the packages and creating some scripts.
To run all tests:
To run specific test:
Reporting:
Though we have results displayed in the terminal, a report implementation is required to share the results as an html file.
Cucumber provides an html reporter plugin to generate the reports based on Gherkinโs steps, which can be customized using metadata.
Install the following:
To confirm your installation, go to package.json.
Add a file โhtmlReportGenerator.jsโ under the root folder to define the report options.
To store the json data, create a folder name Reports. Under reports folder create cucumber_report.json
Navigate to cucumber.js and add the below option to format json dataโโ format json:./ Reports/cucumber_report.json.โ
To run the report:
Sample image for report:
Attach screenshot for failure:
Navigate to steps> world.ts. Add this to the After function
Last updated