This project provides a modular and scalable Playwright framework example. It brings together various features, logic, and best practices to serve as a comprehensive learning resource that replicates a real framework used in production. This document follows a build-up path to help you understand each line of code and guide you to implement it yourself.
Before getting started, ensure you have the following installed:
- Git
- Node.js
Run the following command to set up Playwright:
npm init playwright@latestSelect the following options during setup:
- TypeScript
- Folder: tests
- Add GitHub Actions Workflow
- Install Playwright browsers
- Optionally, install Playwright OS dependencies
To handle tokens and secrets for local environments, install dotenv:
npm install dotenvCreate a .env file at the root level and add the following variables:
ENVIRONMENT:devTOKEN:magictoken
Integrate dotenv in your playwright.config.ts file:
import dotenv from "dotenv";
dotenv.config();To enforce best practices, add linters and plugins for TypeScript and Playwright. If you need more details about this: Setting up ESLint for Playwright Projects with TypeScript.
npm install @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-plugin-playwright --save-devCreate an eslint.config.js file and configure the linters.
Use the example of eslint.config.js here if you need help. link to eslint
Create a tsconfig.json file at the root of the project.
Use this project’s tsconfig.json as a guide. link to tsconfig.json
Update package.json with "type": "module".
Add the following script to package.json:
"static-checks": "npx eslint . && npx tsc --noEmit"Key points for playwright.config.ts:
- Set appropriate
timeouts. For more details read this article about timeouts - Focus on full parallelization. For more details read this article
- Configure
baseURLbased onENVIRONMENT. - Use
globalSetupfor environment and setup dependencies. - Use multiple reporters for CI and debugging.
Create a globalSetup.ts file in the /tests folder to be used for any configurations you need prior to running tests.
Use Playwright's Fixtures feature for flexibility. Merge multiple fixtures in fixtures/index.ts for easy access in tests.
Example:
test("Add new entry on home page", async ({ page, testData }) => {
await page.goto("/");
await page.getByTestId(secretKeyInputField).fill(testData.secretKey);
});Include workflows for GitHub Actions. Set environment variables in repository secrets and reference them in the workflow file.
env:
TOKEN: ${{ secrets.TOKEN }}
ENVIRONMENT: ${{ secrets.ENVIRONMENT }}Use the VS Code extension or run tests via terminal:
npx playwright test tests/specFileName.spec.tsor use the package.json scripts
"runAll": "npm run clean-reports && npm run static-check && npx playwright test",Set environment variables in GitHub Actions repo settings and reference them in the workflow file. You can also use scripts from package.json:
env:
ENVIRONMENT: ${{ secrets.ENVIRONMENT }}Playwright has its own built-in reporter and its pretty awesome, however I have found that Allure brings the most value for engineers to evaluate and debug, but also for management to have a clear view. I mix and use all of the below for practicability purposes
Html - the basic, good for debuggins json - good for advanced stuff , such as publish results in slack for example list - looks good in CI for quick debugging allure-playwright - to make your managers happy
reporter: [["html"], ["list"], ["json"], ["allure-playwright"]];Most comprehensive reporter with tons of configurations - Allure
reporter: ["allure-playwright"];To install allure reporter you must do
npm install allure-playwright allure-commandlineAdd reporter in playwright config file and if needed add extra configurations. I highly suggest handling actions such as API calls or evironment related with a custom error handler. By doing this you can combine with allure reporters categorize options, to always have a clear view on which tests fail, based on the type of failure (eg: environment issue, test data issue, etc)
Important dependency detail:
Allure reporter requires you to have Java installed on your machine to show you the results in browser. However if you generate results using --single-file param it will just give you a html file with everything you need, without the need to install Java. You can use the script below
npm run allure-report-singleContributing
To contribute, fork the repository, create a new branch, and submit a pull request to the main branch with a detailed description of your changes.