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-webdriver-runner

v0.6.4

Published

Run Mocha tests using Selenium WebDriver

Downloads

1,460

Readme

mocha-webdriver-runner

npm Build status

Run Mocha tests in browsers using Selenium WebDriver.

Inspired by mocha-chrome, but with following features implemented from start:

  • drives browser using Selenium WebDriver, so you can run tests on everything that selenium WebDriver supports. Hint: it supports everything (tm).
  • runs reporters locally, in node environment so most of reporters (which are designed to work in node environment) should work out-of-box:
    • tested mocha builtins: spec, xunit, tap, etc ...
    • support for mochawesome (including usage of addContext)

That's it, have fun.

Install

$ npm install mocha-webdriver-runner

(Also ensure that you've got proper drivers or Selenium Grid available, see Browser Driver section below).

Usage

Prepare your tests to run in browser as described on Mocha website.

Add mocha-webdriver-runner browser side client just after normal mocha.js <script> tag:

   <script src="../node_modules/mocha/mocha.js"></script
 + <script src="../node_modules/mocha-webdriver-runner/dist/mocha-webdriver-client.js"></script>

Run the test suite against local browser:

SELENIUM_BROWSER=chrome npx mocha-webdriver-runner test/index.html

SELENIUM_BROWSER=firefox npx mocha-webdriver-runner test/index.html --reporter=tap

(assuming your tests are in test/index.html).

The HTML test page works in two environments:

  • normal browser window - HTML report is generated in browser window as usual
  • when ran through mocha-webdriver-runner, report is forwarded to reporter running in node (default is spec)

See package.json scripts and test/sample-suite/index-headless.html for reference.

Browser capabilities

Use -C key[=value] (or --capability) options to set requested browser capabilities. Value may be plain string, or JSON value, examples:

-C browserName=firefox
-C moz:firefoxOptions.args='["-headless"]'
-C browserName=chrome
-C goog:chromeOptions.args='["--headless", "--window-size=300,300"]'

Convenience shortcuts:

| Shortcut option | Resolves to | - | ---- | --headless-chrome | -C browserName=chrome -C goog:chromeOptions.args='["--headless"]' | --chrome | -C browserName=chrome | --headless-firefox | -C browserName=firefox -C moz:firefoxOptions.args='["-headless"]' | --firefox | -C browserName=firefox | --edge | -C browserName=MicrosoftEdge | --safari | -C browserName=safari

Useful links:

Selenium WebDriverJS accepts capabilities passed by environment variables as below:

SELENIUM_BROWSER=chrome
SELENIUM_BROWSER=firefox:52
SELENIUM_REMOTE_URL=http://my-selenium-grid:4444/wd/hub

See WebDriverJS Builder

Options

  -c, --config <FILE>                     config file (default: ".mocha-webdriver-runner.json")
  -C, --capability <name[=value]>         required browser capability
  -O, --reporter-options <k=v,k2=v2,...>  reporter-specific options
  -R, --reporter <name>                   specify the reporter to use (default: "spec")
  -t, --timeout <ms>                      set test-case timeout in milliseconds (default: 2000)
  -L, --capture-console-log <boolean>     whether to capture console.log in browser context (default: true)
  -g, --grep <pattern>                    only run tests/suites that match pattern
  -V, --version                           output the version number
  --chrome                                use Chrome
  --headless-chrome                       use headless Chrome
  --firefox                               use Firefox
  --headless-firefox                      use headless Firefox
  --safari                                use Safari
  --edge                                  use Edge

Config file

mocha-webdriver-runner can load options from config file. Use -c FILE option to specify custom file. If -c is not specified, mocha-webdriver-runner will attempt to load it from .mocha-webdriver-runner.json.

Config file, is JSON with, following properties:

  • capabilities - object representing WebDriver capabilities
  • all other CLI options are available as properties

Example config for tests on Headless Chrome with no GPU and selecting only tests with #performance tag:

{
    "timeout": 0,
    "grep": "#performance
    "capabilities": {
        "browserName": "chrome",
        "goog:chromeOptions": {
            "args": ["--headless", "--disable-gpu=true"]
        }
    }
}

Browser Drivers

Testing against browser that runs on your desktop requires that proper drivers are installed in your environment.

  • Chrome - requires chromedriver
    • available as NPM packet chromedriver
    • documentation & manual download: http://chromedriver.chromium.org/
  • Firefox - requires geckdriver
    • available as NPM packet geckodriver
    • documentation & manual download: https://firefox-source-docs.mozilla.org/testing/geckodriver/geckodriver/
  • Safari - requires safaridriver
    • SafariDriver (now) is installed by default, see https://developer.apple.com/documentation/webkit/testing_with_webdriver_in_safari

For mocha-webdriver-runner to work, particular webdriver must be installed somwehere in PATH.

Note, convenience NPM packages -chromedriver and geckdriver - install them it to ./node_modules/.bin, so if you run your tests via npm drivers are found automagically.

API

Node.Js

From Node.js you can start tests using runMochaWebDriverTest.

// in node.js context
import { runMochaWebDriverTest } from "mocha-webdriver-runner";

const webDriverCapabilities = {
    browserName: "firefox"
};

runMochaWebDriverTest(webDriverCapabilities, "https://localhost:8080/test/index.html")
    .then(result => {
        // result is boolean i.e ok or not ok
        console.log("test result", result ? ":)" : ":(");
    })
    .catch(error => {
        // something bad happened with runner itself i.e webdriver error or something
    });

Browser general API

Browser module export global object MochaWebdriverClient.

Import examples:

<!-- from CDN -->
<script src="https://unpkg.com/mocha-webdriver-runner/dist/mocha-webdriver-client.js"></script>
<!-- from local node_modules -->
<script src="../node_modules/mocha-webdriver-runner/dist/mocha-webdriver-client.js"></script>

MochaWebdriverClient API

  • addMochaSource(mocha) - instruments mocha instance to send runner events back to mocha-selenium-runner process.

    Example:

    mocha.setup({ ui: "bdd" });
    MochaWebdriverClient.install(mocha);
    // load sources
    mocha.run();
  • addWorkerSource(worker: Worker) - forwards all mocha-selenium-runner related events from worker back to mocha-selenium-runner process (requires properly initialized mocha in worker context

    Example:

    const worker = new Worker("some-test-worker.js");
    MochaWebdriverClient.addWorkerSource(worker);

Examples:

Contribute

PRs accepted.

License

MIT © Zbigniew Zagórski