CHAPTERS
OVERVIEW
Playwright is an open-source automation testing framework by Microsoft for web testing. Playwright features help you test across all modern browsers in a reliable, effective and fast manner. Be it codeless or code-based, Playwright features help you test anything from unit testing to automation end-to-end testing It is becoming increasingly popular amongst many developers. For instance a survey conducted by StateofJS, showcases that Playwright has gained an increase in awareness from 19% to 34% amongst many developers.
In our previous Playwright tutorial series, we discussed the basics of Playwright, outlining its capabilities and how to install Playwright using the VS Code plugin. Letâs discuss Playwright features in this tutorial.
As per StateofJS survey, Playwright was able to consistently retain 100% of its users from 2020 to 2021. No wonder, Playwright has been the talk of the town in the testing community as one of the best options to consider automation testing. What makes Playwright special? Let's have a look at some of the most notable Playwright features for test automation.
You can also go through the following video from the LambdaTest YouTube channel to learn more about futuristic Playwright features.
Letâs look at the above Playwright features one by one!
Playwright comes with the ability to generate tests out of the box and is a great way to quickly get started with testing. It will open two windows, a browser window where you interact with the website to test. The Playwright Inspector window records your tests, copy the tests, clear your tests as well as change the language of your tests.This enables us to easily create tests by recording our actions. Also known as Playwright Test Script Recorder.
In the previous tutorial, we read about a test script showcasing the browser followed by context and page, which eventually led us to a particular site. Since we considered the LambdaTest eCommerce Playground (a demo account), it will get redirected to its home page.
We also learned about the exact use of new context and new pages. The next step was login.
This time, we will use a Playwright feature instead of writing code. To use this feature, we need to follow a few steps:
ânpx playwright codegen
This command is used for recording tests or generating test scripts.
Eventually, the Playwright Inspector records whatever actions that occur on the browser. (By default, it will be in record mode)
For example â copy the URL of a particular site and paste it into the browser (which is in incognito mode). Now when you look at the Inspector page, you get the exact URL you searched for in the browser.
One of the major highlights around Playwright is its Language Compatibility. Playwright tests by default are generated in JavaScript. However, you can change the Playwright test to other programming languages such as Java, .NET or C#, Python.
A small example of how the Playwright Inspector works is when you make a few changes:
Test Scenario:
Playwright inspector is a great tool that helps in authoring and debugging Playwright scripts. This is a default recommended tool for script troubleshooting. It opens up a browser window highlighting the selectors as you step through each line of the test. You can also use the explore button to find other available selectors which you can then copy into your test file and rerun your tests to see if it passes.
test("Login test demo", async () => {const browser = await chromium.connect("wss://cdp.lambdatest.com/playwright?capabilities=
${encodeURIComponent(JSON.stringify(capabilities))}");
const context = await browser.newContext();
const page = await context.newPage();
await page.goto("https://ecommerce-playground.lambdatest.io/")
await page.hover("//a[@data-toggle='dropdown']//span[contains(.,'My account')]")
// await page.click("text=Login")
await page.click("'Login'")
await page.fill("input[name='email']", "koushik350@gmail.com")
await page.fill("input[name='password']", "Pass123$")
await page.click("input[value='Login']");
await page.close();
await context.close();
await browser.close();
Playwright test comes from the concept of test fixtures. Fixtures establish an environment for each test. After we complete the steps under Playwright test script recording, we need to;-
Test Scenario:
Also, this configuration test applies to all the tests we perform.
In-built reporters is another Playwright feature for different needs to provide custom reporters. There are a wide variety of reporters. You can also use multiple reporters at the same time. The reporter command-line option is the simplest way to try out built-in reporters.
Examples of a few reporters are - DOT, LINE, GitHub, JSON, JUnit, and HTML. We will focus more on DOT, HTML, and JSON.
Test Scenario:
{testmatch: [ââtests/recorded.test.tsââ], use: {headless: false}, reporter: [ [ââdotââ], [ââjsonââ, {output file: ââjsonReports/jsonReport.jsonââ}], [ââhtmlââ, {open: ââneverââ}]]
You will be able to see the multiple steps in the test. Example âânavigating to a particular site, hover action, login, accessionââ.
Also, if you are familiar with Selenium Cucumber, you might know about hooks (before and after hooks).
The function of hooks - âbefore hooksâ runs before the test begins, and âafter hooksâ runs after the test ends.
Screenshots/Videos can benefit a testing server when a test fails. It can show what exists on the screen when a test fails. This Playwright feature is beneficial when we want to know where we could have gone wrong at one glance.
Suppose you want to view a screenshot or video of a particular test. You will have to add two additions to your script - screenshots and video.
After your test ends, you can view screenshots and videos.
Screenshot:
Video:
You will get three options when you add a âscreenshotâ in the script.
Next, we need to add âvideoâ in the script, which will also show a few options.
We need to understand that taking screenshots or videos of multiple tests may increase reporters' size. Hence we can choose a particular part of the test rather than the entire process. To enable this feature, select âretain-on-failureâ in the test script.
Under this feature, we capture a screenshot only when thereâs a failure in the test.
If you want to retry any particular test, you can add âretries'' to the script. Playwright retries are reliable as we can acquire screenshots and videos regardless of how many times you repeat a test. We can use it to run a test multiple times.
The image below shows the code we enter to enable the Playwright Retry option. To view a failed test, we have the option to enter the maximum number of retries we would want.
After the test ends, we can view the failed script we chose to retry.
This Playwright feature is beneficial when a test fails, as you can retry and figure out where you went wrong. Hence, with multiple retries, we can avoid the failure of a test.
The auto-wait mechanism is a process that might seem lengthy but easy if you are familiar with JavaScript. There are a few conditions under which Playwright's actions will depend. The conditions are as follows:
Every action ensures they pass through these conditions or elements. It waits for all relevant checks to pass before performing the requested action. The combination of conditions and actions eliminates the need for artificial timeouts. Hence the auto-wait mechanism helps reduce flaky tests.
If you are a developer or a tester looking to validate your Playwright test automation skills, you can go for Playwright 101 certification from LambdaTest.
LambdaTest is a test execution and test orchestration platform on cloud with an automation testing grid of 3000+ browsers and operating systems to help you scale your Playwright test execution effortlessly. LambdaTest lets you:
LambdaTestâs automation testing cloud also supports Selenium, Cypress, Puppeteer, and even app automation frameworks such as Appium, Espresso, XCUITest etc.
Playwright makes automation quick, reliable, and simple. In this tutorial, we discussed the prominent features of Playwright such as test generator, generating screenshots and videos on test failures, retries, fixtures and auto-wait mechanism. In the upcoming tutorial, we will deep dive into understanding handling of inputs and buttons.
Hereâs a glimpse of how you can perform Playwright browser testing on the LambdaTest cloud grid.
Subscribe to the LambdaTest YouTube Channel and stay updated with the latest Playwright topics.
Playwright has full API coverage for all modern browsers, including Google Chrome and Microsoft Edge (with Chromium), Apple Safari (with WebKit), and Mozilla Firefox.
Script execution in Playwright is faster compared to Selenium. Playwright uses a WebSocket connection rather than the WebDriver API and HTTP. This stays open for the test, so everything is sent on one connection. This is one reason why Playwright's execution speeds are faster.
Playwright works with some of the most popular programming languages, including JavaScript, Python, Java, C#, and NodeJS.
Did you find this page helpful?