From 2722b4257debcc44909534e4cf4591ccbc40ca17 Mon Sep 17 00:00:00 2001 From: Lenny Marks Date: Tue, 29 Dec 2009 22:05:38 -0500 Subject: [PATCH] Fix and specs for find_by_id; Moved DSL_METHODS to session where they are added. --- lib/capybara/dsl.rb | 11 ++--------- lib/capybara/searchable.rb | 2 +- lib/capybara/session.rb | 9 +++++++++ spec/dsl/find_by_id_spec.rb | 18 ++++++++++++++++++ spec/session_spec.rb | 1 + 5 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 spec/dsl/find_by_id_spec.rb diff --git a/lib/capybara/dsl.rb b/lib/capybara/dsl.rb index 0c1cc398..7f2f453d 100644 --- a/lib/capybara/dsl.rb +++ b/lib/capybara/dsl.rb @@ -46,15 +46,8 @@ module Capybara Capybara.current_session end - SESSION_METHODS = [ - :visit, :current_url, :body, :click_link, :click_button, :drag, :fill_in, - :choose, :has_xpath?, :has_css?, :check, :uncheck, :attach_file, :select, - :has_content?, :within, :within_fieldset, :within_table, - :save_and_open_page, :find, :find_field, :find_link, :find_button, - :field_labeled, :all, :wait_for, :wait_for_condition, :evaluate_script, - :click, :wait_until - ] - SESSION_METHODS.each do |method| + + Session::DSL_METHODS.each do |method| class_eval <<-RUBY, __FILE__, __LINE__+1 def #{method}(*args, &block) page.#{method}(*args, &block) diff --git a/lib/capybara/searchable.rb b/lib/capybara/searchable.rb index 193b66f5..94b99d9a 100644 --- a/lib/capybara/searchable.rb +++ b/lib/capybara/searchable.rb @@ -36,7 +36,7 @@ module Capybara end def find_by_id(id) - find(Xpath.for_css("##{id}")) + find(XPath.for_css("##{id}")) end def all(locator, options = {}) diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index d79e5741..ce38e8a9 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -2,6 +2,15 @@ module Capybara class Session include Searchable + DSL_METHODS = [ + :visit, :current_url, :body, :click_link, :click_button, :drag, :fill_in, + :choose, :has_xpath?, :has_css?, :check, :uncheck, :attach_file, :select, + :has_content?, :within, :within_fieldset, :within_table, + :save_and_open_page, :find, :find_field, :find_link, :find_button, :find_by_id, + :field_labeled, :all, :wait_for, :wait_for_condition, :evaluate_script, + :click, :wait_until + ] + attr_reader :mode, :app def initialize(mode, app) diff --git a/spec/dsl/find_by_id_spec.rb b/spec/dsl/find_by_id_spec.rb new file mode 100644 index 00000000..722134a6 --- /dev/null +++ b/spec/dsl/find_by_id_spec.rb @@ -0,0 +1,18 @@ +module FindByIdSpec + shared_examples_for "find_by_id" do + describe '#find_by_id' do + before do + @session.visit('/with_html') + end + + it "should find any element by id" do + @session.find_by_id('red').tag_name.should == 'a' + @session.find_by_id('hidden_via_ancestor').tag_name.should == 'div' + end + + it "should return nil if no element with id is found" do + @session.find_by_id('nothing_with_this_id').should be_nil + end + end + end +end \ No newline at end of file diff --git a/spec/session_spec.rb b/spec/session_spec.rb index d63de5f2..efcae2a4 100644 --- a/spec/session_spec.rb +++ b/spec/session_spec.rb @@ -32,6 +32,7 @@ shared_examples_for "session" do it_should_behave_like "find_button" it_should_behave_like "find_field" it_should_behave_like "find_link" + it_should_behave_like "find_by_id" it_should_behave_like "find" it_should_behave_like "has_content" it_should_behave_like "has_css"