mirror of
https://github.com/thoughtbot/capybara-webkit
synced 2023-03-27 23:22:28 -04:00
Improve pattern of testing driver against a rack app
This commit is contained in:
parent
259a38f206
commit
265a7da652
4 changed files with 435 additions and 437 deletions
|
@ -3,14 +3,10 @@ require 'capybara/webkit/driver'
|
||||||
require 'mini_magick'
|
require 'mini_magick'
|
||||||
|
|
||||||
describe Capybara::Webkit::Driver, "rendering an image" do
|
describe Capybara::Webkit::Driver, "rendering an image" do
|
||||||
|
include AppRunner
|
||||||
|
|
||||||
before(:all) do
|
let(:driver) do
|
||||||
# Set up the tmp directory and file name
|
driver_for_app do |env|
|
||||||
tmp_dir = File.join(PROJECT_ROOT, 'tmp')
|
|
||||||
FileUtils.mkdir_p tmp_dir
|
|
||||||
@file_name = File.join(tmp_dir, 'render-test.png')
|
|
||||||
|
|
||||||
app = lambda do |env|
|
|
||||||
body = <<-HTML
|
body = <<-HTML
|
||||||
<html>
|
<html>
|
||||||
<body>
|
<body>
|
||||||
|
@ -22,22 +18,25 @@ describe Capybara::Webkit::Driver, "rendering an image" do
|
||||||
{ 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s },
|
{ 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s },
|
||||||
[body]]
|
[body]]
|
||||||
end
|
end
|
||||||
|
|
||||||
@driver = Capybara::Webkit::Driver.new(app, :browser => $webkit_browser)
|
|
||||||
@driver.visit("/hello/world?success=true")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
after(:all) { @driver.reset! }
|
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')
|
||||||
|
driver.visit '/'
|
||||||
|
end
|
||||||
|
|
||||||
def render(options)
|
def render(options)
|
||||||
FileUtils.rm_f @file_name
|
FileUtils.rm_f @file_name
|
||||||
@driver.render @file_name, options
|
driver.render @file_name, options
|
||||||
|
|
||||||
@image = MiniMagick::Image.open @file_name
|
@image = MiniMagick::Image.open @file_name
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with default options" do
|
context "with default options" do
|
||||||
before(:all){ render({}) }
|
before { render({}) }
|
||||||
|
|
||||||
it "should be a PNG" do
|
it "should be a PNG" do
|
||||||
@image[:format].should == "PNG"
|
@image[:format].should == "PNG"
|
||||||
|
@ -54,7 +53,7 @@ describe Capybara::Webkit::Driver, "rendering an image" do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with dimensions set larger than necessary" do
|
context "with dimensions set larger than necessary" do
|
||||||
before(:all){ render(:width => 500, :height => 400) }
|
before { render(:width => 500, :height => 400) }
|
||||||
|
|
||||||
it "width should match the width given" do
|
it "width should match the width given" do
|
||||||
@image[:width].should == 500
|
@image[:width].should == 500
|
||||||
|
@ -66,7 +65,7 @@ describe Capybara::Webkit::Driver, "rendering an image" do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with dimensions set smaller than the document's default" do
|
context "with dimensions set smaller than the document's default" do
|
||||||
before(:all){ render(:width => 50, :height => 10) }
|
before { render(:width => 50, :height => 10) }
|
||||||
|
|
||||||
it "width should be greater than the width given" do
|
it "width should be greater than the width given" do
|
||||||
@image[:width].should > 50
|
@image[:width].should > 50
|
||||||
|
@ -76,5 +75,4 @@ describe Capybara::Webkit::Driver, "rendering an image" do
|
||||||
@image[:height].should > 10
|
@image[:height].should > 10
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,6 +1,7 @@
|
||||||
require 'rspec'
|
require 'rspec'
|
||||||
require 'rspec/autorun'
|
require 'rspec/autorun'
|
||||||
require 'rbconfig'
|
require 'rbconfig'
|
||||||
|
require 'capybara'
|
||||||
|
|
||||||
PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')).freeze
|
PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')).freeze
|
||||||
|
|
||||||
|
@ -20,12 +21,16 @@ RSpec.configure do |c|
|
||||||
c.filter_run_excluding :skip_on_windows => !(RbConfig::CONFIG['host_os'] =~ /mingw32/).nil?
|
c.filter_run_excluding :skip_on_windows => !(RbConfig::CONFIG['host_os'] =~ /mingw32/).nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
require File.join(spec_dir, "spec_helper")
|
|
||||||
|
|
||||||
require 'capybara/webkit'
|
require 'capybara/webkit'
|
||||||
connection = Capybara::Webkit::Connection.new(:socket_class => TCPSocket, :stdout => nil)
|
connection = Capybara::Webkit::Connection.new(:socket_class => TCPSocket, :stdout => nil)
|
||||||
$webkit_browser = Capybara::Webkit::Browser.new(connection)
|
$webkit_browser = Capybara::Webkit::Browser.new(connection)
|
||||||
|
|
||||||
|
RSpec.configure do |config|
|
||||||
|
config.before { $webkit_browser.reset! }
|
||||||
|
end
|
||||||
|
|
||||||
|
require File.join(spec_dir, "spec_helper")
|
||||||
|
|
||||||
Capybara.register_driver :reusable_webkit do |app|
|
Capybara.register_driver :reusable_webkit do |app|
|
||||||
Capybara::Webkit::Driver.new(app, :browser => $webkit_browser)
|
Capybara::Webkit::Driver.new(app, :browser => $webkit_browser)
|
||||||
end
|
end
|
||||||
|
|
49
spec/support/app_runner.rb
Normal file
49
spec/support/app_runner.rb
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
# Boots a single Capybara::Server for a Rack application that delegates to another, singleton Rack
|
||||||
|
# application that can be configured for each spec. Also configures Capybara to use that server.
|
||||||
|
|
||||||
|
module AppRunner
|
||||||
|
class << self
|
||||||
|
attr_accessor :app, :app_host
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.boot
|
||||||
|
app_container = lambda { |env| AppRunner.app.call(env) }
|
||||||
|
server = Capybara::Server.new(app_container)
|
||||||
|
server.boot
|
||||||
|
self.app_host = "http://127.0.0.1:#{server.port}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.reset
|
||||||
|
self.app = lambda do |env|
|
||||||
|
[200, { 'Content-Type' => 'html', 'Content-Length' => 0 }, []]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def run_application(&app)
|
||||||
|
AppRunner.app = app
|
||||||
|
end
|
||||||
|
|
||||||
|
def driver_for_app(&app)
|
||||||
|
run_application(&app)
|
||||||
|
Capybara::Webkit::Driver.new(app, :browser => $webkit_browser)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.included(example_group)
|
||||||
|
example_group.class_eval do
|
||||||
|
before { AppRunner.reset }
|
||||||
|
|
||||||
|
around do |example|
|
||||||
|
Capybara.run_server = false
|
||||||
|
Capybara.app_host = AppRunner.app_host
|
||||||
|
begin
|
||||||
|
example.run
|
||||||
|
ensure
|
||||||
|
Capybara.run_server = true
|
||||||
|
Capybara.app_host = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
AppRunner.boot
|
Loading…
Reference in a new issue