2011-05-26 15:05:16 -04:00
|
|
|
require 'spec_helper'
|
2012-07-08 15:17:08 -04:00
|
|
|
require 'capybara/webkit/driver'
|
2011-05-26 15:05:16 -04:00
|
|
|
require 'mini_magick'
|
|
|
|
|
2012-07-08 15:17:08 -04:00
|
|
|
describe Capybara::Webkit::Driver, "rendering an image" do
|
2012-07-08 16:38:19 -04:00
|
|
|
include AppRunner
|
2011-05-26 15:05:16 -04:00
|
|
|
|
2012-07-08 16:38:19 -04:00
|
|
|
let(:driver) do
|
2012-07-08 19:09:40 -04:00
|
|
|
driver_for_html(<<-HTML)
|
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
<h1>Hello World</h1>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
HTML
|
2011-05-26 15:05:16 -04:00
|
|
|
end
|
|
|
|
|
2012-07-08 16:38:19 -04:00
|
|
|
before(:each) do
|
|
|
|
# Set up the tmp directory and file name
|
|
|
|
tmp_dir = File.join(PROJECT_ROOT, 'tmp')
|
|
|
|
FileUtils.mkdir_p tmp_dir
|
|
|
|
@file_name = File.join(tmp_dir, 'render-test.png')
|
2012-11-18 10:23:53 -05:00
|
|
|
driver.visit("#{AppRunner.app_host}/")
|
2012-07-08 16:38:19 -04:00
|
|
|
end
|
2011-05-26 15:05:16 -04:00
|
|
|
|
|
|
|
def render(options)
|
|
|
|
FileUtils.rm_f @file_name
|
2012-11-17 10:33:31 -05:00
|
|
|
driver.save_screenshot @file_name, options
|
2011-05-26 15:05:16 -04:00
|
|
|
|
|
|
|
@image = MiniMagick::Image.open @file_name
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with default options" do
|
2012-07-08 16:38:19 -04:00
|
|
|
before { render({}) }
|
2011-05-26 15:05:16 -04:00
|
|
|
|
|
|
|
it "should be a PNG" do
|
2013-08-11 10:36:53 -04:00
|
|
|
@image[:format].should eq "PNG"
|
2011-05-26 15:05:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "width default to 1000px (with 15px less for the scrollbar)" do
|
2011-07-05 11:33:30 -04:00
|
|
|
@image[:width].should be < 1001
|
|
|
|
@image[:width].should be > 1000-17
|
2011-05-26 15:05:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "height should be at least 10px" do
|
2013-01-13 17:19:46 -05:00
|
|
|
@image[:height].should be >= 10
|
2011-05-26 15:05:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with dimensions set larger than necessary" do
|
2012-07-08 16:38:19 -04:00
|
|
|
before { render(:width => 500, :height => 400) }
|
2011-05-26 15:05:16 -04:00
|
|
|
|
|
|
|
it "width should match the width given" do
|
2013-08-11 10:36:53 -04:00
|
|
|
@image[:width].should eq 500
|
2011-05-26 15:05:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "height should match the height given" do
|
2013-08-11 10:36:53 -04:00
|
|
|
@image[:height].should eq 400
|
2013-01-13 17:19:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should reset window dimensions to their default value" do
|
2013-08-11 10:36:53 -04:00
|
|
|
driver.evaluate_script('window.innerWidth').should eq 1680
|
|
|
|
driver.evaluate_script('window.innerHeight').should eq 1050
|
2011-05-26 15:05:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with dimensions set smaller than the document's default" do
|
2012-07-08 16:38:19 -04:00
|
|
|
before { render(:width => 50, :height => 10) }
|
2011-05-26 15:05:16 -04:00
|
|
|
|
|
|
|
it "width should be greater than the width given" do
|
2013-01-13 17:19:46 -05:00
|
|
|
@image[:width].should be > 50
|
2011-05-26 15:05:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "height should be greater than the height given" do
|
2013-01-13 17:19:46 -05:00
|
|
|
@image[:height].should be > 10
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should restore viewport dimensions after rendering" do
|
2013-08-11 10:36:53 -04:00
|
|
|
driver.evaluate_script('window.innerWidth').should eq 1680
|
|
|
|
driver.evaluate_script('window.innerHeight').should eq 1050
|
2013-01-13 17:19:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a custom viewport size" do
|
|
|
|
before { driver.resize_window(800, 600) }
|
|
|
|
|
|
|
|
it "should restore viewport dimensions after rendering" do
|
|
|
|
render({})
|
2013-08-11 10:36:53 -04:00
|
|
|
driver.evaluate_script('window.innerWidth').should eq 800
|
|
|
|
driver.evaluate_script('window.innerHeight').should eq 600
|
2011-05-26 15:05:16 -04:00
|
|
|
end
|
|
|
|
end
|
2014-02-06 01:36:00 -05:00
|
|
|
|
|
|
|
context "with invalid filepath" do
|
|
|
|
before do
|
|
|
|
@file_name = File.dirname(@file_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an InvalidResponseError" do
|
|
|
|
expect { render({}) }.to raise_error(Capybara::Webkit::InvalidResponseError)
|
|
|
|
end
|
|
|
|
end
|
2011-05-26 15:05:16 -04:00
|
|
|
end
|