PuppeteerPuppeteer Tutorials

Puppeteer Tutorial | How To Run Your First Puppeteer Test

After you learn how to install Puppeteer on your local computer In the previous article you are ready for this puppeteer tutorial to write and run your first test.

The First Test

For this demo, I will use the Visual Studio Code editor.
Create a new file with the name test1.js a blank page should open with the given name.
In each test file in puppeteer, you need to add at the top of the page the command:

const puppeteer = require('puppeteer');

this command will give you access to the puppeteer package with all the capabilities of the API.
Tests must be executed from the async function otherwise it will not compile.
Puppeteer running inside the browser and there is no need to install any driver to be able to run tests with the browser.

To launch the browser you will need to use the launch command:

const browser = await puppeteer.launch();

And to open a new tab (page):

const page = await browser.newPage();

After you set the launch and the new page command you should target the endpoint URL of the page you want to open by set the goto command:

await page.goto('https://example.com');

At the end of each test, you must set the close command to force puppeteer to close the browser:

await browser.close();

By default, Puppeteer run in headless mode but you can modify it by setting the headless to {headless: false} inside the launch command and this will run the test with opening the browser.

Run The Test

To run the test you need to open the terminal window by clicking on the Terminal tab at the top navbar -> New Terminal.

This action will open the Terminal window at the bottom of the editor, inside the terminal type node test1.js , and press enter.
Now the test will run.


In this puppeteer tutorial, you learned how to run a test with puppeteer.
remember the tests runs only inside the async function, you also need to set the puppeteer require command and closing the browser at the end of the test.

The overall outcome should look like this:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');

  await browser.close();

Ofir Blumental

QA Automation Engineer - ofirblumental1@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button