screenshot snippets

Screenshot in wmii

Tagged wmii, screenshot, linux, imagemagick  Languages bash

Method 1

A simple script to create a screenshot in wmii. It probably works in other window managers too. I call it 'scrot'. Put it in the path and run it as you'd run any program in wmii. The "import" program comes with imagemagick.

import -window root /tmp/screenshot.png

Method 2

If you want timestamped screenshots then

apt-get install scrot
mkdir -p ~/screenshots

And create a script with the following content for making the screenshot.

scrot '%Y-%m-%d-%H-%M-%S_$wx$h_scrot.png' -e 'mv $f ~/screenshots'

How to capture screenshots with Selenium, Ruby and Firefox

Tagged ruby, selenium, screenshot  Languages ruby

Option #1 Selenium-WebDriver gem

First install the selenium-webdriver gem:

gem install selenium-webdriver

Then start using it:

require 'selenium-webdriver'
width = 1024
height = 728
driver = Selenium::WebDriver.for :firefox ''
driver.execute_script %Q{
  window.resizeTo(#{width}, #{height});


Option #2 Selenium Server and the Selenium Gem

I was using this option for quite some time, when it stopped working I started using the selenium-webdriver gem instead.

First create a separate Firefox profile:

firefox -ProfileManager -no-remote

Find the path to the profile by executing this code in the JavaScript error console in Firefox:

Components.classes[";1"].getService( Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile).path

The path looks somewhat like this on OSX:

/Users/christian/Library/Application Support/Firefox/Profiles/xxx

Now download the Selenium Server. Then start a Selenium server:

java -jar ~/Downloads/selenium-server-standalone-2.0b1.jar -firefoxProfileTemplate "/Users/christian/Library/Application Support/Firefox/Profiles/9eb0mn2d.screenshots/" -singleWindow

Next install the Selenium Ruby client:

gem install selenium-client

Take a screenshot with Ruby:

require 'rubygems'
require 'selenium'

@selenium ="localhost", 4444, "*firefox", "", 10000);
@selenium.start "/US/"
@selenium.send(:sleep, 7) # Wait for Flash and other crap to load
@selenium.capture_entire_page_screenshot('/tmp/screenshot.png', '');

Want more? Read the Selenium RC and Selenium client documentation

How to use PhantomJS to take screenshots

Tagged phantomjs, screenshot  Languages javascript
page = new WebPage()
if phantom.args.length < 2 or phantom.args.length > 3
  console.log "Usage: phantomjs URL filename"
  address = phantom.args[0]
  output = phantom.args[1]
  ua = "User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.101 Safari/537.11"
  page.settings.userAgent = ua
  page.customHeaders = "Accept-Language": "sv-SE,sv;q=0.8,en-US;q=0.6,en;q=0.4"
  page.viewportSize =
    width: 1024
    height: 760 address, (status) ->
    if status isnt "success"
      console.log "Unable to load the address!"
      window.setTimeout (->
        page.clipRect =
          top: 0
          left: 0
          width: 1024
          height: 760

        page.render output
        console.log "Exiting"
      ), 200

Usage: google.png