From e0591fef5c9b4de9d56f71608043c86aed6a8fa4 Mon Sep 17 00:00:00 2001 From: Steve Hull Date: Sat, 9 Feb 2013 01:33:49 -0800 Subject: [PATCH 1/2] Adding asset server support with base tag approach --- lib/capybara.rb | 2 ++ lib/capybara/session.rb | 10 +++++++++- lib/capybara/spec/session/save_page_spec.rb | 21 +++++++++++++++++++++ lib/capybara/spec/views/with_base_tag.erb | 10 ++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 lib/capybara/spec/views/with_base_tag.erb diff --git a/lib/capybara.rb b/lib/capybara.rb index fe4638fb..232b372e 100644 --- a/lib/capybara.rb +++ b/lib/capybara.rb @@ -16,6 +16,7 @@ module Capybara class << self attr_accessor :asset_root, :app_host, :run_server, :default_host, :always_include_port + attr_accessor :asset_host attr_accessor :server_host, :server_port attr_accessor :default_selector, :default_wait_time, :ignore_hidden_elements attr_accessor :save_and_open_page_path, :automatic_reload @@ -36,6 +37,7 @@ module Capybara # [asset_root = String] Where static assets are located, 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) diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index e48d6eef..c8242d50 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -304,10 +304,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(//) + body_to_write.insert match.end(0), "" + 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 diff --git a/lib/capybara/spec/session/save_page_spec.rb b/lib/capybara/spec/session/save_page_spec.rb index c9e894d3..85ee1b35 100644 --- a/lib/capybara/spec/session/save_page_spec.rb +++ b/lib/capybara/spec/session/save_page_spec.rb @@ -43,4 +43,25 @@ 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("") + 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 diff --git a/lib/capybara/spec/views/with_base_tag.erb b/lib/capybara/spec/views/with_base_tag.erb new file mode 100644 index 00000000..5b568fd3 --- /dev/null +++ b/lib/capybara/spec/views/with_base_tag.erb @@ -0,0 +1,10 @@ + + + + + with_external_source + + +

FooBar

+ + Date: Fri, 15 Feb 2013 11:16:00 -0800 Subject: [PATCH 2/2] Added a spec for default case --- lib/capybara/spec/session/save_page_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/capybara/spec/session/save_page_spec.rb b/lib/capybara/spec/session/save_page_spec.rb index 85ee1b35..2ee2544d 100644 --- a/lib/capybara/spec/session/save_page_spec.rb +++ b/lib/capybara/spec/session/save_page_spec.rb @@ -56,6 +56,15 @@ Capybara::SpecHelper.spec '#save_page' do result.should include("") 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