top of page
90s theme grid background
Writer's pictureGunashree RS

Puppeteer Show: Guide to Web Automation and Debugging

Introduction

In the evolving world of web automation, testing frameworks like Puppeteer have become indispensable for developers and automation testers. Puppeteer Show refers to the process of using Puppeteer to run tests and scripts while being able to "show" what happens in the browser. It allows for more effective debugging by visually observing the actions executed during automation. This can be particularly helpful in identifying issues with scripts, verifying behaviors, and fine-tuning performance.


Puppeteer is a Node.js-based framework that provides control over Chrome or Chromium browsers through JavaScript. Initially designed to run in headless mode (where browsers run in the background without a UI), Puppeteer offers an option to disable this mode and run browsers visually. This allows for an in-depth look at the test or script execution in real-time, hence giving birth to the term Puppeteer Show.


This comprehensive guide will walk you through various techniques, methods, and best practices to show and debug your Puppeteer tests effectively. Whether you’re a novice to web automation or an experienced tester, this guide will provide valuable insights into Puppeteer’s debugging capabilities.


Puppeteer Show


What is the Puppeteer Show?

Puppeteer Show is an advanced feature of the Puppeteer framework, where developers can observe and debug their automated browser tests by running them with the browser’s UI (user interface) visible. This “show” mode allows developers to spot errors and fix bugs in real-time, making it a more practical way to test dynamic websites and applications.

With Puppeteer Show, you can:

  • Visualize browser actions like clicks, scrolling, or form submissions.

  • Debug tests live by inspecting browser events and console logs.

  • Improve test reliability by ensuring that scripts are interacting correctly with elements.

By default, Puppeteer runs in headless mode, which means that it executes scripts without opening a browser window. However, with the Puppeteer Show feature, you can disable this headless mode and see how the browser behaves during test runs.



Why You Need to Debug with Puppeteer Show

Debugging is a critical aspect of software development. Testing and debugging web applications with tools like Puppeteer Show offers several advantages, such as:

  1. Improved error detection: It helps developers spot errors more easily by visually displaying browser interactions.

  2. Enhanced test precision: It allows you to fine-tune test automation scripts and ensures they work correctly across various browsers and platforms.

  3. Streamlined troubleshooting: Puppeteer Show provides real-time visual feedback, helping developers troubleshoot complex scenarios with greater clarity.



Prerequisites for Running Puppeteer Show

Before diving into Puppeteer Show, you must have a few tools installed:

  1. Node.js: Puppeteer requires Node.js to function. Make sure you have it installed.

  2. Puppeteer framework: You can install Puppeteer via npm by running the command npm install puppeteer.

  3. Visual Studio Code (optional): Though not mandatory, using VS Code helps streamline the debugging process with its built-in debugging tools.



Setting Up Puppeteer for Automation Testing

Puppeteer setup is relatively simple. Below is the basic code snippet to initialize Puppeteer in your test environment:

javascript

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({ headless: false }); // Disable headless mode
  const page = await browser.newPage();
  await page.goto('https://example.com'); // Navigate to the website
  await page.screenshot({ path: 'example.png' }); // Take a screenshot
  await browser.close();
})();

This code will open the browser, navigate to the URL, and take a screenshot of the page.


How to Debug Puppeteer Tests

Puppeteer offers multiple ways to debug your test scripts effectively. Let’s explore these methods:


1. Disable Headless Mode

The first step in Puppeteer Show is to disable headless mode. Headless mode runs tests in the background without a browser window. Disabling it allows you to see how your scripts interact with the browser.

javascript

const browser = await puppeteer.launch({ headless: false });

In Jest Puppeteer, you can configure the headless flag in jest-puppeteer.config.js:

javascript

module.exports = {
  launch: {
    headless: false,
    product: 'chrome',
    args: ['--start-maximized'],
    defaultViewport: { width: 1700, height: 800 },
  },
};

2. Slow Down Execution Speed

