UI Testing with Puppeteer

Book description

Write fail-safe automation tests, device emulation, and browser automation using Puppeteer's Google-powered API

Key Features

  • Get up and running with Puppeteer and discover best practices for automation testing
  • Automate your modern web applications using Jest and Mocha with Puppeteer
  • Generate screenshots using Puppeteer and find out how they can be used for UI regression tests

Book Description

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.

What you will learn

  • Understand browser automation fundamentals
  • Explore end-to-end testing with Puppeteer and its best practices
  • Apply CSS Selectors and XPath expressions to web automation
  • Discover how you can leverage the power of web automation as a developer
  • Emulate different use cases of Puppeteer such as network speed tests and geolocation
  • Get to grips with techniques and best practices for web scraping and web content generation

Who this book is for

The book is for QA engineers, testing professionals, and frontend web developers alike who want to perform end-to-end testing using Google's developer tools. Web developers who want to learn how to use Puppeteer for generating content, scraping websites, and evaluating website performance will also find this book useful. Although knowledge of Node.js isn't necessary, basic JavaScript knowledge will assist with understanding the concepts covered.

Table of contents

  1. UI Testing with Puppeteer
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Disclaimer
    6. Download the color images
    7. Conventions used
    8. Get in touch
    9. Reviews
  6. Chapter 1: Getting started with Puppeteer
    1. What is browser automation?
      1. Selenium and Puppeteer
    2. Introducing Headless browsers
      1. Available headless browsers
      2. The Chromium DevTools Protocol
      3. Introducing Puppeteer
      4. The Puppeteer object model
    3. Puppeteer use cases
      1. Task Automation
      2. Web Scraping
      3. Content generation
      4. End-to-end testing
    4. Setting up the environment
      1. Node.js
      2. Visual Studio Code
    5. Our first Puppeteer code
      1. Installing Puppeteer
      2. Hello world in Puppeteer
    6. Asynchronous programming in JavaScript
      1. Promise.all
      2. Promise.race
      3. Fulfilling our own promises
    7. Summary
  7. Chapter 2: Automated Testing and Test runners
    1. Technical requirements
    2. Introduction to Automated Testing
      1. Unit tests
      2. Service tests
      3. End-to-end tests
    3. Test runner features
    4. Available test runners
    5. Creating our first test project
    6. Organizing our code
      1. Introducing the Page Object Model
    7. Summary
  8. Chapter 3: Navigating through a website
    1. Technical requirements
    2. Introducing the tests sites for this chapter
    3. Creating a Puppeteer browser
      1. Using the Puppeteer.launch function
      2. Headless
      3. The user data directory
      4. Executable Path
      5. Default Viewport
      6. Product
      7. Browser Arguments
      8. Mobile options
      9. Options in practice
    4. Navigating through a site
      1. Timeouts
      2. waitUntil
      3. Referrer
    5. Using the response object
      1. Getting the response URL
      2. Getting the response status code
    6. Introduction to continuous integration
    7. Summary
  9. Chapter 4: Interacting with a page
    1. Technical requirements
    2. Introduction to HTML, the DOM, and CSS
      1. HTML
      2. The DOM
      3. CSS Selectors
    3. Finding elements
    4. Finding elements using XPath
    5. Interacting with Elements
      1. Typing on input elements
      2. Clicking on elements
      3. Selecting options in drop-down lists
    6. Keyboard and Mouse emulation
    7. Debugging tests with Visual Studio Code
    8. Summary
  10. Chapter 5: Waiting for elements and network calls
    1. Technical requirements
    2. Waiting for the page to load
    3. Waiting for elements
      1. Await a wait function
    4. Waiting for network calls
      1. Arrange, Act, Await
      2. Fire and forget
      3. Promise.all
    5. Waiting for page events
      1. The close event
      2. The popup event
      3. Target created event
      4. The console event
      5. The dialog event
    6. The headless recorder
    7. Summary
  11. Chapter 6: Executing and Injecting JavaScript
    1. Technical requirements
    2. Executing JavaScript code
      1. Variable scopes in JavaScript
    3. Manipulating handles with JavaScript code
      1. Getting information from the elements
      2. Acting on elements
      3. Enforcing server rules
      4. Finding elements using JavaScript
    4. Waiting for functions
    5. Exposing local functions
    6. Running our checks with Checkly
    7. Summary
  12. Chapter 7: Generating Content with Puppeteer
    1. Technical requirements
    2. Taking screenshots
    3. Using screenshots for regression tests
    4. Generate PDF files
    5. Creating HTML content
    6. Summary
  13. Chapter 8: Environments emulation
    1. Technical requirements
    2. Understanding the browser's market share
      1. The browser's popularity over the years
      2. Operative Systems market share
      3. Screen resolution distribution
    3. Emulating mobile devices
      1. The Viewport
      2. Pixel ratio
      3. The touchscreen
      4. The user agent
      5. Emulating mobile devices with Puppeteer
    4. Emulating network conditions
    5. Emulating localization
      1. Emulating geolocation
      2. Emulating time zones
      3. Emulating languages
    6. Other emulations
    7. Summary
  14. Chapter 9: Scraping tools
    1. Technical requirements
    2. Introduction to web scraping
      1. Does the site allow scrapers?
    3. Creating scrapers
    4. Running scrapers in parallel
    5. How to avoid being detected as a bot
    6. Dealing with authorization
    7. Summary
  15. Chapter 10: Evaluating and Improving the Performance of a Website
    1. Technical requirements
    2. The Issue of Performance
    3. Getting started with Google Lighthouse
      1. As part of Chrome DevTools
      2. Using PageSpeed Insights
      3. Using the command line
      4. Using the node module
      5. The performance category
      6. Using Lighthouse for testing
    4. Tracing Pages
    5. Analyzing code coverage
    6. Summary
    7. Why subscribe?
  16. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Leave a review - let other readers know what you think

Product information

  • Title: UI Testing with Puppeteer
  • Author(s): Dario Kondratiuk
  • Release date: March 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781800206786