lib/capybara/node/finders.rb in capybara-2.7.1 vs lib/capybara/node/finders.rb in capybara-2.8.0

- old
+ new

@@ -31,88 +31,104 @@ def find(*args) query = Capybara::Queries::SelectorQuery.new(*args) synchronize(query.wait) do if query.match == :smart or query.match == :prefer_exact result = query.resolve_for(self, true) - result = query.resolve_for(self, false) if result.size == 0 && !query.exact? + result = query.resolve_for(self, false) if result.empty? && !query.exact? else result = query.resolve_for(self) end if query.match == :one or query.match == :smart and result.size > 1 raise Capybara::Ambiguous.new("Ambiguous match, found #{result.size} elements matching #{query.description}") end - if result.size == 0 + if result.empty? raise Capybara::ElementNotFound.new("Unable to find #{query.description}") end result.first end.tap(&:allow_reload!) end ## # # Find a form field on the page. The field can be found by its name, id or label text. # - # @macro waiting_behavior + # @overload find_field([locator], options={}) + # @param [String] locator name, id, placeholder or text of associated label element # - # @param [String] locator Which field to find + # @macro waiting_behavior # - # @option options [Boolean] checked Match checked field? - # @option options [Boolean] unchecked Match unchecked field? - # @option options [Boolean, Symbol] disabled (false) Match disabled field? - # * true - only finds a disabled field - # * false - only finds an enabled field - # * :all - finds either an enabled or disabled field - # @option options [Boolean] readonly Match readonly field? - # @option options [String] with Value of field to match on - # @option options [String] type Type of field to match on + # + # @option options [Boolean] checked Match checked field? + # @option options [Boolean] unchecked Match unchecked field? + # @option options [Boolean, Symbol] disabled (false) Match disabled field? + # * true - only finds a disabled field + # * false - only finds an enabled field + # * :all - finds either an enabled or disabled field + # @option options [Boolean] readonly Match readonly field? + # @option options [String, Regexp] with Value of field to match on + # @option options [String] type Type of field to match on + # @option options [Boolean] multiple Match fields that can have multiple values? + # @option options [String] id Match fields that match the id attribute + # @option options [String] name Match fields that match the name attribute + # @option options [String] placeholder Match fields that match the placeholder attribute # @return [Capybara::Node::Element] The found element # - def find_field(locator, options={}) + + def find_field(locator=nil, options={}) + locator, options = nil, locator if locator.is_a? Hash find(:field, locator, options) end alias_method :field_labeled, :find_field ## # # Find a link on the page. The link can be found by its id or text. # - # @macro waiting_behavior + # @overload find_link([locator], options={}) + # @param [String] locator id, title, text, or alt of enclosed img element # - # @param [String] locator Which link to find - # @option options [String,Regexp] href Value to match against the links href + # @macro waiting_behavior + # + # @option options [String,Regexp] href Value to match against the links href # @return [Capybara::Node::Element] The found element # - def find_link(locator, options={}) + def find_link(locator=nil, options={}) + locator, options = nil, locator if locator.is_a? Hash find(:link, locator, options) end ## # # Find a button on the page. # This can be any \<input> element of type submit, reset, image, button or it can be a # \<button> element. All buttons can be found by their id, value, or title. \<button> elements can also be found # by their text content, and image \<input> elements by their alt attribute - - # @macro waiting_behavior # - # @param [String] locator Which button to find - # @option options [Boolean, Symbol] disabled (false) Match disabled button? - # * true - only finds a disabled button - # * false - only finds an enabled button - # * :all - finds either an enabled or disabled button + # @overload find_button([locator], options={}) + # @param [String] locator id, value, title, text content, alt of image + # + # @overload find_button(options={}) + # + # @macro waiting_behavior + # + # @option options [Boolean, Symbol] disabled (false) Match disabled button? + # * true - only finds a disabled button + # * false - only finds an enabled button + # * :all - finds either an enabled or disabled button # @return [Capybara::Node::Element] The found element # - def find_button(locator, options={}) + def find_button(locator=nil, options={}) + locator, options = nil, locator if locator.is_a? Hash find(:button, locator, options) end ## # # Find a element on the page, given its id. # # @macro waiting_behavior # - # @param [String] id Which element to find + # @param [String] id id of element # # @return [Capybara::Node::Element] The found element # def find_by_id(id, options={}) find(:id, id, options)