Playwright - The Game Changer Test Automation Tool

Are you looking for a new Software Test Automation Tool at your company? If so, then read on...

Playwright (PW) has taken the Test Automation world by storm. I started hearing about it's success stories couple of years back. Realizing it's potential i gave it a try by creating a Synthetic monitoring system for one of the products at my workplace. I was truly amazed at the results and my enthusiasm for it only increased with time as i helped roll it out to multiple teams at work. In this blog i will share with you the main reasons i like and truly enjoy working with Playwright. 

1. Reliability : Out of all the features of a test automation tool - i consider reliability to be highest.  A reliable tool reduces debugging time, improves quality of work life and opens up time to work on other value adding activities. After analyzing hundreds of test failures over 50K test runs in the past year - i am happy to report that the number of test failures directly attributable to PW was actually Zero! All the failure causes were external to PW e.g. recent changes in product code, environment or configuration, 3rd party dependencies, network outages etc. Although, i haven't compared PW head to head with other tools, but anecdotally from my experience PW is by far the most reliable test automation tool. 

2. Adoption rate :  Playwright has had phenomenal growth since its founding 4 years ago. Here you can see it's meteoric growth trajectory compared to Selenium and Cypress with over 60K+ GitHub stars. It also has a very active user base in discord with over 10K users. This means Playwright is rapidly getting adopted across businesses of all sizes across in various software domains and it's also getting easier to hire people skilled in Playwright. 

3. Longetivity : You don't want to spend time and effort rolling out a tool only to hear the company supporting it went bankrupt or if it's OpenSourced then the main contributors stopped supporting it. Playwright is fully open-sourced, with Microsoft as it's main backer. Microsoft has a history of sponsoring other open source projects like VScode, Typescript, .NET and considering the postive impact of Playwright in software indeustry it's very likely Microsoft will support  PW for a long time. Some of the key contributors to PW are from Microsoft, but the project has over 500+ contributors from opensource community as well.

4. Cross Browser Support : PW supports all the major browsers - Chrome, FireFox , Edge and Safari. It used to be that UI features would work on some but not all major browsers. With the implementation of W3C Standards by all major browsers and cross-browser support provided by major UI frameworks such as Reactjs - the number of browser specific issues has gone down substantially. Even so these issues do exist and it's reassuring to know that PW supports all major browsers. 
One caveat here is that each Playwright version has a recommended browser version. This provides reliability, but also has a limitation that you cannot test with older browser version. Most companies and users nowadays are on latest version of browser due to security and reasons, so i consider this a minor limitation. However, if your use case requires testing with older browser version then you may need to use other tools or use a workaround.

5. Language and IDE Support : PW supports Nodejs, Python, Java and .NET. Nowadays, most of frontend web development is done using Typescript or Javascript via frameworks like React, Vue or Svelte, so naturally PW's support is best in Typescript with Nodejs. PW Nodejs comes fully integrated (a.ka. batteries included) with VSCode and provides excellent support for running tests, debugging via Trace storage, html report generation, console log and network call capture etc. In short, it makes debugging fun and efficient.
However, if you are fully invested in Java, python or .NET you can still use PW, just know that these languages may not have the full feature set provided as part of Nodejs VSCode integration.

6. Getting help : So many times you hear about great products with incomplete accompanying documentation that then slows down the adoption rate. Playwright team seems to have given thought to this and as they have made sure Playwright Documentation is top notch and uptodate with the latest features and bug fixes. With every release there is usually a youtube video showcasing how to effectively leverage the new features. The Playwright Youtube channel is also filled with many videos showcasing best practices. There is also the discord channel where in you can post questions and quickly get answers. I can vouch for it as multiple times i posted questions and got unblocked by the answers i got.

7. New test creation : When creating new tests, PW provides multiple ways to identify web elements. 
First off, it has a built-in test recorder (Refer "Record new" in below screenshot) that actually works quite well compared with other tools. In most cases it's able to identify reliable locators. The preferred ones are the 7 locators that start with getBy* e.g. getByRole, getByTestId, getByText, getByAltText , getByTitle and getByPlaceholder.



Second, you can individually locate the element using "Pick locator". If these 2 options don't work for you then you can use the traditional approach via Chrome Dev Tools > Elements tab to identify XPath or CSS.

 


With PW you can combine API and UI tests in same test suite without needing to maintain separate repos for API and UI Tests. Also, in a UI test if needed you can use API's to setup and teardown test data. e.g. refer below code snippet to make an API call using PW's request object. You can get more details here.
const response = (await request.post('/api/path', {
data: {"query": query}
}))


8. Reduced test run time :  PW provides multiple ways to reduce test run time.

First, it's event driven meaning as soon as an element is detected it moves to the next step vs. waiting for a fixed amount of time. As a test creator you also don’t need to add any custom logic to add wait time. E.g. refer below line of code
await expect(page.getByTestId('expand-toggle')).toBeVisible({timeout: 60000})
Here at most PW will wait upto 60 sec for element to be present. In most cases it will move to next step as soon as an element is detected in well under 60 sec. The current default wait time for expect assertions is 5 sec. If this doesn't meet your needs you can modify it globally for all tests in playwright's config file called playwright.config.ts file with below code
expect: { timeout: 30000 }

And you can override it at element level only when needed, leading to faster and reliable execution and clean code.  Here you can find details on the various types of timeouts PW supports for reliable execution.

Second, PW supports test parallelization out of the box. It’s a one line change in playwright.config.ts file as shown below.

workers: 4

Here PW will run 4 tests in parallel using 4 worker processes on the host or node on which it is running. The number workers should be less than or at most the maximum number of CPU's on the machine. For Cloud Env, to set worker count properly you need to check the number of vCPU's on the Virtual Machine such as AWS EC2.

If you have a larger test suite or you want to bring down the run time as low as possible then you can try sharding tests across multiple machines. One of the key things to keep in mind when running tests in parallel is that the tests should not share state between each other. PW enables this to a good extent by utilizing completely independent browser instances called browser contexts that spin up in under a second.

Conclusion : As we covered in this blog Playwright is a highly reliable, production ready, feature-rich test automation tool with a fast growing user base and fanbase. During my career i have worked with a variety of test automation tools, but never felt so much excitement as with Playwright. If you are looking to take your test automation to the next level I highly recommend evaluating Playwright for your needs.

Comments

Popular posts from this blog

Software Alerting maturity levels

New Habits for 2023