1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Failure message gets second param

This commit is contained in:
Jonas Nicklas and Nicklas Ramhöj 2011-02-10 16:59:26 +01:00
parent f12ce00144
commit 764f022973
4 changed files with 38 additions and 21 deletions

View file

@ -31,8 +31,8 @@ module Capybara
unless node
options = extract_normalized_options(args)
normalized = Capybara::Selector.normalize(*args)
message = options[:message] || "Unable to find #{normalized[:selector].name} #{normalized[:locator].inspect}"
message = normalized[:selector].failure_message.call(self) if normalized[:selector].failure_message
message = options[:message] || "Unable to find #{normalized.name} #{normalized.locator.inspect}"
message = normalized.failure_message.call(self, normalized) if normalized.failure_message
raise Capybara::ElementNotFound, message
end
return node
@ -120,7 +120,7 @@ module Capybara
def all(*args)
options = extract_normalized_options(args)
Capybara::Selector.normalize(*args)[:xpaths].
Capybara::Selector.normalize(*args).xpaths.
map { |path| find_in_base(path) }.flatten.
select { |node| matches_options(node, options) }.
map { |node| convert_element(node) }
@ -143,7 +143,7 @@ module Capybara
options = extract_normalized_options(args)
found_elements = []
Capybara::Selector.normalize(*args)[:xpaths].each do |path|
Capybara::Selector.normalize(*args).xpaths.each do |path|
find_in_base(path).each do |node|
if matches_options(node, options)
found_elements << convert_element(node)

View file

@ -16,8 +16,8 @@ module Capybara
end
def failure_message_for_should
if normalized[:selector].failure_message
normalized[:selector].failure_message.call(@actual)
if normalized.failure_message
normalized.failure_message.call(@actual)
else
"expected #{selector_name} to return something"
end
@ -28,8 +28,8 @@ module Capybara
end
def selector_name
name = "#{normalized[:selector].name} #{normalized[:locator].inspect}"
name << " with text #{normalized[:options][:text].inspect}" if normalized[:options][:text]
name = "#{normalized.name} #{normalized.locator.inspect}"
name << " with text #{normalized.options[:text].inspect}" if normalized.options[:text]
name
end

View file

@ -2,6 +2,13 @@ module Capybara
class Selector
attr_reader :name
class Normalized
attr_accessor :selector, :locator, :options, :xpaths
def failure_message; selector.failure_message; end
def name; selector.name; end
end
class << self
def all
@selectors ||= {}
@ -16,25 +23,25 @@ module Capybara
end
def normalize(*args)
result = {}
result[:options] = if args.last.is_a?(Hash) then args.pop else {} end
normalized = Normalized.new
normalized.options = if args.last.is_a?(Hash) then args.pop else {} end
if args[1]
result[:selector] = all[args[0]]
result[:locator] = args[1]
normalized.selector = all[args[0]]
normalized.locator = args[1]
else
result[:selector] = all.values.find { |s| s.match?(args[0]) }
result[:locator] = args[0]
normalized.selector = all.values.find { |s| s.match?(args[0]) }
normalized.locator = args[0]
end
result[:selector] ||= all[Capybara.default_selector]
normalized.selector ||= all[Capybara.default_selector]
xpath = result[:selector].call(result[:locator])
xpath = normalized.selector.call(normalized.locator)
if xpath.respond_to?(:to_xpaths)
result[:xpaths] = xpath.to_xpaths
normalized.xpaths = xpath.to_xpaths
else
result[:xpaths] = [xpath.to_s].flatten
normalized.xpaths = [xpath.to_s].flatten
end
result
normalized
end
end

View file

@ -79,8 +79,8 @@ shared_examples_for "find" do
end
end
context "with custom selector with failure_message option", :focus => true do
it "it should raise an error with the failure message if the element is not found" do
context "with custom selector with failure_message option" do
it "should raise an error with the failure message if the element is not found" do
Capybara.add_selector(:monkey) do
xpath { |num| ".//*[contains(@id, 'monkey')][#{num}]" }
failure_message { |node| node.all(".//*[contains(@id, 'monkey')]").map { |node| node.text }.sort.join(', ') }
@ -89,6 +89,16 @@ shared_examples_for "find" do
@session.find(:monkey, '14').text.should == 'Monkey Paul'
end.should raise_error(Capybara::ElementNotFound, "Monkey John, Monkey Paul")
end
it "should pass the selector as the second argument" do
Capybara.add_selector(:monkey) do
xpath { |num| ".//*[contains(@id, 'monkey')][#{num}]" }
failure_message { |node, selector| selector.name.to_s + ': ' + selector.locator + ' - ' + node.all(".//*[contains(@id, 'monkey')]").map { |node| node.text }.sort.join(', ') }
end
running do
@session.find(:monkey, '14').text.should == 'Monkey Paul'
end.should raise_error(Capybara::ElementNotFound, "monkey: 14 - Monkey John, Monkey Paul")
end
end
context "with css as default selector" do