basic screenshot support
This commit is contained in:
parent
dfb814cd50
commit
00324b0538
@ -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
|
||||
|
||||
|
22
lib/cuke-pack/support/screenshots.rb
Normal file
22
lib/cuke-pack/support/screenshots.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user