This project is a Behavior-Driven Development (BDD) framework that integrates Playwright with Cucumber.js. It allows you to write end-to-end tests in Gherkin syntax and execute them using Playwright's powerful browser automation capabilities.
The project is organized as follows:
playwright-bdd-framework/
├── src/
│ ├── test/
│ │ ├── features/ # Gherkin feature files
│ │ │ └── login.feature # Example feature file
│ │ ├── step-definitions/ # Step definitions for Gherkin steps
│ │ │ └── loginSteps.js # Step definitions for login feature
│ │ ├── utils/ # Utility functions
│ │ │ └── manageStepsDefinitions.js # Helper functions for step definitions
│ ├── support/
│ │ ├── hooks.js # Cucumber hooks (e.g., Before, After)
│ │ ├── world.js # CustomWorld for shared test context
│ │ └── report_chart.js # HTML report generation script
├── reports/ # Test reports (ignored by Git)
│ ├── cucumber_report.json # JSON report generated by Cucumber.js
│ ├── html-report/ # HTML report generated by multiple-cucumber-html-reporter
├── config.json # Custom configuration for environments and settings
├── package.json # Project dependencies and scripts
├── .gitignore # Ignored files and folders
- Node.js (v16 or later)
- npm (comes with Node.js)
- Playwright (installed via
npm)
-
Clone the repository:
git clone <repository-url> cd playwright-bdd-framework
-
Install dependencies:
npm install
-
Install Playwright browsers:
npx playwright install
-
Run all tests:
npm test -
Run tests with specific tags:
TAGS="@smoke" npm test
-
Debug a specific feature file:
npm run debug
-
Generate an HTML report: After running tests, the HTML report will be automatically generated in the
reports/html-reportfolder. Openindex.htmlin your browser to view the report.
- Use ESLint for consistent code formatting.
- Follow JavaScript best practices for readability and maintainability.
- Use
async/awaitfor all asynchronous operations. - Wrap critical steps in
try-catchblocks to handle errors gracefully. - Use
this.configto access shared configuration values.
Example:
Given('I navigate to url {word}', async function (url) {
const targetUrl = await go_to_url(url, this.config);
await this.page.goto(targetUrl, { waitUntil: 'load' });
});- Use
BeforeandAfterhooks to set up and tear down the browser context. - Take screenshots for failed scenarios in the
Afterhook.
Example:
After(async function (scenario) {
if (scenario.result?.status === 'FAILED') {
const screenshotPath = `reports/screenshots/${scenario.pickle.name.replace(/[^a-zA-Z0-9]/g, '_')}.png`;
await this.page.screenshot({ path: screenshotPath });
console.log(`Screenshot saved at: ${screenshotPath}`);
}
await this.closeBrowser();
});- Store environment-specific settings in config.json or
playwright.config.ts. - Use
this.configin step definitions to dynamically fetch configuration values.
-
Gherkin Syntax:
- Write tests in plain English using
.featurefiles. - Example:
Feature: Login functionality Scenario: Verify login with valid credentials Given I navigate to url "login" Then I verify title this page is "Dashboard"
- Write tests in plain English using
-
Playwright Integration:
- Automate browsers like Chromium, Firefox, and WebKit.
- Use Playwright's powerful API for browser interactions.
-
HTML Reporting:
- Automatically generate detailed HTML reports after test execution.
-
Environment-Specific Configurations:
- Manage multiple environments (e.g., QA, Staging, Production) using config.json.
-
No Scenarios Found:
- Ensure the
tagsin config.json or theTAGSenvironment variable match the tags in your.featurefiles.
- Ensure the
-
HTML Report Not Generated:
- Verify that the
cucumber_report.jsonfile is generated in the reports folder. - Ensure the
posttestscript in package.json is correctly configured.
- Verify that the
-
Browser Not Launching:
- Ensure Playwright browsers are installed:
npx playwright install
- Ensure Playwright browsers are installed: