English 中文(简体)
Puppeteer - Basic Test
  • 时间:2024-11-05

Puppeteer - Basic Test


Previous Page Next Page  

To start with a basic test on Puppeteer, we have to follow the below mentioned steps −

Step 1 − Create a new file within the directory where the node_modules folder is created (location where the Puppeteer and Puppeteer core have been installed).

The details on Puppeteer installation is discussed in the Chapter of Puppeteer Installation.

Right-cpck on the folder where the node_modules folder is created, then cpck on the New file button.

Node Modules

Step 2 − Enter a filename, say testcase1.js.

Testcase1.js

Step 3 − Add the below code in this file −


//adding Puppeteer pbrary
const pt = require( puppeteer );
pt.launch().then(async browser => {
   //browser new page
   const p = await browser.newPage();
   //set viewpoint of browser page
   await p.setViewport({ width: 1000, height: 500 })
   //launch URL
   await p.goto( https://www.tutorialspoint.com/index.htm )
   //capture screenshot
   await p.screenshot({
      path:  tutorialspoint.png 
   });
   //browser close
   await browser.close()
})

Step 4 − Execute the code with the following command −


node <filename>

So in our example, we shall run the following command −


node testcase1.js
Browser in Headless Mode

After the command has been successfully executed, a new file called the tutorialspoint.png gets created within the page directory. It contains the captured screenshot of the page launched in the browser in a headless mode.

Advertisements