Playwright is a free open-source framework for automation tests. It is new to the market but is building up popularity fast. 1st release happened on 2020. Microsoft maintains it. It receives regular updates and improvements. If we look at the number of downloads for similar frameworks which have been on the market for a while, you can see Playwright has burst onto the scene.
It is simple to set up Playwright in your project or create a new test project.
You can scaffold your project using the init command.
To add dependency and install browsers.
The above command installs the Playwright version of Chromium, Firefox, and Webkit browser.
Playwright also ships with a code generator that can generate the tests for you. Run codegen and interact with the browser. The playwright will generate the code for the user interactions. codegen will attempt to build text-based selections that are durable.
You can copy generated code by clicking the button near the record button.
Run a single test file:
Playwright test just runs a test using Chromium browser, in a headless manner. Let's tell it to use the headed browser:
Run all the tests against a specific project:
There are more commands to run the test as per requirement, you can see them here.
In many projects, we maintain different environments like dev, QA and prod, etc. To make environment variables easier to manage, consider something like .env files. Here is an example that uses the dotenv package to read environment variables directly in the configuration file.
Global Setting for pick env file to run a test against:
In the playwright.config.ts file add the below code.
Pass environment in test command:
In package.json add the below commands to the script section. Install the cross-env package which Runs scripts that set and use environment variables across platforms.
We have environment files like the below .env.prod file. Same REDIRECT_URI key added in .env.development and .env.qafiles but with different values.
Run test against the environment :
Now run a test against the different environments by running commands as per env.
Like Selenium Playwright also offers parametrized tests. You can either parametrize tests on a test level or on a project level.
You can also do it with the test.describe() or with multiple tests as long the test name is unique.
Playwright Test supports running multiple test projects at the same time. In the following example, we'll run two projects with different options.
We declare the option person and set the value in the config. The first project runs with the value Alpha and the second with the value Beta.
Now in the test file, we can use it.
Now, we can run tests in multiple configurations by using projects. We can also use the option in a fixture.
When testing complex, integrated applications, test teams frequently turn to "modular testing" as a way to break down application functionality into small pieces. The modular pattern provides an easier-to-follow road map and then rearranges the chunks of functionality into software testing scenarios that represent different customer workflows. Creating a modular regression suite takes time, but in the end, the business has a complete list of functional areas, along with integration points.
A detailed Blog on the Modular test is here Modular Testing.
It is very easy to run the Playwright automation tests using the GitHub action. Like all the other GitHub actions, you need to create a YAML file for the execution.
Here is a sample YAML file that can help you to get started. Please go through each step carefully.
The GitHub action or the Azure Pipeline can also publish the test result directly to your test management tool.
Let's see how it can be done for the two most used test management tools in the industry.
Xray is a test management tool widely used in Jira. It integrates very well in Jira. Test cases, suits, plans, and executions are also created as Jira issues by Xray. Let's see how our CI/CD pipeline can be integrated with the Xray.