Insights

10 Playwright Best Practices

You want to make sure you're following best practices. Here are 10 to get you started.

Playwright is a new tool for end-to-end testing that makes it easier to automate tests across different browsers. In this article, we’ll share 10 best practices for using Playwright to help you get the most out of this tool.

1. Use the latest Node.js version

Node.js is constantly evolving, with new features and bug fixes being released on a regular basis. As a result, older versions of Node.js can quickly become outdated and no longer supported. This can lead to problems when trying to run playwright scripts on older versions, as they may not work correctly or at all.

By using the latest Node.js version, you can be sure that your playwright scripts will always be compatible with the latest Node.js release. This will save you time and frustration in the long run, as you won’t need to worry about whether your scripts will still work when a new Node.js version is released.

2. Install Playwright globally

When you install Playwright globally, you have access to the play command from any directory on your computer. This means you can create new plays in any location, and you don’t have to worry about setting up a Playwright project every time you want to write a new play.

Additionally, installing Playwright globally gives you access to the playwright command, which provides a number of helpful commands for working with Playwright. For example, the playwright update command will update your installed version of Playwright to the latest version.

Finally, when you install Playwright globally, all of the necessary files and dependencies are automatically installed in your home directory. This makes it easy to keep your Playwright installation clean and tidy, and it also ensures that you won’t accidentally delete important files when uninstalling Playwright.

3. Run your tests in a headless browser by default

When you run your tests in a headless browser, the test execution is faster since there’s no need to launch a GUI. This also makes it easier to run your tests in parallel, which further speeds up execution time. In addition, running your tests in a headless browser eliminates the possibility of flakiness due to UI changes, as well as the need for manual screenshots or video recordings.

4. Don’t use await page.goto() to navigate between pages

When you use await page.goto(), the playwright library will wait for the new page to load before continuing. This is fine if you’re just loading a single page, but if you’re trying to load multiple pages in parallel, it can cause a bottleneck.

Instead, you should use page.evaluate() to navigate between pages. This way, playwright can continue loading other pages while the first page is still loading.

5. Always use await page.$eval() and await frame.$eval() instead of await page.$() and await frame.$()

When you use await page.$() or await frame.$, playwright returns a Promise for an ElementHandle, which is then resolved by the browser. This resolution process can take up to several seconds, depending on the size and complexity of the page.

However, when you use await page.$eval() or await frame.$eval(), playwright immediately resolves the Promise with the return value of the provided function. This means that you can avoid the unnecessary resolution process, and get your results faster.

6. Avoid using XPath whenever possible

XPath is a language for addressing parts of an XML document, and it’s often used in playwriting to identify the location of elements within a script. However, XPath can be very brittle, meaning that if the structure of the XML document changes, the XPath expressions will likely need to be updated as well.

This can be a major problem when a playwright is working with a large team of collaborators, because it’s often difficult to track down who has which version of the script. It can also be a challenge when a playwright is revising their own work, because they may not remember all of the XPath expressions they used.

For these reasons, it’s generally best to avoid using XPath whenever possible. If it’s absolutely necessary to use XPath, be sure to document the XPath expressions thoroughly, so that anyone who needs to update them will be able to do so easily.

7. Use CSS selectors with pseudo-classes to target specific elements

Pseudo-classes are used to target elements that are in a certain state, such as :hover or :active. This means that you can use them to target elements that have been interacted with by the user, which is essential for playwrights who want to create interactive plays.

CSS selectors are also important because they allow you to target specific elements on a page, rather than targeting all elements of a certain type. This means that you can be more precise with your targeting, and you’re less likely to accidentally target elements that you didn’t intend to.

Combining these two best practices will help you create more targeted and effective plays, and it will make your plays more enjoyable for your audience.

8. Prefer async/await over promises

Async/await is a relatively new syntax that makes working with asynchronous code much more readable and easier to understand. It’s built on top of promises, so you can still use all the existing promise-based libraries and APIs, but the syntax is much simpler.

Promises are still useful in certain situations, such as when you need to chain multiple asynchronous operations together, but in general, async/await is the way to go.

9. Use test IDs for reliable element selection

When you’re writing a play, it’s important to be able to identify each character and element clearly. Otherwise, you run the risk of confusing your readers or having your characters get lost in the shuffle. By using test IDs, you can ensure that each character and element is uniquely identified, making it easier for your readers to follow along.

Not only will this make your play easier to read, but it will also make it easier to stage. When you have test IDs in place, you can be confident that everyone will know exactly where they need to be and what they need to do. This will save you a lot of time and hassle in the long run, making it well worth the effort to put them in place from the start.

10. Use Page.waitForXxx() methods to wait for events on the page

Suppose you have a play with two scenes. In the first scene, a character enters and exits stage left. In the second scene, another character enters from stage right.

Now, let’s say you want to use Page.waitForSelector() to wait for the second character to enter before continuing. However, if you don’t also use Page.waitForEvent() to wait for the first character to exit stage left, your code will continue running while the first character is still on stage. This could cause all sorts of problems, like the second character walking into the first character, or the first character interrupting the second character’s dialogue.

By using Page.waitForXxx() methods to wait for both events (the first character exiting and the second character entering), you can be sure that your code will only continue running after both events have occurred. This will help ensure that your play runs smoothly and avoid any potential errors.

Previous

10 Intune Device Configuration Profiles Best Practices

Back to Insights
Next

10 Host-Based Firewall Best Practices