Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Hands-On Automated Testing with Playwright
Hands-On Automated Testing with Playwright

Hands-On Automated Testing with Playwright: Create fast, reliable, and scalable tests for modern web apps with Microsoft's automation framework

Arrow left icon
Profile Icon Faraz K. Kelhini Profile Icon Butch Mayhew
Arrow right icon
$26.99 $29.99
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (1 Ratings)
eBook Jan 2026 412 pages 1st Edition
eBook
$26.99 $29.99
Paperback
$37.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Faraz K. Kelhini Profile Icon Butch Mayhew
Arrow right icon
$26.99 $29.99
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (1 Ratings)
eBook Jan 2026 412 pages 1st Edition
eBook
$26.99 $29.99
Paperback
$37.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
$26.99 $29.99
Paperback
$37.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Hands-On Automated Testing with Playwright

Summary

In this chapter, we've laid the groundwork for using Playwright by walking through the setup process and exploring key aspects of testing. We began by installing Node.js and Playwright, then distinguished between the core automation library and the testing framework. We wrote a simple first test, configured our test environment with best practices in Node.js and IDE integration, and finally, dove into the inner workings of Playwright’s test runner.By now, you should feel confident in setting up Playwright for your projects. In the next chapter, we will build upon these fundamentals by exploring techniques for interacting with complex web elements using Playwright’s selector engine.

Technical requirements

Prioritizing Playwright’s getBy* locators for accessible tests

When you’re writing end-to-end tests with Playwright, the locators you choose can make a big difference: not just to how reliable your tests are, but also to how accessible your app is. Playwright’s getBy* locators are designed to reflect how real users, including those using assistive tech, interact with your site. By leaning on these locators, you’re not only making your tests easier to maintain but you’re also building a more inclusive experience. Let’s dive into why these locators are preferred.

Why getBy* locators beat CSS and XPath

Playwright makes it easier to work with web elements by offering a handy set of locators such as getByRole, getByLabel, getByPlaceholder, and more. These are designed with accessibility in mind and are a modern alternative to the old-school CSS or XPath selectors (we’ll explore these in more detail shortly).

One of the biggest...

Using fallback selectors (CSS, XPath, text-based) for complex or unsupported cases

While they might not be the most reliable for long-term use, CSS and XPath selectors can be very helpful when dealing with legacy code or third-party components. Sometimes, you just don’t have control over an element’s accessible attributes. If an element doesn’t have ARIA roles, labels, or other accessibility features, locators may not work as expected.

That’s where fallback selectors come in. They can get the job done when HTML is poorly structured or you’re working with external components. Just keep in mind that these selectors often depend on the page’s structure or internal implementation details, which means they can break more easily if the page changes. So, while fallback selectors are useful in certain situations, it’s best to stick with getBy* locators or test IDs (we’ll discuss test IDs in a bit) whenever you can.

CSS selectors...

Managing dynamic elements with auto-wait and custom waits

Playwright has your back with built-in auto-waiting that pauses your test just long enough for things to be ready. But sometimes, you might need a bit more control, and that’s where custom waits come in handy. In this section, we’ll look at how to make your tests more reliable by using Playwright’s smart auto-wait features and adding custom waits when needed.

How Playwright ensures actions happen at the right time

When you’re working with modern web apps, you’ll often run into dynamic elements that show up, disappear, or change depending on what the user does or what’s happening in the background. These changes can make automated testing tricky. If your test tries to interact with something before it’s ready, it might fail or behave inconsistently.

That’s where Playwright’s auto-wait mechanism and a suite of custom wait methods come in: they ensure...

Handling alerts and confirmation dialogs

Working with browser dialogs in Playwright (such as alerts, confirmations, and prompts) is surprisingly easy, thanks to its event-driven API. As soon as a dialog appears on the page, Playwright lets you interact with it right away.

In web applications, these dialogs usually pop up when JavaScript functions such as window.alert(), window.confirm(), or window.prompt() are called. These modal windows pause JavaScript execution until the user responds.

Playwright lets you handle these by listening for the 'dialog' event on the page. When a dialog shows up, Playwright emits the event and hands you a dialog object you can use to understand what kind of dialog it is. You can use the following:

  • dialog.type() to find out whether it’s an alert, confirm, or prompt
  • dialog.message() to read the message shown in the dialog
  • dialog.defaultValue() to see the default input text if it’s a prompt
...

Interacting with iframes and nested frames

Playwright sees every web page as a tree made up of frames. The main document is the primary or main frame, and any <iframe> elements inside it are treated as child frames. Some websites use iframes or even nested frames to organize content, often for security or to keep things modular. If you want to interact with something inside one of those frames, such as clicking a button or typing in a field, you’ll need to switch your focus to that specific frame so your actions happen in the right place.

Accessing an iframe

An <iframe> element creates an inline frame that loads another HTML document. It operates as a separate browsing context, meaning elements inside an iframe are isolated from the parent page.

To work with elements inside an iframe, start by finding the iframe using frameLocator. Just call page.frameLocator(selector) and pass in the iframe’s CSS selector, ID, or another attribute to target it...

Handling Shadow DOM components

