Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Screenshotting support #109

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,27 @@ mocha: {

The PhantomJS -> Grunt superdimensional conduit uses `alert`. If you have disabled or aliased alert in your app, this won't work. I have conveniently set a global `PHANTOMJS` on `window` so you can conditionally override alert in your app.

## Screenshotting

To take a screenshot:
```js
var container = $('article')[0]; // or any DOM node
console.log(JSON.stringify({
action: 'render', // mandatory
filename: 'screenshots/article.png', // mandatory
clipRect: container.getBoundingClientRect() // optional
}));
```

To change the resolution of the viewport:
```js
console.log(JSON.stringify({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can may now configure a viewport size in available in newer versions of the grunt-lib-phantomjs dependency. So, maybe bump the dependency version?
https://github.com/gruntjs/grunt-lib-phantomjs/blob/master/Gruntfile.js#L74-78

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@robcolburn we needed to be able to keep changing the viewport during testing - especially with our responsive Sky.com websites that cater for mobile, tablet and desktop viewports.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Makes sense. Having both (grunt) declarative and programmatic control of viewport is ideal.

action: 'viewportSize', // mandatory
width: '960', // mandatory
height: '960' // mandatory
}));
```

## Examples

### Vanilla JS
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "grunt-mocha",
"description": "Grunt task for running client-side Mocha specs in PhantomJS",
"name": "grunt-mocha-screenshot",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't modify the package name or description. :)

"description": "grunt-mocha with screenshot added pending pull request",
"version": "0.4.10",
"homepage": "https://github.com/kmiyashiro/grunt-mocha",
"author": {
Expand Down
15 changes: 15 additions & 0 deletions phantomjs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ page.onAlert = function(str) {

// Relay console logging messages.
page.onConsoleMessage = function(message) {
try {
var json = JSON.parse(message);
if (json.action === 'viewportSize') {
sendMessage('console', 'Changing viewport to width: ' + json.width + ' and height: ' + json.height);
page.viewportSize = { width: json.width, height: json.height };
} else if (json.action === 'render') {
sendMessage('console', 'Taking a screenshot to file: ' + json.filename);
if (page.clipRect) {
page.clipRect = json.clipRect;
}
page.render(json.filename);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we lift filename to a grunt option or automate it?
Example: https://github.com/gruntjs/grunt-lib-phantomjs/blob/master/phantomjs/main.js#L46

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@robcolburn we've done both using what's in this PR. What we've done is to create a global function screenshot(container) where container is the HTML DOM element that we want to take a screenshot of. The filename is calculated through using a folder specified in the Mocha setup.js file and the container's id. I thought it was best to offer the users the flexibility of coming up with what works best for them, through offering a very simple API to write a function for (copied from README.md):

To take a screenshot:

var container = $('article')[0]; // or any DOM node
console.log(JSON.stringify({
  action: 'render', // mandatory
  filename: 'screenshots/article.png', // mandatory
  clipRect: container.getBoundingClientRect() // optional
}));

The user can of course replace the 'screenshots/article.png' there with, for example opts.screenshotFolder + new Date() + 'png'.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still feel like I'd want to define the pattern along with other grunt configuration, rather than application-level code. But, that's just splitting hairs when there's a working solution here.

}
} catch(err) {
sendMessage('console', 'Console message could not be parsed.');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This catch statement seems like it could get rather chatty with application code also using the console. Maybe scrap it or put the output behind a specific flag?

}
sendMessage('console', message);
};

Expand Down