diff --git a/lib/capybara/node/actions.rb b/lib/capybara/node/actions.rb index 29192469..affaa18f 100644 --- a/lib/capybara/node/actions.rb +++ b/lib/capybara/node/actions.rb @@ -3,53 +3,53 @@ module Capybara module Actions def click_link_or_button(locator) msg = "no link or button '#{locator}' found" - locate(:xpath, XPath.link(locator).button(locator), msg).click + locate(:xpath, XPath.link(locator).button(locator), :message => msg).click end def click_link(locator) msg = "no link with title, id or text '#{locator}' found" - locate(:xpath, XPath.link(locator), msg).click + locate(:xpath, XPath.link(locator), :message => msg).click end def click_button(locator) msg = "no button with value or id or text '#{locator}' found" - locate(:xpath, XPath.button(locator), msg).click + locate(:xpath, XPath.button(locator), :message => msg).click end def fill_in(locator, options={}) msg = "cannot fill in, no text field, text area or password field with id, name, or label '#{locator}' found" raise "Must pass a hash containing 'with'" if not options.is_a?(Hash) or not options.has_key?(:with) - locate(:xpath, XPath.fillable_field(locator), msg).set(options[:with]) + locate(:xpath, XPath.fillable_field(locator), :message => msg).set(options[:with]) end def choose(locator) msg = "cannot choose field, no radio button with id, name, or label '#{locator}' found" - locate(:xpath, XPath.radio_button(locator), msg).set(true) + locate(:xpath, XPath.radio_button(locator), :message => msg).set(true) end def check(locator) msg = "cannot check field, no checkbox with id, name, or label '#{locator}' found" - locate(:xpath, XPath.checkbox(locator), msg).set(true) + locate(:xpath, XPath.checkbox(locator), :message => msg).set(true) end def uncheck(locator) msg = "cannot uncheck field, no checkbox with id, name, or label '#{locator}' found" - locate(:xpath, XPath.checkbox(locator), msg).set(false) + locate(:xpath, XPath.checkbox(locator), :message => msg).set(false) end def select(value, options={}) msg = "cannot select option, no select box with id, name, or label '#{options[:from]}' found" - locate(:xpath, XPath.select(options[:from]), msg).select_option(value) + locate(:xpath, XPath.select(options[:from]), :message => msg).select_option(value) end def unselect(value, options={}) msg = "cannot unselect option, no select box with id, name, or label '#{options[:from]}' found" - locate(:xpath, XPath.select(options[:from]), msg).unselect_option(value) + locate(:xpath, XPath.select(options[:from]), :message => msg).unselect_option(value) end def attach_file(locator, path) msg = "cannot attach file, no file field with id, name, or label '#{locator}' found" - locate(:xpath, XPath.file_field(locator), msg).set(path) + locate(:xpath, XPath.file_field(locator), :message => msg).set(path) end def drag(source_locator, target_locator) diff --git a/lib/capybara/node/finders.rb b/lib/capybara/node/finders.rb index f49b6b13..c6f6efbf 100644 --- a/lib/capybara/node/finders.rb +++ b/lib/capybara/node/finders.rb @@ -2,10 +2,11 @@ module Capybara class Node module Finders #return node identified by locator or raise ElementNotFound(using desc) - def locate(kind_or_locator, locator=nil, fail_msg = nil) - node = wait_conditionally_until { find(kind_or_locator, locator) } + def locate(*args) + node = wait_conditionally_until { find(*args) } ensure - raise Capybara::ElementNotFound, fail_msg || "Unable to locate '#{locator || kind_or_locator}'" unless node + options = if args.last.is_a?(Hash) then args.last else {} end + raise Capybara::ElementNotFound, options[:message] || "Unable to locate '#{args[1] || args[0]}'" unless node return node end diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index 5fc5444b..7c7f7e7f 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -56,7 +56,7 @@ module Capybara end def within(kind, scope=nil) - new_scope = locate(kind, scope, "scope '#{scope || kind}' not found on page") + new_scope = locate(kind, scope, :message => "scope '#{scope || kind}' not found on page") begin scopes.push(new_scope) yield diff --git a/lib/capybara/spec/session/locate_spec.rb b/lib/capybara/spec/session/locate_spec.rb index c5608732..064dd063 100644 --- a/lib/capybara/spec/session/locate_spec.rb +++ b/lib/capybara/spec/session/locate_spec.rb @@ -9,6 +9,11 @@ shared_examples_for "locate" do @session.locate("//input[@id='test_field']")[:value].should == 'monkey' end + it "should find the first element using the given locator and options" do + @session.locate('//a', :text => 'Redirect')[:id].should == 'red' + @session.locate(:css, 'a', :text => 'A link')[:title].should == 'twas a fine link' + end + context "with css selectors" do it "should find the first element using the given locator" do @session.locate(:css, 'h1').text.should == 'This is a test' @@ -34,7 +39,7 @@ shared_examples_for "locate" do it "should raise ElementNotFound with specified fail message if nothing was found" do running do - @session.locate(:xpath, '//div[@id="nosuchthing"]', 'arghh').should be_nil + @session.locate(:xpath, '//div[@id="nosuchthing"]', :message => 'arghh').should be_nil end.should raise_error(Capybara::ElementNotFound, "arghh") end