A few weeks ago I was looking for an end-to-end testing framework. An alternative to Selenium, and all the other end-to-end frameworks. I came across a project called Playwright. Playwright is a new end-to-end framewrok created and maintained by Microsoft, it allows you to test web applications on different browsers. Some the major feature it provides are as follows.
- Playwright has full API coverage for all modern browsers, including Google Chrome and Microsoft Edge (with Chromium), Apple Safari (with WebKit) and Mozilla Firefox.
- Supports multiple languages like Node.js, Python, c# and Java.
- First-party Docker image and GitHub Actions to deploy tests to your preferred CI/CD provider.
- Use device emulation to test your responsive web apps in mobile web browsers.
- Provides APIs to monitor and modify network traffic, both HTTP and HTTPS. Any requests that page does, including XHRs and fetch requests, can be tracked, modified and handled.
While those feature are all great and useful, they don’t measure up to what I consider to be the best feature of Playwright. That is being able to create and execute test as easily as unit tests. You can also leverage tools like qa wolf and headless-recorder, these tools record any action you take on the browser, those actions are then converted into Playwright scripts.
For today’s post, I am going to build a small playwright project, I will use this project to demonstrate how to create some basic tests using playwright. To follow along you will need to have Node.js installed, version 10.17 or greater.
Open your favorite cli tool, windows terminal in my case. Run the following npm command to install playwright
With playwright now installed, create a new file on the same directory you opened your terminal. Call the file google.ts, copy and paste the following code.
The first line of code imports the playwright library, then an asynchronous context is created, this is where playwright is executed. The second line of code launches an instace of chrome, from that instance a new page is created, a go to url is provided so that it can be visited, once the page is loaded, a screenshot is saved to the directory playwright is running under. Finally, the browser instance is disposed.
To execute the code, run the following command on the cli.
This is the most basic example on how to use playwright. Time to take things to the next level. Clean the working directory, delete all files. Create a new node.js project by running the following command.
Provided the required information such as project name, author, description and so on. A package.json fille will be created with the information you provided.
Time to establish the folder structure. Whenever I work with a Node.js library, I like to follow the project structure outlined in this stack overflow post.
Fow now I will only add the test folder using the following command.
It is time to install a few additional node packages. Since I wiped the directory clean, I will need to reinstall playwright, I will also need a testing library, Playwright supports Mocha, Jest and Ava. For this repo I will use Mocha. I will also need an assertion library, you can use whichever library you want on your project, chaijs is my preferred framework. I work a lot with postman for API testing, postman uses chaijs to do assertions so I’m most familiar with chaijs and I know the syntax by heart. Other packages I will need are ts-node, typescript and mocha-junit-reporter.
Run the following command to install the required packages
Your package.json should look somewhat like this.
I do want to make a slight modification. I will repalce the scripts field with the following script commands to let npm know how to handle the command “npm test” and “npm lint”.
Your package.json should now look like this.
Perfect, our project is ready, I can start writing some test. I’m going to start small by adding two test, one that goes to google.com and confirms the page title is ‘Google’ and another that assert that when google.com is loaded succesfully, a 200 OK Http status should be the response status code.
Add a new file under the test folder, call it google.ts and open it up on your favorite editor, I recommend vscode. Copy and paste the following code.
The code above imports the required node packages, then playwright’s browser and page objects are configured to run before and after each test, then finally at the end, you have the two test. Like I said before one test will assert the title of the page and the other test will aserrt the response code. To execute these tests run the following command.
The command should output the following result.
Playwright run headless by default, if you want to see it execute the tests on the browser, then set the headless property to false on the launch() method.
For a complete list of available launch options, see the playwright api docs.
Awesome. A foundation for building end-to-end test with playwright has been established. More test can now be added. If you are building tests that deal with data input, then I suggest installing faker.js, it is another node libary used to create fake data such as people, address, number, text and so on. Learn more about playwright by reading the docs.