In web development, the Shadow DOM lets you encapsulate parts of the DOM (such as custom components) so their styles and elements don’t accidentally interfere with the rest of the page. This makes it a bit tricky to access or interact with those nested elements using traditional DOM selectors. Let’s take a look at ways to pierce through this boundary and interact with these encapsulated elements.

Locating the host element

This is often the most direct way to access elements within a Shadow DOM. You first locate the host element that has the Shadow DOM. From this root locator, you can then query for elements within it using standard Playwright locators.

Let’s imagine you have a custom web component like this:

<my-widget>
  #shadow-root
    <div class="internal-button">Click me!</div>
    <style>
      .internal-button { color: blue; }
    </style>
</my-widget>

Here...

Summary

In this chapter, you’ve explored some of Playwright’s most useful tools for navigating the ever-changing landscape of modern web interfaces. You’ve gotten hands-on with the reliable getBy* locators and learned when it’s best to fall back on traditional selectors.

You also discovered how Playwright’s smart waiting features can help you handle dynamic content smoothly, which makes your tests less prone to flakiness.

On top of that, you learned how to deal with system dialogs, work with iframes, and interact with shadow DOMs, which are key skills for testing today’s complex, component-driven UIs.

As you proceed with building your tests, always remember that the goal is to mimic genuine user interactions while ensuring your code remains resilient in the face of change.

In our next chapter, we will shift focus to browser-agnostic testing, which ensures tests work across Chromium, Firefox, and WebKit.

...
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Architect resilient test suites using Playwright's advanced APIs
  • Optimize performance, parallelism, and debugging for complex apps
  • Integrate cross-browser testing into scalable CI/CD workflows

Description

Hands-On Automated Testing with Playwright draws from Faraz and Butch’s extensive experience to help you harness the full potential of Microsoft's Playwright framework. The book begins with a quick setup refresher, walking you through intermediate and advanced concepts such as dynamic content handling, accessibility testing, AI-generated test scripts, and parallel test execution. You'll explore real-world applications with end-to-end testing workflows for e-commerce and single-page applications. Alongside traditional topics such as accessibility, mobile, and visual regression testing, this book delves into modern trends such as CI/CD pipelines, DevOps integration, and AI-driven testing enhancements. Each chapter is packed with practical examples, expert techniques, and performance optimization tips, helping you gain a deep understanding of maintainable automation strategies. Whether you're transitioning from manual testing or expanding your automation toolbox, this Playwright book provides the skills and confidence to build future-proof testing systems in today's complex web development landscape. *Email sign-up and proof of purchase required

Who is this book for?

This book is for two main groups. First, web developers and test automation engineers with up to five years of experience in JavaScript or similar languages. If you have a solid grasp of web development and want to adopt or deepen your use of Playwright, you'll find this book beneficial. Second, QA professionals who have basic scripting knowledge and are ready to transition from manual to automated testing. If you want to upskill using Playwright, this book offers clear, relatable examples that bridge manual testing concepts with automation practices.

What you will learn

  • Set up and configure Playwright for cross-browser testing
  • Generate and refine AI-powered test scripts
  • Perform mobile, accessibility, and visual regression tests
  • Debug with Playwright Inspector and Trace Viewer
  • Integrate tests into CI/CD with GitHub Actions
  • Use page objects and patterns for maintainable code
  • Simulate real-world user flows in complex apps
  • Optimize performance and parallelize test execution

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jan 19, 2026
Length: 412 pages
Edition : 1st
Language : English
ISBN-13 : 9781806106462
Vendor :
Microsoft
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jan 19, 2026
Length: 412 pages
Edition : 1st
Language : English
ISBN-13 : 9781806106462
Vendor :
Microsoft
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Table of Contents

21 Chapters
Quick Setup Refresher Chevron down icon Chevron up icon
Advanced Selectors and Handling Dynamic Content Chevron down icon Chevron up icon
Browser-Agnostic Testing Across Chromium, Firefox, and WebKit Chevron down icon Chevron up icon
AI-Powered Test Generation Chevron down icon Chevron up icon
Crafting Scalable Tests with the Fixture System Chevron down icon Chevron up icon
Test Parallelization and Performance Optimization Chevron down icon Chevron up icon
Integrating Workflows with CI/CD Pipelines Chevron down icon Chevron up icon
Headless Testing and Debugging Chevron down icon Chevron up icon
Accessibility Testing with Playwright and axe-core Chevron down icon Chevron up icon
Setting Up Visual Regression Testing Chevron down icon Chevron up icon
Testing Mobile Web Experiences Chevron down icon Chevron up icon
Testing Forms Chevron down icon Chevron up icon
Handling File Uploads and Downloads Chevron down icon Chevron up icon
Security and Authentication Chevron down icon Chevron up icon
Best Practices for Test Maintainability Chevron down icon Chevron up icon
Real-World Project – Testing an E-Commerce Website Chevron down icon Chevron up icon
Unlock Your Exclusive Benefits Chevron down icon Chevron up icon
Streamlining Playwright in Modern Development Workflows Chevron down icon Chevron up icon
References and Resources Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(1 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
N/A Feb 18, 2026
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Excellent!!
Feefo Verified review Feefo
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.

Modal Close icon
Modal Close icon