@touch4it/puppeteer v2.0.0
Puppeteer wrapper
Wrapper around Puppeteer library to make it easier to use.
Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium.
Installing
Install puppeteer wrapper from npm
npm i puppeteer @touch4it/puppeteeror
npm i puppeteer-core @touch4it/puppeteerIn order to use it you will need to require it first
const puppeteerT4IT = require('@touch4it/puppeteer');Documentation
Get Puppeteer instance
Returns Puppeteer library.
const puppeteer = puppeteerT4IT.getPuppeteer();Change default TIMEOUT and SCREENSHOT_DIRECTORY vars
If you want to use different timeout and screenshot directory, you can use init function to change these variables. You can pass following arguments into this function:
timeoutDefault request timeout which will be use in the module (defaults to30000ms)screenshotDirectoryName of the directory where screenshots will be stored (defaults toscreenshots/)
puppeteerT4IT.init(5000, 'some_other_dir/');Launch new browser instance
You can pass option object as argument, which can contain all available option for launch() function
Returns new Browser instance.
let browser = await puppeteerT4IT.launch({ headless: false });Get new page instance
You can pass following arguments into this function:
browserA Browser instance that you have created with puppeteer.launch()authenticationDataObject containing "username" and "password" authentication keys for basic auth when the page is launched
Returns new Page instance.
let page = await puppeteerT4IT.getPage(browser, { username: 'User', password: '123abc' });Navigate to web page and test status code
You can pass following arguments into this function:
pagePuppeteer page objecturlURL to be redirected tostatusCodeawaited HTTP response status code (defaults to200)
Returns page
await puppeteerT4IT.goto(page, baseUrl);Timeout execution for specified amount of milliseconds
You can pass following arguments into this function:
mstime in ms to wait
Returns empty Promise
await puppeteerT4IT.sleep(500);Make screenshots from web page
You can pass following arguments into this function:
pagePuppeteer page objectwidthScreen widthheightScreen heightpageNameWeb page name under which a screenshot will be storeddeviceScaleFactorSpecify device scale factor (Defaults to1)isMobileisMobile Whether the meta viewport tag is taken into account (defaults tofalse)delayTime in ms to wait after reload (defaults to0)beforeActionFunction to be called before screenshot is made (defaults tonull)beforeActionParametersbeforeAction parameters (defaults to[])
Returns empty Promise
await puppeteerT4IT.screenshotPage(page, 360, 640, 'frontpage', 1, true, delay, clickOnButton, ['.navigation-element--class']);Make screenshots in most popular screen resolutions
You can pass following arguments into this function:
pagePuppeteer page objectnameWeb page name under which screenshots will be storeddelayTime in ms to wait after reload (defaults to0)beforeActionFunction to be called before screenshot is made (defaults tonull)beforeActionParametersbeforeAction parameters (defaults to[])
Returns empty Promise
await puppeteerT4IT.screenshotMultipleResolutions(page, 'projects', 4000);Click on element
You can pass following arguments into this function:
pagePuppeteer page objectquerySelectorPage element selectordelayTime in ms to wait after reload (defaults to0)
Returns empty Promise
await puppeteerT4IT.click(page, '.class', 500);Tap on element
You can pass following arguments into this function:
pagePuppeteer page objectquerySelectorPage element selectordelayTime in ms to wait after reload (defaults to0)
Returns empty Promise
await puppeteerT4IT.tapOnElement(page, '.class', 500);Get href link from element
You can pass following arguments into this function:
pagePuppeteer page objectquerySelectorPage element selector
Returns link href from element
await puppeteerT4IT.getLinkByQuerySelector(page, '.class');Get href link hostname from element
You can pass following arguments into this function:
pagePuppeteer page objectquerySelectorPage element selector
It returns link hostname from element.
await puppeteerT4IT.getHostnameByQuerySelector(page, '.class');Get value from element
You can pass following arguments into this function:
pagePuppeteer page objectquerySelectorPage element selector
Returns value of element
await puppeteerT4IT.getValueByQuerySelector(page, '.class');License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
Project updates are tracked in a CHANGELOG file