Automate CSS regression testing with PhantomCSS
This is a fork of the original grunt-phantomcss, with the following updates and enhancements:
- Anselmh's updates detailed here
- More modular file structure, storing test baseline(s) and result(s) in the directory of the test file itself rather than within a single root directory. This keeps the baselines, results, and tests together with less coupling between tests.
- PhantomJS updated to v1.98 to fix SSLv3 bug
- New rootUrl option, to accomodate testing against multiple envirronments
Currently this fork is not available on npm, however, you can install and use this version by following the steps below.
This plugin requires Grunt
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin through the following steps.
Install from the command line:
$ npm install @micahgodbolt/grunt-phantomcss --save-dev
Or add the following line to your
Then, once the plugin has been installed via
The "phantomcss" task
In your project's Gruntfile, add a section named
phantomcss to the data object passed into
The test files to run.
The change percentange tolerated between screenshots (for instance to match anti-aliasing bugs).
The screenshots directory, relative to the src file, where test fixtures (comparison screenshots) are stored. Baseline screenshots will be stored here on the first run if they're not present.
The directory, relative to the src file, to store source, diff, and failure screenshots after tests.
The viewport size to test the site in
[width, height] format. Useful when testing responsive layouts.
The CasperJS log level. See CasperJS: Logging for details.
Optional parameter passed to testfiles for prepending to relative URL's. Useful when testing against multiple environments.
Optional array of CLI arguments passed when running phantomJS. See PhantomJS Command-line Options for details.
// Example PhantomJS Command-line Optionsphantomcss:options:...phantomjsArgs:'--ssl-protocol=tlsv1''--ignore-ssl-errors=true'
Basic visual tests
Run tests in
test/visual/ against comparison screenshots stored in
test/visual/screenshots/, and put the resulting screenshots in
Responsive layout testing
Run tests in
test/visual/ against comparison screenshots for destop and mobile. Pass rootUrl option to specify testing against
Sample test file
Test files should do the following:
- Instruct CasperJS to open the URL you want to test. Grunt automatically envokes
casper.start()when it begins, so all test files need to start with
- Manipulate the page in some way if necessary. PhantomJS is known to have trouble rendering web fonts and, as such, replacing text with a standard web font may be warranted.
- Take screenshots
You can also make URL's relative, prepending the rootUrl to them:
For further examples, refer to the following posts: