Book Image

UI Testing with Puppeteer

By : Dario Kondratiuk
Book Image

UI Testing with Puppeteer

By: Dario Kondratiuk

Overview of this book

Puppeteer is an open source web automation library created by Google to perform tasks such as end-to-end testing, performance monitoring, and task automation with ease. Using real-world use cases, this book will take you on a pragmatic journey, helping you to learn Puppeteer and implement best practices to take your automation code to the next level! Starting with an introduction to headless browsers, this book will take you through the foundations of browser automation, showing you how far you can get using Puppeteer to automate Google Chrome and Mozilla Firefox. You’ll then learn the basics of end-to-end testing and understand how to create reliable tests. You’ll also get to grips with finding elements using CSS selectors and XPath expressions. As you progress through the chapters, the focus shifts to more advanced browser automation topics such as executing JavaScript code inside the browser. You’ll learn various use cases of Puppeteer, such as mobile devices or network speed testing, gauging your site’s performance, and using Puppeteer as a web scraping tool. By the end of this UI testing book, you’ll have learned how to make the most of Puppeteer’s API and be able to apply it in your real-world projects.
Table of Contents (12 chapters)

Waiting for the page to load

In Chapter 3, Navigating through a website, we talked about navigation through a site. We covered functions such as goto, goBack, goForward, and reload. One of the options these functions have is the waitUntil option. This option will help us determine when the function we are calling will be resolved. Let's do a quick recap. We have four options there:

  • domcontentloaded, which relies on the DOMContentLoaded event.
  • load: If you pass this option, the promise will be resolved when the load event is fired.
  • networkidle0 will resolve the promise when there are no more network connections for the past 500 ms.
  • networkidle2 will resolve the promise when there are no more than 2 network connections for the past 500 ms.

Let's see how these options work with a site full of content such as https://shop.mango.com/gb. We are going to see what content is ready, depending on which waitUntil is used:

Mango home...