mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
Merge branch 'asset_server' of git://github.com/sdhull/capybara into sdhull-asset_server
Conflicts: lib/capybara.rb
This commit is contained in:
commit
d908bc1d99
5 changed files with 52 additions and 4 deletions
|
@ -15,7 +15,7 @@ module Capybara
|
|||
class InfiniteRedirectError < CapybaraError; end
|
||||
|
||||
class << self
|
||||
attr_accessor :asset_root, :app_host, :run_server, :default_host, :always_include_port
|
||||
attr_accessor :asset_host, :app_host, :run_server, :default_host, :always_include_port
|
||||
attr_accessor :server_host, :server_port, :exact, :match, :exact_options, :visible_text_only
|
||||
attr_accessor :default_selector, :default_wait_time, :ignore_hidden_elements
|
||||
attr_accessor :save_and_open_page_path, :automatic_reload, :raise_server_errors
|
||||
|
@ -33,9 +33,10 @@ module Capybara
|
|||
#
|
||||
# === Configurable options
|
||||
#
|
||||
# [asset_root = String] Where static assets are located, used by save_and_open_page
|
||||
# [asset_host = String] The hostname for a server from which assets can be loaded, used by save_and_open_page
|
||||
# [app_host = String] The default host to use when giving a relative URL to visit
|
||||
# [always_include_port = Boolean] Whether the Rack server's port should automatically be inserted into every visited URL (Default: false)
|
||||
# [asset_host = String] Where dynamic assets are hosted - will be prepended to relative asset locations if present (Default: nil)
|
||||
# [run_server = Boolean] Whether to start a Rack server for the given Rack app (Default: true)
|
||||
# [default_selector = :css/:xpath] Methods which take a selector use the given type by default (Default: CSS)
|
||||
# [default_wait_time = Integer] The number of seconds to wait for asynchronous processes to finish (Default: 2)
|
||||
|
|
|
@ -12,7 +12,6 @@ Capybara.app = Rack::Builder.new do
|
|||
end
|
||||
end.to_app
|
||||
|
||||
Capybara.asset_root = Rails.root.join('public')
|
||||
Capybara.save_and_open_page_path = Rails.root.join('tmp/capybara')
|
||||
|
||||
# Override default rack_test driver to respect data-method attributes.
|
||||
|
|
|
@ -313,10 +313,18 @@ module Capybara
|
|||
def save_page(path=nil)
|
||||
path ||= "capybara-#{Time.new.strftime("%Y%m%d%H%M%S")}#{rand(10**10)}.html"
|
||||
path = File.expand_path(path, Capybara.save_and_open_page_path) if Capybara.save_and_open_page_path
|
||||
body_to_write = body
|
||||
|
||||
if Capybara.asset_host
|
||||
parsed = Nokogiri::HTML(body_to_write)
|
||||
if parsed.css("base").empty? && match = body_to_write.match(/<head[^<]*?>/)
|
||||
body_to_write.insert match.end(0), "<base href='#{Capybara.asset_host}' />"
|
||||
end
|
||||
end
|
||||
|
||||
FileUtils.mkdir_p(File.dirname(path))
|
||||
|
||||
File.open(path,'w') { |f| f.write(body) }
|
||||
File.open(path,'w') { |f| f.write(body_to_write) }
|
||||
path
|
||||
end
|
||||
|
||||
|
|
|
@ -43,4 +43,34 @@ Capybara::SpecHelper.spec '#save_page' do
|
|||
filename = path.split("/").last
|
||||
result.should == filename
|
||||
end
|
||||
|
||||
context "asset_host contains a string" do
|
||||
before { Capybara.asset_host = "http://example.com" }
|
||||
after { Capybara.asset_host = nil }
|
||||
|
||||
it "prepends base tag with value from asset_host to the head" do
|
||||
@session.visit("/with_js")
|
||||
path = @session.save_page
|
||||
|
||||
result = File.read(path)
|
||||
result.should include("<head><base href='http://example.com' />")
|
||||
end
|
||||
|
||||
it "doesn't prepend base tag to pages when asset_host is nil" do
|
||||
Capybara.asset_host = nil
|
||||
@session.visit("/with_js")
|
||||
path = @session.save_page
|
||||
|
||||
result = File.read(path)
|
||||
result.should_not include("http://example.com")
|
||||
end
|
||||
|
||||
it "doesn't prepend base tag to pages which already have it" do
|
||||
@session.visit("/with_base_tag")
|
||||
path = @session.save_page
|
||||
|
||||
result = File.read(path)
|
||||
result.should_not include("http://example.com")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
10
lib/capybara/spec/views/with_base_tag.erb
Normal file
10
lib/capybara/spec/views/with_base_tag.erb
Normal file
|
@ -0,0 +1,10 @@
|
|||
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
|
||||
<head>
|
||||
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
|
||||
<base href="http://example2.com" />
|
||||
<title>with_external_source</title>
|
||||
</head>
|
||||
<body id="with_js">
|
||||
<h1>FooBar</h1>
|
||||
</body>
|
||||
</html
|
Loading…
Reference in a new issue