Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get a page screenshot from within a mocha/phantomjs unit test?

I'm using grunt-mocha to run unit tests via phantomJS.

I know that phantomJS has a lot of useful functionality. Can I access that from within a mocha test?

I've looked in the obvious places such as the window object to see if I can somehow access the page object, but nothing seems obvious.

Specifically, I'd like to render a screenshot of the page under test.

like image 582
izb Avatar asked Nov 04 '22 01:11

izb


1 Answers

This is called "Screen Capture" in the documentation.

The important part is that you need a reference to the WebPage, not the browser window object (this just emulates what JavaScript can usually see from inside the browser).

var page = require('webpage').create();
page.open('http://github.com/', function () {
    page.render('github.png');
    phantom.exit();
});
like image 165
Aaron Digulla Avatar answered Nov 12 '22 11:11

Aaron Digulla