From 8df6ed16ec692f1e3c19a3cbd5e9ba128cafaf8e Mon Sep 17 00:00:00 2001 From: molfar Date: Wed, 6 Feb 2013 21:36:55 +0200 Subject: [PATCH] Add #title method to Capybara::Node::Document --- lib/capybara/node/document.rb | 4 ++++ lib/capybara/rack_test/browser.rb | 4 ++++ lib/capybara/rack_test/driver.rb | 4 ++++ lib/capybara/selenium/driver.rb | 4 ++++ lib/capybara/session.rb | 10 +++++++++- lib/capybara/spec/session/title_spec.rb | 16 ++++++++++++++++ lib/capybara/spec/views/with_title.erb | 1 + 7 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 lib/capybara/spec/session/title_spec.rb create mode 100644 lib/capybara/spec/views/with_title.erb diff --git a/lib/capybara/node/document.rb b/lib/capybara/node/document.rb index f72e43ab..6e902362 100644 --- a/lib/capybara/node/document.rb +++ b/lib/capybara/node/document.rb @@ -20,6 +20,10 @@ module Capybara def text find(:xpath, '/html').text end + + def title + session.driver.title + end end end end diff --git a/lib/capybara/rack_test/browser.rb b/lib/capybara/rack_test/browser.rb index e52decf0..53510e40 100644 --- a/lib/capybara/rack_test/browser.rb +++ b/lib/capybara/rack_test/browser.rb @@ -89,6 +89,10 @@ class Capybara::RackTest::Browser rescue Rack::Test::Error "" end + + def title + dom.xpath("//title").text + end protected diff --git a/lib/capybara/rack_test/driver.rb b/lib/capybara/rack_test/driver.rb index 62b0bb6d..7dc84120 100644 --- a/lib/capybara/rack_test/driver.rb +++ b/lib/capybara/rack_test/driver.rb @@ -73,6 +73,10 @@ class Capybara::RackTest::Driver < Capybara::Driver::Base def dom browser.dom end + + def title + browser.title + end def reset! @browser = nil diff --git a/lib/capybara/selenium/driver.rb b/lib/capybara/selenium/driver.rb index 840c2b16..a620bbf6 100644 --- a/lib/capybara/selenium/driver.rb +++ b/lib/capybara/selenium/driver.rb @@ -38,6 +38,10 @@ class Capybara::Selenium::Driver < Capybara::Driver::Base browser.page_source end + def title + browser.title + end + def current_url browser.current_url end diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index e48d6eef..1dbaa57c 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -37,7 +37,7 @@ module Capybara :has_no_unchecked_field?, :query, :assert_selector, :assert_no_selector ] SESSION_METHODS = [ - :body, :html, :current_url, :current_host, :evaluate_script, :source, + :body, :html, :current_url, :current_host, :evaluate_script, :source, :title, :visit, :within, :within_fieldset, :within_table, :within_frame, :within_window, :current_path, :save_page, :save_and_open_page, :save_screenshot, :reset_session!, :response_headers, :status_code @@ -136,6 +136,14 @@ module Capybara def current_url driver.current_url end + + ## + # + # @return [String] Title of the current page + # + def title + driver.title + end ## # diff --git a/lib/capybara/spec/session/title_spec.rb b/lib/capybara/spec/session/title_spec.rb new file mode 100644 index 00000000..0da86bc5 --- /dev/null +++ b/lib/capybara/spec/session/title_spec.rb @@ -0,0 +1,16 @@ +Capybara::SpecHelper.spec '#title' do + + it "should print the title of the page" do + @session.visit('/with_title') + @session.title.should == 'Test Title' + end + + context "with css as default selector" do + before { Capybara.default_selector = :css } + it "should print the title of the page" do + @session.visit('/with_title') + @session.title.should == 'Test Title' + end + after { Capybara.default_selector = :xpath } + end +end \ No newline at end of file diff --git a/lib/capybara/spec/views/with_title.erb b/lib/capybara/spec/views/with_title.erb new file mode 100644 index 00000000..6b64b921 --- /dev/null +++ b/lib/capybara/spec/views/with_title.erb @@ -0,0 +1 @@ +Test Title \ No newline at end of file