From 6f2e8c547b6d27678eefc407976ef7296bec4627 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 11 Mar 2011 17:01:13 +0100 Subject: [PATCH] Added current_host method --- lib/capybara/session.rb | 12 ++++++++++-- lib/capybara/spec/session.rb | 1 + lib/capybara/spec/session/current_host_spec.rb | 9 +++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 lib/capybara/spec/session/current_host_spec.rb diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index f554b8a4..300d6dbc 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -27,8 +27,8 @@ module Capybara # class Session DSL_METHODS = [ - :all, :first, :attach_file, :body, :html, :text, :check, :choose, :click_link_or_button, :click_button, :click_link, :current_url, :drag, :evaluate_script, - :field_labeled, :fill_in, :find, :find_button, :find_by_id, :find_field, :find_link, :has_content?, :has_css?, + :all, :first, :attach_file, :body, :html, :text, :check, :choose, :click_link_or_button, :click_button, :click_link, :current_url, :current_host, + :drag, :evaluate_script, :field_labeled, :fill_in, :find, :find_button, :find_by_id, :find_field, :find_link, :has_content?, :has_css?, :has_no_content?, :has_no_css?, :has_no_xpath?, :has_xpath?, :locate, :save_page, :save_and_open_page, :select, :source, :uncheck, :visit, :wait_until, :within, :within_fieldset, :within_table, :within_frame, :within_window, :has_link?, :has_no_link?, :has_button?, :has_no_button?, :has_field?, :has_no_field?, :has_checked_field?, :has_unchecked_field?, :has_no_table?, :has_table?, @@ -107,6 +107,14 @@ module Capybara URI.parse(current_url).path end + ## + # + # @return [String] Host of the current page + # + def current_host + URI.parse(current_url).host + end + ## # # @return [String] Fully qualified URL of the current page diff --git a/lib/capybara/spec/session.rb b/lib/capybara/spec/session.rb index cd07b7cd..995896fe 100644 --- a/lib/capybara/spec/session.rb +++ b/lib/capybara/spec/session.rb @@ -81,6 +81,7 @@ shared_examples_for "session" do it_should_behave_like "unselect" it_should_behave_like "within" it_should_behave_like "current_url" + it_should_behave_like "current_host" it "should encode complex field names, like array[][value]" do @session.visit('/form') diff --git a/lib/capybara/spec/session/current_host_spec.rb b/lib/capybara/spec/session/current_host_spec.rb new file mode 100644 index 00000000..3f19a81a --- /dev/null +++ b/lib/capybara/spec/session/current_host_spec.rb @@ -0,0 +1,9 @@ +shared_examples_for "current_host" do + describe '#current_host' do + it "is affected by visiting a page directly" do + @session.visit('http://capybara-testapp.heroku.com/') + @session.body.should include('Hello world') + @session.current_host.should == 'capybara-testapp.heroku.com' + end + end +end