Sometimes, Puppeteer’s speed can make it hard to spot errors in real-time. You can slow down the execution with the slowMo option.

javascript

const browser = await puppeteer.launch({
  headless: false,
  slowMo: 250, // slow down by 250ms
});

This adds a delay between actions, making it easier to see what’s happening.


3. Use console.log() to Debug

Using console.log() in your Puppeteer tests can help track variable values, element selections, and more.

javascript

console.log("The element's text content is:", elementText);

This is particularly useful when combined with other debugging techniques like disabling headless mode.


4. Set devtools Flag

Enabling the devtools flag opens Chrome DevTools automatically, providing access to advanced debugging features such as inspecting elements, viewing network activity, and accessing the console.

javascript

const browser = await puppeteer.launch({ devtools: true });

5. Increase Timeout for Jest Puppeteer Debugging

Puppeteer tests often time out in Jest after 5 seconds by default. Increasing the timeout duration allows more time for your tests to run.

javascript

jest.setTimeout(50000); // Set timeout to 50 seconds

6. Enable Verbose Logging

Verbose logging provides additional debugging information during test execution.

bash

$env:DEBUG="puppeteer:*"
npm run test

7. Add debugger Keyword

The debugger keyword pauses the execution of the test and opens Chrome DevTools for in-depth analysis.

javascript

debugger; // Pauses test execution

8. Integrating Puppeteer with Visual Studio Code for Debugging

You can integrate Puppeteer with Visual Studio Code for an enhanced debugging experience. Configure the launch.json file in VS Code:

json

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Jest",
      "program": "${workspaceFolder}/node_modules/jest/bin/jest",
      "args": ["--config=${workspaceFolder}/jest.config.js"],
      "internalConsoleOptions": "openOnSessionStart",
      "skipFiles": ["<node_internals>/**"]
    }
  ]
}

This setup allows you to place breakpoints, step through your code, and inspect variables during execution.


9. Debug Puppeteer Tests with ndb

ndb is an advanced Node.js debugging tool integrated with Chrome DevTools. Install ndb with the following command:

bash

npm install --save-dev ndb

Run your tests with ndb:

bash

npx ndb npm run test

ndb will open a debugging window where you can set breakpoints and pause execution for real-time debugging.



Conclusion

Debugging is an essential part of the web development process, and Puppeteer Show provides an intuitive and visual way to debug web automation tests. By disabling headless mode, utilizing slow motion, and leveraging other debugging tools like Chrome DevTools, you can gain deep insights into your test scripts and make informed improvements.

Whether you're new to Puppeteer or an experienced tester, the debugging techniques discussed in this guide will equip you with the tools you need to refine your tests, improve performance, and ensure cross-browser compatibility.



Key Takeaways

  • Disable headless mode to view browser interactions visually.

  • Use console.log() to output variable values and help with debugging.

  • Enable Chrome DevTools for advanced debugging tools like element inspection.

  • Slow down execution using the slowMo option for better observation.

  • Increase timeout limits to prevent premature test failures.

  • Add breakpoints in Visual Studio Code for a streamlined debugging experience.

  • Utilize ndb for enhanced debugging with Chrome DevTools.





FAQs


1. What is Puppeteer Show?

Puppeteer Show refers to running browser automation in non-headless mode to visually observe and debug browser interactions.


2. How do I disable the headless mode in Puppeteer?

You can disable headless mode by setting the headless option to false when launching Puppeteer.


3. Why should I use slow motion in Puppeteer?

Slow motion helps you observe each browser action in real-time, making it easier to detect issues during test execution.


4. Can I debug Puppeteer tests in Visual Studio Code?

Yes, by configuring a launch.json file, you can integrate Puppeteer debugging with Visual Studio Code.


5. What is ndb and how does it help with Puppeteer debugging?

ndb is a debugging tool for Node.js applications that integrates with Chrome DevTools, providing an interactive environment for debugging Puppeteer tests.



Article Sources

Comments


bottom of page