Create WebPage Screenshots with Node.js and SlimerJS

By  on  
SlimerJS

Last week I featured PhantomJS, a headless WebKit tool, which allows for taking screenshots, automating events on the page, and so on.  PhantomJS is an excellent tool that does so much but being locked into the WebKit engine doesn't help if you want to test other rendering engines like Firefox.  Mo engines, mo problems.

Luckily SlimerJS exists.  SlimerJS is very much like PhantomJS:  a promise-based automation system that uses Firefox's Gecko rendering engine instead of WebKit.  Let's have a look at how to automate screenshot creation using SlimerJS!

Creating a Screenshot

SlimerJS is Node.js-based so you'll write your screenshot snapping code with JavaScript:

var webpage = require('webpage').create();
webpage
  .open('https://davidwalsh.name')
  .then(function(){
    webpage.render('dwb.png', { onlyViewport: true });
    slimer.exit()
  });

With your script written you'll execute:

slimerjs take-screenshot.js

SlimerJS has its own command line tool which you'll get during install.

Setting Viewport Size

SlimerJS, much like PhatomJS, allows you to set the viewport so you can take screenshots at any size:

var webpage = require('webpage').create();
webpage
    .open('https://davidwalsh.name')
    .then(function(){
      webpage.viewportSize = { width: 1042, height: 768 };
      webpage.render('dwb.png', { onlyViewport: true });
      slimer.exit()
    });

... which is important for mobile Firefox testing as well.  I recommend setting up a script to take screenshots at all popular sizes when you hand designs over to a client or simply want to check your site's integrity!

Recent Features

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

  • By
    CSS Animations Between Media Queries

    CSS animations are right up there with sliced bread. CSS animations are efficient because they can be hardware accelerated, they require no JavaScript overhead, and they are composed of very little CSS code. Quite often we add CSS transforms to elements via CSS during...

Incredible Demos

  • By
    Telephone Link Protocol

    We've always been able to create links with protocols other than the usual HTTP, like mailto, skype, irc ,and more;  they're an excellent convenience to visitors.  With mobile phone browsers having become infinitely more usable, we can now extend that convenience to phone numbers: The tel...

  • By
    MooTools Text Flipping

    There are lots and lots of useless but fun JavaScript techniques out there. This is another one of them. One popular April Fools joke I quickly got tired of was websites transforming their text upside down. I found a jQuery Plugin by Paul...

Discussion

  1. Kevin Grandon

    Hey David!

    Curious as to your thoughts on Slimer/Phantom APIs wrapped with async functions. For long running test scripts I think it makes the API easier to reason about. Plug for a project I’ve been working on: https://github.com/kevingrandon/ghostjs

  2. affan

    Hey David,

    I am using slimerjs to take a screenshot, I am getting screen shot but its taking almost 20 secfor screenshot, what we can do to reduce time. I have install mozilla on server

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!