npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mocha-puppeteer-launcher

v1.0.1

Published

launch puppeteer without any setup code

Downloads

22

Readme

mocha-puppeteer-launcher

Build Status Coverage Status

write end-to-end tests with mocha and puppeteer without any setup code

Install

npm install mocha-puppeteer-launcher --save-dev

Usage

Mocha-puppeteer-launcher can be used simply by importing the register file contained in this module. Require the script in any or all tests using puppeteer.

require('mocha-puppeteer-launcher/register');
// or
require('mocha-puppeteer-launcher').register();

It's also possible to require the register script directly when calling mocha.

mocha --require mocha-puppeteer-launcher/register my/test/path/*.spec.js

The register script will start puppeteer, expose a newPage function to the global scope and create a global mocha after hook to close the browser after test execution automatically.

describe('my-test', function () {
    this.timeout(20000);
    
    it('my-test-case', async () => {
        let page = await newPage({viewport: {height: 1000, width: 1000}});   
        // test code here
    });    
});

Note

to avoid timeouts, remember to call this.timeout() in tests using puppeteer. Depending on the time chrome takes to launch on your local machine, you may also need to increase the global before test timeout by calling mocha with --timeout timeoutInMs option.

the following global constants are exposed by the register script:

Configuring mocha-puppeteer-launcher

Mocha-puppeteer-launcher can be configured by, creating a mplconfig.json file with the following properties:

  • browserOptions: options for puppeteer's launch call launch options.
  • autoClose: disables the register script from closing the browser with mocha's after hook.
  • keepAlive: delay closing the browser for x ms, after tests execution is finished.

Different config files can be loaded by passing the path to the test call with --mplconfig path.

mocha --require mocha-puppeteer-launcher/register my/test/path/*.spec.js --mplconfig ./settings.json

Without a configuration file or missing properties the following default configuration will be used:

{
  "browserOptions": {
    "headless": true,
    "timeout": 30000
  },
  "autoClose": true,
  "keepAlive": 0
}

Note

If keepAlive causes mocha to run into a timeout, try calling mocha with --timeout x option; with x >= the keepAlive ms specified.

Alternative API-Usage

Mocha-puppeteer-launcher can also be used without exposing constants to the global namespace by using the exported constant browseLauncher. It's still possible to use a mplconfig.json file when using the browserLauncher directly. You can access the exported config object with default settings overwritten by any settings specified in mplconfig.json.

const mpl = require('mocha-puppeteer-launcher');

// global scope

before(async () => {
    await mpl.browserLauncher.start(mpl.config); // config uses the format specified above
});

after(async () => {
    await mpl.browserLauncher.closeBrowser(5000); // closes the browser after 5s delay (optional) 
});

The global before and after hooks can be placed in a separate file containing no tests, so it's included when calling mocha with different patterns.

The browserLauncher allows you to create new Page objects in your test files:

const browserLauncher = require('mocha-puppeteer-launcher').browserLauncher;

it('my-test-case', async () => {
    let page = await browserLauncher.newPage({viewport: {width: 1000, height: 1000}});
    // test code here
});

API

class: BrowserLauncher

start(config)

  • config: MplConfig
  • returns: Promise<void> When the browser finishes loading, the promise is resolved

starts the browser.

newPage(emulateOptions?)

  • emulateOptions: <\EmulateOptions> for details refer to puppeteer API
  • returns: Promise<Page>

creates a new Page and sets Viewport and UserAgent if the option object was passed to the call.

closeBrowser(keepAlive?)

  • keepAlive: number optional time in ms
  • returns: Promise<void>

close the browser. When using register, this function does not need to be called unless autoClose is disabled.

getPuppeteerLoadedPromise()

  • returns: Promise<void> When the browser finishes loading, the promise is resolved

getBrowser()

  • returns: Browser getter for created browser instance

Related

  • puppeteer - Control chrome in headless mode with puppeteer for automated testing.

License

MIT