basic screenshot support

This commit is contained in:
John Bintz 2012-07-25 13:57:43 -04:00
parent dfb814cd50
commit 00324b0538
2 changed files with 70 additions and 0 deletions

View File

@ -1,6 +1,7 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
require 'thor' require 'thor'
require 'builder'
module CukePack module CukePack
class CLI < Thor class CLI < Thor
@ -33,9 +34,56 @@ end
RB RB
end 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 default_task :install
end end
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 CukePack::CLI.start

View File

@ -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