From 00324b053846fcf2dc774ba5cc139c166a209719 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 25 Jul 2012 13:57:43 -0400 Subject: [PATCH] basic screenshot support --- bin/cuke-pack | 48 ++++++++++++++++++++++++++++ lib/cuke-pack/support/screenshots.rb | 22 +++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 lib/cuke-pack/support/screenshots.rb diff --git a/bin/cuke-pack b/bin/cuke-pack index ebf0e10..320c93d 100755 --- a/bin/cuke-pack +++ b/bin/cuke-pack @@ -1,6 +1,7 @@ #!/usr/bin/env ruby require 'thor' +require 'builder' module CukePack class CLI < Thor @@ -33,9 +34,56 @@ end RB end + desc "screenshots DIR", "Start a screenshot server" + method_options %w{port -p} => 4432 + def screenshots(dir = "features/screenshots") + require 'rack' + + Rack::Handler.default.run(ScreenshotsApp.new(dir), :Port => options[:port]) + end + default_task :install end end +class ScreenshotsApp + def initialize(dir) + @dir = dir + end + + def call(env) + by_file = {} + + Dir[@dir + '/**/*.png'].each do |file| + file = file.gsub(%r{^#{@dir}/}, '') + + parts = file.split('/') + + browser = parts.shift + + name = parts.join('/') + + by_file[name] ||= [] + by_file[name] << browser + end + + output = Builder::XmlMarkup.new + + output.html { + output.head { + output.title("Screenshots") + } + + output.body { + by_file.each do |name, browsers| + + end + } + } + + [ 200, {}, [ output.to_s ] ] + end +end + CukePack::CLI.start diff --git a/lib/cuke-pack/support/screenshots.rb b/lib/cuke-pack/support/screenshots.rb new file mode 100644 index 0000000..af57ef8 --- /dev/null +++ b/lib/cuke-pack/support/screenshots.rb @@ -0,0 +1,22 @@ +module CukePack + class << self + attr_accessor :screenshot_options + end + + self.screenshot_options = { :width => 1280, :height => 1024, :prepend_driver_name => true, :directory => "features/screenshots" } +end + +def take_screenshot(name, options = {}) + options = CukePack.screenshot_options.merge(options) + + selenium = Capybara.current_session.driver.browser + selenium.manage.window.resize_to(options[:width], options[:height]) + + target = options[:directory] + target = File.join(target, Capybara.current_driver.to_s) + target = File.join(target, name + ".png") + + FileUtils.mkdir_p File.dirname(target) + + selenium.save_screenshot(target) +end