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-selenium-bridge

v0.3.0

Published

Run tests in the browser, report results in Node

Downloads

64

Readme

mocha-selenium-bridge

This is a tool that allows you to run Mocha tests in browser environments from the command line, while also reporting the test results to your command line terminal. This allows you to take a pre-existing Node test suite and run it in multiple environments, or to define complex automated browser tests without needing to use the selenium-webdriver API.

The normal way of using Mocha and Selenium together is a bit tricky, as practically every test needs to undertake multiple operations to interact between the Node and browser environments. Our approach circumvents all of that by running Mocha entirely in the browser, and only sending the events one-way across the Selenium interface.

Setup

First, install the package along with mocha itself:

npm install --save-dev mocha-selenium-bridge mocha

Next, prepare your browser-based tests as usual. In addition to including mocha.js, also add our Bridge reporter:

<script src="../node_modules/mocha-selenium-bridge/browser/reporter.js"></script>

Alternatively, the reporter is also available via unpkg:

<script src="https://unpkg.com/mocha-selenium-bridge/browser/reporter.js"></script>

If included after mocha.js, the reporter will automatically register itself. You may also register it manually:

mocha.reporter(Bridge)

In order to further configure Mocha, you should use the Mocha JavaScript API, as the browser instance does not support command-line options or configuration files.

Usage

As your tests run, the Mocha test events will be queued in a buffer that'll be read by the Node part of mocha-selenium-bridge, and there reconstructed into corresponding events. These can then in turn be fed into any of the standard Mocha reporters.

To make that work, a CLI is provided. This command will spawn a new headless Chrome instance and open your local file test.html in it. Provided that it includes a mocha.run() call, the tests will automatically run, and on completion the browser will be closed:

npx mocha-selenium-bridge test.html --browser chrome

By default, messages logged to the browser console will be also logged to the terminal, and any calls to console.error will cause the test run to fail. To adjust this behaviour, use --silent and --allow-console.

For more complete documentation of the CLI, use its --help argument. Note in particular --driver, which allows you to define and build your own WebDriver for use e.g. with BrowserStack or other Selenium cloud providers.

JavaScript API

import executeTests from 'mocha-selenium-bridge'

const executeTests: (driver: WebDriver, Reporter: Function, url: string, options?: {
  eventsName = '_TEST_EVENTS';
  runnerName = '_TEST_RUNNER';
  interval = 100; // buffer poll interval: 100ms
  timeout = 10000; // maximum time between events: 10s
}) => Promise<number>

The first three arguments are all required. Reporter should be a Mocha reporter; by default the CLI uses require('mocha/lib/reporters').spec. On success, the returned promise resolves to the count of failed tests, so may be used directly as an exit code. Your application code should take care of calling driver.quit() as appropriate.