Skip to content

Instantly share code, notes, and snippets.

@Attamusc
Created June 23, 2012 14:43
Show Gist options
  • Save Attamusc/2978540 to your computer and use it in GitHub Desktop.
Save Attamusc/2978540 to your computer and use it in GitHub Desktop.
Take a screenshot of any website, using PhantomJS.
# Simple little script to take a Screenshot of any website
#
# Usage: phantomjs screenshot.coffee http://google.com ./google.png
# I've always liked automated testing tools for front-end development like Selenium
# and Selenium Remote Control. One of the cool features was the ability to take a screenshot
# if the test failed. This was always useful as sometimes I could immediately see what went
# wrong and then fix it based on what I saw in the screenshot.
#
# I just started playing with PhantomJS and thought "I wonder if I could replicate that!"
#
# This is the result of that. Enjoy :)
args = require('system').args
page = new WebPage()
if (args.length isnt 3)
console.log "Usage: screenshot.coffee <URL> <FILENAME>"
phantom.exit 1
else
url = args[1]
filename = args[2]
page.open url, (status) ->
if (status isnt 'success')
console.log "ERROR: There was a problem loading the page"
phantom.exit 1
else
dimensions = page.evaluate(->
height: if document.body.offsetHeight > document.height then document.body.offsetHeight else document.height
width: if document.body.offsetWidth > document.width then document.body.offsetWidth else document.width
)
console.log "Screenshot: #{dimensions.width}px x #{dimensions.height}px"
page.viewportSize = dimensions
page.render filename
console.log "Saved in '#{filename}'"
phantom.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment