Project set up to start web automation with puppeteer, Mocha

How to set up a web automation project with puppeteer, Mocha, and chai. Environment setup to start automation with puppeteer, Mocha.

In this puppeteer tutorial, We will see how to create a new automation project using puppeteer and Mocha from scratch.

The initial requirement to write the NodeJs code in your machine.

Download and Install NodeJS.

Download NodeJs from the official web site or follow the previous tutorial. How to install nodeJS in the windows machine.

Choose an editor

There are lots of editors available to write the nodeJs or JS code. Like Atom, Nodepade++, TextLime and my favorite Visual studio code. So I am going to user VS code as editor in all tutorials of the puppeteer.

Let’s come to the point and round the eye on some commands that will help you to Setup a puppeteer automation project.

Step 1) Enter the command “npm init”

  • Open Visual Studio Code and select an empty folder
  • Open the terminal and enter the command “npm init”
  • Ans all the questions like the Package name, app version ETC and finish the project setup.

 

Step 2) install puppeteer mocha and chai

npm install puppeteer mocha chai

Now you can see an autogenerate file “package.json” edit this and enter the below command to setup run command in under the script.

"test": "node ./node_modules/mocha/bin/mocha --timeout=30000 ./tests"

Here ./tests  is a folder name that contains test cases. you can change it accordingly.

Now we are ready to go and start the code for puppeteer automation. Let’s write a script to test everyting is working fine till now.

Write an automation script in puppeteer

  • Create a folder “tests”.
  • Create an example.test.js file under the tests folder.

Below, code is just to launch the browser. this is only to test the setup. In upcoming tutorials will cover all in detail.

const puppeteer = require('puppeteer')

describe("Setup Testing",()=>{
     it("Home landing page",async()=>{
    const browser = await puppeteer.launch({headless:false})
      
     });
    });

Command to run the puppeteer script

npm run test

Output:

Puppeteer tutorial for beginners