Run nocha browser tests in phantomjs via the command line
Since 4.0, the phantomjs code now is in mocha-phantomjs-core. If you need full control over which phantomjs version to use and where to get it, or want to use it more programatically like a build system plugin, please use that package directly. This project is a node.js CLI around it.
process.stdout.write, done right. Mocha is primarily written for node, hence it relies on writing to standard out without trailing newline characters. This behavior is critical for reporters like the dot reporter. We make up for PhantomJS's lack of stream support by both customizing
console.log and creating a
process.stdout.write function to the current PhantomJS process. This technique combined with a handful of fancy ANSI cursor movement codes allows PhantomJS to support Mocha's diverse reporter options.
Proper exit status codes from PhantomJS using Mocha's failures count. So in standard UNIX fashion, a
0 code means success. This means you can use mocha-phantomjs on your CI server of choice.
You can use your existing Mocha HTML file reporters side by side with mocha-phantomjs. This gives you the option to run your tests both in a browser or with PhantomJS, with no changes needed to your existing test setup.
We distribute mocha-phantomjs as an npm package that is easy to install. Once done, you will have a
mocha-phantomjs binary. See the next usage section for docs or use the
Usage: mocha-phantomjs [options] pageOptions:-h, --help output usage information-V, --version output the version number-R, --reporter <name> specify the reporter to use-f, --file <filename> specify the file to dump reporter output-t, --timeout <timeout> specify the test startup timeout to use-g, --grep <pattern> only run tests matching <pattern>-i, --invert invert --grep matches-b, --bail exit on the first test failure-A, --agent <userAgent> specify the user agent to use-c, --cookies <Object> phantomjs cookie object-h, --header <name>=<value> specify custom header-k, --hooks <path> path to hooks module-s, --setting <key>=<value> specify specific phantom settings-v, --view <width>x<height> specify phantom viewport size-C, --no-color disable color escape codes-p, --path <path> path to PhantomJS binary--ignore-resource-errors ignore resource errorsAny other options are passed to phantomjs (see `phantomjs --help`)Examples:$ mocha-phantomjs -R dot /test/file.html$ mocha-phantomjs --ignore-ssl-errors=true$ mocha-phantomjs -p ~/bin/phantomjs /test/file.html
Now as an node package, using
mocha-phantomjs has never been easier. The page argument can be either a local or fully qualified path or a http or file URL.
--reporter may be a built-in reporter or a path to your own reporter (see below). See phantomjs WebPage settings for options that may be supplied to the
Since 4.0, you need no modifications to your test harness markup file to run. Here is an example
<!-- encoding must be set for mocha's special characters to render properly -->
Mocha-phantomjs supports creating screenshots from your test code. For example, you could write a function like below into your test code.
if windowcallPhantomvar date =var filename = "screenshots/" + dategetTimeconsole.log"Taking screenshot " + filenamecallPhantom'screenshot': filename
If you want to generate a screenshot for each test failure you could add the following into your test code.
afterEachif thiscurrentTeststate == 'failed'takeScreenshot
Mocha-phantomjs does not scrap the web page under test! No other PhantomJS driver stacks up to our runner support. Some have used a debounce method to keep duplicate messages in the spec reporter from showing up twice. Loosing one of Mocha's console reporters neatest features, initial test start feedback. The animation below is an example of how our runner script fully compiles with expected Mocha behavior.
The following is a list of tested reporters. Since moving PhantomJS 1.9.1, most core Mocha reporters should "just work" since we now support stdout properly. Reporters with node dependencies will not work, like
html-cov. If you have an issue with a reporter, open a github issue and let me know.
The default reporter. You can force it using
spec for the reporter argument.
dot for the reporter argument.
The PhantomJS process has no way of knowing anything about your console window's width. So we default the width to 75 columns. But if you pass down the
COLUMNS environment variable, it will pick that up and adjust to your current terminal width. For example, using the
$COLUMNS variable like so.
env COLUMNS=$COLUMNS phantomjs mocha-phantomjs.coffee URL dot
Bundled and tested reporters include:
require: You can only require other reporters, like
require('./base')to build off of the BaseReporter
module: Export your reporter class as normal
process.stdout.writepreferrably to support the
Simple! Just clone the repo, then run
npm install and the various node development dependencies will install to the
node_modules directory of the project. If you have not done so, it is typically a good idea to add
/node_modules/.bin to your
$PATH so these modules bins are used. Now run
npm test to start off the test suite.
We also use Travis CI to run our tests too. The current build status:
Released under the MIT license. Copyright (c) 2015 Ken Collins, Nathan Black, and many generous GitHub Contributors.