1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00
teamcapybara--capybara/spec/save_and_open_page_spec.rb

160 lines
4.8 KiB
Ruby
Raw Normal View History

require 'spec_helper'
2009-11-22 12:12:40 -05:00
2010-07-09 21:06:44 -04:00
require 'capybara/util/save_and_open_page'
2009-11-22 12:12:40 -05:00
require 'launchy'
2010-07-09 21:06:44 -04:00
describe Capybara do
describe ".save_and_open_page" do
2009-11-22 12:12:40 -05:00
before do
@time = Time.new.strftime("%Y%m%d%H%M%S")
@temp_file = mock("FILE")
2009-11-22 12:12:40 -05:00
@temp_file.stub!(:write)
@temp_file.stub!(:close)
2010-09-15 12:19:50 -04:00
2009-11-22 12:12:40 -05:00
@html = <<-HTML
<html>
<head>
<script type="text/javascript" src="/javascripts/prototype.js?123"/>
2009-11-22 12:12:40 -05:00
</head>
<body>
<h1>test</h1>
<p>
Some images (note differing whitespace closing tag):
<img src="/images/image1.jpeg" />
<img src="/images/image2.jpeg"/>
</p>
<p>
Some more in a non-existent directory:
<img src="/img/image3.jpeg" />
<img src="/img/image4.jpeg"/>
</p>
<p>
<a href="/not-here/foo.html">
A link to a file in a non-existent directory.
</a>
</p>
2009-11-22 12:12:40 -05:00
</body>
<html>
HTML
Launchy::Browser.stub(:run)
end
def default_file_expectations
@name = "capybara-#{@time}.html"
2010-09-15 12:19:50 -04:00
@temp_file.stub!(:path).and_return(@name)
File.should_receive(:exist?).and_return true
File.should_receive(:new).and_return @temp_file
end
describe "defaults" do
before do
default_file_expectations
end
2010-09-15 12:19:50 -04:00
it "should create a new temporary file" do
@temp_file.should_receive(:write).with @html
2010-07-09 21:06:44 -04:00
Capybara.save_and_open_page @html
end
it "should open the file in the browser" do
2010-07-09 21:06:44 -04:00
Capybara.should_receive(:open_in_browser).with(@name)
Capybara.save_and_open_page @html
end
2009-11-22 12:12:40 -05:00
end
2010-09-15 12:19:50 -04:00
describe "custom output path" do
before do
@custom_path = File.join('tmp', 'capybara')
@custom_name = File.join(@custom_path, "capybara-#{@time}.html")
2009-11-22 12:12:40 -05:00
@temp_file.stub!(:path).and_return(@custom_name)
2010-09-15 12:19:50 -04:00
Capybara.should_receive(:save_and_open_page_path).at_least(:once).and_return(@custom_path)
end
2010-09-15 12:19:50 -04:00
it "should create a new temporary file in the custom path" do
File.should_receive(:directory?).and_return true
File.should_receive(:exist?).and_return true
File.should_receive(:new).and_return @temp_file
2010-09-15 12:19:50 -04:00
@temp_file.should_receive(:write).with @html
2010-07-09 21:06:44 -04:00
Capybara.save_and_open_page @html
end
2010-09-15 12:19:50 -04:00
it "should open the file - in the custom path - in the browser" do
2010-07-09 21:06:44 -04:00
Capybara.should_receive(:open_in_browser).with(@custom_name)
Capybara.save_and_open_page @html
end
2010-09-15 12:19:50 -04:00
it "should be possible to configure output path" do
Capybara.should respond_to(:save_and_open_page_path)
default_setting = Capybara.save_and_open_page_path
lambda {
Capybara.save_and_open_page_path = File.join('tmp', 'capybara')
Capybara.save_and_open_page_path.should == File.join('tmp', 'capybara')
}.should_not raise_error
Capybara.save_and_open_page_path = default_setting
end
2009-11-22 12:12:40 -05:00
end
describe "rewrite_css_and_image_references" do
before do
default_file_expectations
@asset_root_dir = "/path/to/rails/public"
end
def mock_asset_root_with(directories)
@asset_root = Pathname.new(@asset_root_dir)
Capybara.should_receive(:asset_root).and_return @asset_root
dir = mock('asset_root mock dir')
Dir.should_receive(:new).with(@asset_root).and_return dir
dirents = [ '.', '..', 'file.html' ] + directories
dir.should_receive(:entries).and_return dirents
directories_regexp = directories.join('|')
FileTest.should_receive(:directory?) \
.at_least(dirents.size - 2).times \
.and_return { |dir|
dir =~ %r!#{@asset_root_dir}/(#{directories_regexp})$!
}
end
def expected_html_for_asset_root_with(directories)
mock_asset_root_with(directories)
expected_html = @html.clone
if not directories.empty?
directories_regexp = directories.join('|')
expected_html.gsub!(/"(\/(#{directories_regexp})\/)/,
'"%s\1' % @asset_root_dir)
end
return expected_html
end
def test_with_directories(directories)
@temp_file.should_receive(:write) \
.with expected_html_for_asset_root_with(directories)
Capybara.save_and_open_page @html
end
context "asset_root contains some directories" do
it "should rewrite relative paths to absolute local paths" do
test_with_directories([ 'javascripts', 'images' ])
end
end
context "asset_root path contains no directories" do
it "should not rewrite any relative paths" do
test_with_directories([ ])
end
end
end
2009-11-22 12:12:40 -05:00
end
end