Skip to content

Instantly share code, notes, and snippets.

@hotchpotch
Forked from shin1ohno/gist:2841546
Last active December 10, 2015 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hotchpotch/4500182 to your computer and use it in GitHub Desktop.
Save hotchpotch/4500182 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'capybara-webkit' # '~> 0.14.0'
require 'optparse'
require 'cgi'
options = {}
OptionParser.new do |opt_parser|
opt_parser.on('-O filename') {|value| options[:filename] = value }
opt_parser.on('--width size') {|value| options[:width] = value.to_i }
opt_parser.on('--height size') {|value| options[:height] = value.to_i}
end.parse!
url = ARGV.shift || abort('web_capture.rb [--width n] [--height n] [-O filename] url')
filename = options.delete(:filename) || CGI.escape(url) + '.png'
if File.exist? filename
abort "#{filename} already exist."
end
puts "capture: #{url}"
driver = Capybara::Webkit::Driver.new(nil)
driver.visit(url)
driver.save_screenshot(filename, options)
puts "saved: #{filename}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment