Only include node filters in error descriptions if at least one was applied

This commit is contained in:
Thomas Walpole 2018-07-12 12:32:41 -07:00
parent 1a5f290ee1
commit a483e8d4c5
26 changed files with 178 additions and 122 deletions

View File

@ -21,5 +21,5 @@ When(/^I use a matcher that fails$/) do
end
Then(/^the failing exception should be nice$/) do
expect(@error_message).to match(/expected to find visible css \"h1#doesnotexist\"/)
expect(@error_message).to match(/expected to find css \"h1#doesnotexist\"/)
end

View File

@ -290,8 +290,8 @@ module Capybara
result = query.resolve_for(self)
end
raise Capybara::Ambiguous, "Ambiguous match, found #{result.size} elements matching #{query.description}" if ambiguous?(query, result)
raise Capybara::ElementNotFound, "Unable to find #{query.description}" if result.empty?
raise Capybara::Ambiguous, "Ambiguous match, found #{result.size} elements matching #{query.applied_description}" if ambiguous?(query, result)
raise Capybara::ElementNotFound, "Unable to find #{query.applied_description}" if result.empty?
result.first
end.tap(&:allow_reload!)

View File

@ -12,7 +12,7 @@ module Capybara
end
end
def description
def description(applied = false)
child_query = @child_node&.instance_variable_get(:@query)
desc = super
desc += " that is an ancestor of #{child_query.description}" if child_query

View File

@ -30,22 +30,31 @@ module Capybara
def name; selector.name; end
def label; selector.label || selector.name; end
def description
def description(applied = false)
@description = +""
@description << "visible " if visible == :visible
@description << "non-visible " if visible == :hidden
if !applied || @applied_filters
@description << "visible " if visible == :visible
@description << "non-visible " if visible == :hidden
end
@description << "#{label} #{locator.inspect}"
@description << " with#{' exact' if exact_text == true} text #{options[:text].inspect}" if options[:text]
@description << " with exact text #{exact_text}" if exact_text.is_a?(String)
if !applied || @applied_filters
@description << " with#{' exact' if exact_text == true} text #{options[:text].inspect}" if options[:text]
@description << " with exact text #{exact_text}" if exact_text.is_a?(String)
end
@description << " with id #{options[:id]}" if options[:id]
@description << " with classes [#{Array(options[:class]).join(',')}]" if options[:class]
@description << selector.description(options)
@description << " that also matches the custom filter block" if @filter_block
@description << selector.description(skip_node_filters: applied && (@applied_filters != :node), **options)
@description << " that also matches the custom filter block" if @filter_block && (!applied || (@applied_filters == :node))
@description << " within #{@resolved_node.inspect}" if describe_within?
@description
end
def applied_description
description(true)
end
def matches_filters?(node)
@applied_filters ||= :system
return false if options[:text] && !matches_text_filter(node, options[:text])
return false if exact_text.is_a?(String) && !matches_exact_text_filter(node, exact_text)
@ -54,6 +63,7 @@ module Capybara
when :hidden then return false if node.visible?
end
@applied_filters = :node
matches_node_filters?(node) && matches_filter_block?(node)
rescue *(node.respond_to?(:session) ? node.session.driver.invalid_element_errors : [])
false
@ -88,6 +98,7 @@ module Capybara
# @api private
def resolve_for(node, exact = nil)
@applied_filters = false
@resolved_node = node
node.synchronize do
children = if selector.format == :css
@ -110,6 +121,14 @@ module Capybara
@expression.respond_to? :to_xpath
end
def failure_message
+"expected to find #{applied_description}" << count_message
end
def negative_failure_message
+"expected not to find #{applied_description}" << count_message
end
private
def find_selector(locator)

View File

@ -14,7 +14,7 @@ module Capybara
end
end
def description
def description(applied = false)
desc = super
sibling_query = @sibling_node&.instance_variable_get(:@query)
desc += " that is a sibling of #{sibling_query.description}" if sibling_query

View File

@ -138,6 +138,10 @@ module Capybara
failure_message.sub(/(to find)/, 'not \1')
end
def unfiltered_size
@elements.length
end
private
def full_results

View File

@ -10,7 +10,7 @@ Capybara::Selector::FilterSet.add(:_field) do
expression_filter(:name) { |xpath, val| xpath[XPath.attr(:name) == val] }
expression_filter(:placeholder) { |xpath, val| xpath[XPath.attr(:placeholder) == val] }
describe do |checked: nil, unchecked: nil, disabled: nil, multiple: nil, **_options|
describe(node_filters: true) do |checked: nil, unchecked: nil, disabled: nil, multiple: nil, **|
desc, states = +"", []
states << 'checked' if checked || (unchecked == false)
states << 'not checked' if unchecked || (checked == false)
@ -58,17 +58,21 @@ Capybara.add_selector(:field) do
node_filter(:with) do |node, with|
with.is_a?(Regexp) ? node.value =~ with : node.value == with.to_s
end
describe do |type: nil, **options|
desc = +""
(expression_filters.keys - [:type]).each { |ef| desc << " with #{ef} #{options[ef]}" if options.key?(ef) }
desc << " of type #{type.inspect}" if type
desc << " with value #{options[:with].to_s.inspect}" if options.key?(:with)
desc
end
describe_node_filters do |**options|
" with value #{options[:with].to_s.inspect}" if options.key?(:with)
end
end
Capybara.add_selector(:fieldset) do
xpath(:legend) do |locator, legend: nil, **_options|
xpath(:legend) do |locator, legend: nil, **|
locator_matchers = (XPath.attr(:id) == locator.to_s) | XPath.child(:legend)[XPath.string.n.is(locator.to_s)]
locator_matchers |= XPath.attr(Capybara.test_id) == locator if Capybara.test_id
xpath = XPath.descendant(:fieldset)
@ -81,7 +85,7 @@ Capybara.add_selector(:fieldset) do
end
Capybara.add_selector(:link) do
xpath(:title, :alt) do |locator, href: true, enable_aria_label: false, alt: nil, title: nil, **_options|
xpath(:title, :alt) do |locator, href: true, enable_aria_label: false, alt: nil, title: nil, **|
xpath = XPath.descendant(:a)
xpath = xpath[
case href
@ -128,8 +132,13 @@ Capybara.add_selector(:link) do
describe do |**options|
desc = +""
desc << " with href #{options[:href].inspect}" if options[:href]
desc << " with href #{options[:href].inspect}" if options[:href] && !options[:href].is_a?(Regexp)
desc << " with no href attribute" if options.fetch(:href, true).nil?
desc
end
describe_node_filters do |href: nil, **|
" with href matching #{href.inspect}" if href.is_a? Regexp
end
end
@ -163,11 +172,9 @@ Capybara.add_selector(:button) do
node_filter(:disabled, :boolean, default: false, skip_if: :all) { |node, value| !(value ^ node.disabled?) }
describe do |disabled: nil, **options|
desc = +""
desc << " that is disabled" if disabled == true
desc << describe_all_expression_filters(options)
desc
describe_expression_filters
describe_node_filters do |disabled: nil, **|
" that is disabled" if disabled == true
end
end
@ -179,7 +186,9 @@ Capybara.add_selector(:link_or_button) do
node_filter(:disabled, :boolean, default: false, skip_if: :all) { |node, value| node.tag_name == "a" || !(value ^ node.disabled?) }
describe { |disabled: nil, **_options| " that is disabled" if disabled == true }
describe_node_filters do |disabled: nil, **|
" that is disabled" if disabled == true
end
end
Capybara.add_selector(:fillable_field) do
@ -205,11 +214,9 @@ Capybara.add_selector(:fillable_field) do
with.is_a?(Regexp) ? node.value =~ with : node.value == with.to_s
end
describe do |options|
desc = +""
desc << describe_all_expression_filters(options)
desc << " with value #{options[:with].to_s.inspect}" if options.key?(:with)
desc
describe_expression_filters
describe_node_filters do |**options|
" with value #{options[:with].to_s.inspect}" if options.key?(:with)
end
end
@ -225,11 +232,9 @@ Capybara.add_selector(:radio_button) do
node_filter(:option) { |node, value| node.value == value.to_s }
describe do |option: nil, **options|
desc = +""
desc << " with value #{option.inspect}" if option
desc << describe_all_expression_filters(options)
desc
describe_expression_filters
describe_node_filters do |option: nil, **|
" with value #{option.inspect}" if option
end
end
@ -243,11 +248,9 @@ Capybara.add_selector(:checkbox) do
node_filter(:option) { |node, value| node.value == value.to_s }
describe do |option: nil, **options|
desc = +""
desc << " with value #{option.inspect}" if option
desc << describe_all_expression_filters(options)
desc
describe_expression_filters
describe_node_filters do |option: nil, **|
" with value #{option.inspect}" if option
end
end
@ -286,13 +289,18 @@ Capybara.add_selector(:select) do
(Array(selected) - actual).empty?
end
describe do |options: nil, with_options: nil, selected: nil, with_selected: nil, **opts|
describe do |with_options: nil, **opts|
desc = +""
desc << " with at least options #{with_options.inspect}" if with_options
desc << describe_all_expression_filters(opts)
desc
end
describe_node_filters do |options: nil, selected: nil, with_selected: nil, **|
desc = +""
desc << " with options #{options.inspect}" if options
desc << " with at least options #{with_options.inspect}" if with_options
desc << " with #{selected.inspect} selected" if selected
desc << " with at least #{with_selected.inspect} selected" if with_selected
desc << describe_all_expression_filters(opts)
desc
end
end
@ -318,13 +326,16 @@ Capybara.add_selector(:datalist_input) do
end
end
describe do |options: nil, with_options: nil, **opts|
describe do |with_options: nil, **opts|
desc = +""
desc << " with options #{options.inspect}" if options
desc << " with at least options #{with_options.inspect}" if with_options
desc << describe_all_expression_filters(opts)
desc
end
describe_node_filters do |options: nil, **|
" with options #{options.inspect}" if options
end
end
Capybara.add_selector(:option) do
@ -337,7 +348,7 @@ Capybara.add_selector(:option) do
node_filter(:disabled, :boolean) { |node, value| !(value ^ node.disabled?) }
node_filter(:selected, :boolean) { |node, value| !(value ^ node.selected?) }
describe do |**options|
describe_node_filters do |**options|
desc = +""
desc << " that is#{' not' unless options[:disabled]} disabled" if options.key?(:disabled)
desc << " that is#{' not' unless options[:selected]} selected" if options.key?(:selected)
@ -357,10 +368,8 @@ Capybara.add_selector(:datalist_option) do
node_filter(:disabled, :boolean) { |node, value| !(value ^ node.disabled?) }
describe do |**options|
desc = +""
desc << " that is#{' not' unless options[:disabled]} disabled" if options.key?(:disabled)
desc
describe_node_filters do |**options|
" that is#{' not' unless options[:disabled]} disabled" if options.key?(:disabled)
end
end
@ -373,11 +382,7 @@ Capybara.add_selector(:file_field) do
filter_set(:_field, %i[disabled multiple name])
describe do |**options|
desc = +""
desc << describe_all_expression_filters(options)
desc
end
describe_expression_filters
end
Capybara.add_selector(:label) do
@ -411,15 +416,13 @@ Capybara.add_selector(:label) do
end
end
describe do |**options|
desc = +""
desc << " for #{options[:for]}" if options[:for]
desc
describe_node_filters do |**options|
" for #{options[:for]}" if options[:for]
end
end
Capybara.add_selector(:table) do
xpath(:caption) do |locator, caption: nil, **_options|
xpath(:caption) do |locator, caption: nil, **|
xpath = XPath.descendant(:table)
unless locator.nil?
locator_matchers = (XPath.attr(:id) == locator.to_s) | XPath.descendant(:caption).is(locator.to_s)
@ -430,10 +433,8 @@ Capybara.add_selector(:table) do
xpath
end
describe do |caption: nil, **_options|
desc = +""
desc << " with caption #{caption}" if caption
desc
describe do |caption: nil, **|
" with caption \"#{caption}\"" if caption
end
end
@ -449,15 +450,13 @@ Capybara.add_selector(:frame) do
xpath
end
describe do |name: nil, **_options|
desc = +""
desc << " with name #{name}" if name
desc
describe do |name: nil, **|
" with name #{name}" if name
end
end
Capybara.add_selector(:element) do
xpath do |locator, **_options|
xpath do |locator, **|
XPath.descendant((locator || '@').to_sym)
end
@ -476,10 +475,6 @@ Capybara.add_selector(:element) do
val.is_a?(Regexp) ? node[name] =~ val : true
end
describe do |**options|
desc = +""
desc << describe_all_expression_filters(options)
desc
end
describe_expression_filters
end
# rubocop:enable Metrics/BlockLength

View File

@ -5,11 +5,12 @@ require 'capybara/selector/filter'
module Capybara
class Selector
class FilterSet
attr_reader :descriptions, :node_filters, :expression_filters
attr_reader :descriptions, :node_filter_descriptions, :node_filters, :expression_filters
def initialize(name, &block)
@name = name
@descriptions = []
@node_filter_descriptions = []
@expression_filters = {}
@node_filters = {}
instance_eval(&block)
@ -24,13 +25,19 @@ module Capybara
add_filter(name, Filters::ExpressionFilter, *types_and_options, &block)
end
def describe(&block)
descriptions.push block
def describe(node_filters: false, &block)
if node_filters
node_filter_descriptions.push block
else
descriptions.push block
end
end
def description(**options)
def description(skip_node_filters: false, **options)
opts = options_with_defaults(options)
@descriptions.map { |desc| desc.call(opts).to_s }.join
d = @descriptions.map { |desc| desc.call(opts).to_s }.join
d += @node_filter_descriptions.map { |desc| desc.call(opts).to_s }.join unless skip_node_filters
d
end
class << self

View File

@ -354,10 +354,25 @@ module Capybara
@filter_set.expression_filters.merge!(f_set.expression_filters.select(&filter_selector))
@filter_set.node_filters.merge!(f_set.node_filters.select(&filter_selector))
f_set.descriptions.each { |desc| @filter_set.describe(&desc) }
f_set.node_filter_descriptions.each { |desc| @filter_set.describe(node_filters: true, &desc) }
end
def_delegator :@filter_set, :describe
def describe_expression_filters(&block)
if block_given?
describe(node_filters: false, &block)
else
describe(node_filters: false) do |**options|
describe_all_expression_filters(options)
end
end
end
def describe_node_filters(&block)
describe(node_filters: true, &block)
end
##
#
# Set the default visibility mode that shouble be used if no visibile option is passed when using the selector.

View File

@ -62,7 +62,7 @@ Capybara::SpecHelper.spec '#ancestor' do
el = @session.find(:css, '#child')
expect do
el.ancestor(:xpath, '//div[@id="nosuchthing"]')
end.to raise_error(Capybara::ElementNotFound, "Unable to find visible xpath \"//div[@id=\\\"nosuchthing\\\"]\" that is an ancestor of visible css \"#child\"")
end.to raise_error(Capybara::ElementNotFound, "Unable to find xpath \"//div[@id=\\\"nosuchthing\\\"]\" that is an ancestor of visible css \"#child\"")
end
context "within a scope" do

View File

@ -98,7 +98,7 @@ Capybara::SpecHelper.spec "#attach_file" do
context "with a locator that doesn't exist" do
it "should raise an error" do
msg = "Unable to find visible file field \"does not exist\" that is not disabled"
msg = "Unable to find file field \"does not exist\""
expect do
@session.attach_file('does not exist', with_os_path_separators(@test_file_path))
end.to raise_error(Capybara::ElementNotFound, msg)

View File

@ -76,7 +76,7 @@ Capybara::SpecHelper.spec "#check" do
context "with a locator that doesn't exist" do
it "should raise an error" do
msg = "Unable to find visible checkbox \"does not exist\" that is not disabled"
msg = "Unable to find checkbox \"does not exist\""
expect do
@session.check('does not exist')
end.to raise_error(Capybara::ElementNotFound, msg)
@ -85,9 +85,10 @@ Capybara::SpecHelper.spec "#check" do
context "with a disabled checkbox" do
it "should raise an error" do
msg = "Unable to find visible checkbox \"Disabled Checkbox\" that is not disabled"
expect do
@session.check('Disabled Checkbox')
end.to raise_error(Capybara::ElementNotFound)
end.to raise_error(Capybara::ElementNotFound, msg)
end
end
@ -149,11 +150,11 @@ Capybara::SpecHelper.spec "#check" do
end
it "should raise original error when no label available" do
expect { @session.check('form_cars_ariel') }.to raise_error(Capybara::ElementNotFound, 'Unable to find visible checkbox "form_cars_ariel" that is not disabled')
expect { @session.check('form_cars_ariel') }.to raise_error(Capybara::ElementNotFound, 'Unable to find visible checkbox "form_cars_ariel"')
end
it "should raise error if not allowed to click label" do
expect { @session.check('form_cars_mclaren', allow_label_click: false) }.to raise_error(Capybara::ElementNotFound, 'Unable to find visible checkbox "form_cars_mclaren" that is not disabled')
expect { @session.check('form_cars_mclaren', allow_label_click: false) }.to raise_error(Capybara::ElementNotFound, 'Unable to find visible checkbox "form_cars_mclaren"')
end
end
@ -165,7 +166,11 @@ Capybara::SpecHelper.spec "#check" do
end
it "should raise error if checkbox not visible" do
expect { @session.check('form_cars_mclaren') }.to raise_error(Capybara::ElementNotFound, 'Unable to find visible checkbox "form_cars_mclaren" that is not disabled')
expect { @session.check('form_cars_mclaren') }.to raise_error(Capybara::ElementNotFound, 'Unable to find visible checkbox "form_cars_mclaren"')
end
it "should include node filter in error if verified" do
expect { @session.check('form_cars_maserati') }.to raise_error(Capybara::ElementNotFound, 'Unable to find visible checkbox "form_cars_maserati" that is not disabled')
end
context "with allow_label_click == true" do

View File

@ -31,7 +31,7 @@ Capybara::SpecHelper.spec "#choose" do
context "with a locator that doesn't exist" do
it "should raise an error" do
msg = "Unable to find visible radio button \"does not exist\" that is not disabled"
msg = "Unable to find radio button \"does not exist\""
expect do
@session.choose('does not exist')
end.to raise_error(Capybara::ElementNotFound, msg)
@ -89,7 +89,7 @@ Capybara::SpecHelper.spec "#choose" do
end
it "should raise error if not allowed to click label" do
expect { @session.choose("party_democrat", allow_label_click: false) }.to raise_error(Capybara::ElementNotFound, 'Unable to find visible radio button "party_democrat" that is not disabled')
expect { @session.choose("party_democrat", allow_label_click: false) }.to raise_error(Capybara::ElementNotFound, 'Unable to find visible radio button "party_democrat"')
end
end
end

View File

@ -345,7 +345,7 @@ Capybara::SpecHelper.spec '#click_button' do
context "with a locator that doesn't exist" do
it "should raise an error" do
msg = "Unable to find visible button \"does not exist\""
msg = "Unable to find button \"does not exist\""
expect do
@session.click_button('does not exist')
end.to raise_error(Capybara::ElementNotFound, msg)

View File

@ -56,7 +56,7 @@ Capybara::SpecHelper.spec '#click_link_or_button' do
context "when `true`" do
it "does not click on link which matches approximately" do
@session.visit('/with_html')
msg = "Unable to find visible link or button \"abore\""
msg = "Unable to find link or button \"abore\""
expect do
@session.click_link_or_button('abore', exact: true)
end.to raise_error(Capybara::ElementNotFound, msg)
@ -64,7 +64,7 @@ Capybara::SpecHelper.spec '#click_link_or_button' do
it "does not click on approximately matching button" do
@session.visit('/form')
msg = "Unable to find visible link or button \"awe\""
msg = "Unable to find link or button \"awe\""
expect do
@session.click_link_or_button('awe', exact: true)
@ -76,7 +76,7 @@ Capybara::SpecHelper.spec '#click_link_or_button' do
context "with a locator that doesn't exist" do
it "should raise an error" do
@session.visit('/with_html')
msg = "Unable to find visible link or button \"does not exist\""
msg = "Unable to find link or button \"does not exist\""
expect do
@session.click_link_or_button('does not exist')
end.to raise_error(Capybara::ElementNotFound, msg)

View File

@ -69,7 +69,7 @@ Capybara::SpecHelper.spec '#click_link' do
context "with a locator that doesn't exist" do
it "should raise an error" do
msg = "Unable to find visible link \"does not exist\""
msg = "Unable to find link \"does not exist\""
expect do
@session.click_link('does not exist')
end.to raise_error(Capybara::ElementNotFound, msg)

View File

@ -193,7 +193,7 @@ Capybara::SpecHelper.spec "#fill_in" do
after { Capybara.ignore_hidden_elements = false }
it "should not find a hidden field" do
msg = "Unable to find visible field \"Super Secret\" that is not disabled"
msg = "Unable to find visible field \"Super Secret\""
expect do
@session.fill_in('Super Secret', with: '777')
end.to raise_error(Capybara::ElementNotFound, msg)
@ -202,7 +202,7 @@ Capybara::SpecHelper.spec "#fill_in" do
context "with a locator that doesn't exist" do
it "should raise an error" do
msg = "Unable to find visible field \"does not exist\" that is not disabled"
msg = "Unable to find field \"does not exist\""
expect do
@session.fill_in('does not exist', with: 'Blah blah')
end.to raise_error(Capybara::ElementNotFound, msg)

View File

@ -218,7 +218,7 @@ Capybara::SpecHelper.spec '#find' do
it "should raise ElementNotFound with a useful default message if nothing was found" do
expect do
@session.find(:xpath, '//div[@id="nosuchthing"]').to be_nil
end.to raise_error(Capybara::ElementNotFound, "Unable to find visible xpath \"//div[@id=\\\"nosuchthing\\\"]\"")
end.to raise_error(Capybara::ElementNotFound, "Unable to find xpath \"//div[@id=\\\"nosuchthing\\\"]\"")
end
it "should accept an XPath instance" do

View File

@ -31,6 +31,9 @@ Capybara::SpecHelper.spec "#to_capybara_node" do
end.to raise_error(/^expected to find text "Header Class Test One" in "Lore/)
expect do
expect(para).to have_css('#second')
end.to raise_error(/^expected to find visible css "#second" within #<Capybara::Node::Element/)
end.to raise_error(/^expected to find css "#second" within #<Capybara::Node::Element/)
expect do
expect(para).to have_link(href: %r{/without_simple_html})
end.to raise_error(%r{^expected to find visible link nil with href matching /\\/without_simple_html/ within #<Capybara::Node::Element})
end
end

View File

@ -94,7 +94,7 @@ Capybara::SpecHelper.spec "#select" do
it "should not find an input without a datalist" do
expect do
@session.select("Thomas", from: 'form_first_name')
end.to raise_error(/Unable to find visible input box with datalist completion "form_first_name" that is not disabled/)
end.to raise_error(/Unable to find input box with datalist completion "form_first_name"/)
end
it "should not select an option that doesn't exist" do
@ -112,7 +112,7 @@ Capybara::SpecHelper.spec "#select" do
context "with a locator that doesn't exist" do
it "should raise an error" do
msg = /Unable to find visible select box "does not exist" that is not disabled/
msg = /Unable to find select box "does not exist"/
expect do
@session.select('foo', from: 'does not exist')
end.to raise_error(Capybara::ElementNotFound, msg)
@ -121,7 +121,7 @@ Capybara::SpecHelper.spec "#select" do
context "with an option that doesn't exist" do
it "should raise an error" do
msg = /^Unable to find visible option "Does not Exist" within/
msg = /^Unable to find option "Does not Exist" within/
expect do
@session.select('Does not Exist', from: 'form_locale')
end.to raise_error(Capybara::ElementNotFound, msg)

View File

@ -77,11 +77,15 @@ Capybara::SpecHelper.spec "#uncheck" do
end
it "should raise original error when no label available" do
expect { @session.uncheck('form_cars_porsche') }.to raise_error(Capybara::ElementNotFound, 'Unable to find visible checkbox "form_cars_porsche" that is not disabled')
expect { @session.uncheck('form_cars_porsche') }.to raise_error(Capybara::ElementNotFound, 'Unable to find visible checkbox "form_cars_porsche"')
end
it "should raise error if not allowed to click label" do
expect { @session.uncheck('form_cars_jaguar', allow_label_click: false) }.to raise_error(Capybara::ElementNotFound, 'Unable to find visible checkbox "form_cars_jaguar" that is not disabled')
expect { @session.uncheck('form_cars_jaguar', allow_label_click: false) }.to raise_error(Capybara::ElementNotFound, 'Unable to find visible checkbox "form_cars_jaguar"')
end
it "should include node filter description in error if necessary" do
expect { @session.uncheck('form_cars_maserati', allow_label_click: false) }.to raise_error(Capybara::ElementNotFound, 'Unable to find visible checkbox "form_cars_maserati" that is not disabled')
end
end
end

View File

@ -56,7 +56,7 @@ Capybara::SpecHelper.spec "#unselect" do
context "with a locator that doesn't exist" do
it "should raise an error" do
msg = "Unable to find visible select box \"does not exist\" that is not disabled"
msg = "Unable to find select box \"does not exist\""
expect do
@session.unselect('foo', from: 'does not exist')
end.to raise_error(Capybara::ElementNotFound, msg)
@ -65,7 +65,7 @@ Capybara::SpecHelper.spec "#unselect" do
context "with an option that doesn't exist" do
it "should raise an error" do
msg = /^Unable to find visible option "Does not Exist" within/
msg = /^Unable to find option "Does not Exist" within/
expect do
@session.unselect('Does not Exist', from: 'form_underwear')
end.to raise_error(Capybara::ElementNotFound, msg)

View File

@ -195,6 +195,8 @@ New line after and before textarea tag
Koenigsegg
<input type="checkbox" value="koenigsegg" name="form[cars][]" id="form_cars_koenigsegg" checked="checked" style="display: none"/>
</label>
<input type="checkbox" value="maserati" name="form[cars][]" id="form_cars_maserati" disabled="disabled"/>
<label for="form_cars_maserati">Maserati</label>
</p>
<p>

View File

@ -138,6 +138,6 @@ RSpec.describe 'capybara/minitest/spec' do
reporter.report
expect(output.string).to include("19 runs, 41 assertions, 1 failures, 0 errors, 1 skips")
# Make sure error messages are displayed
expect(output.string).to include('expected to find visible select box "non_existing_form_title" that is not disabled but there were no matches')
expect(output.string).to include('expected to find select box "non_existing_form_title" but there were no matches')
end
end

View File

@ -25,7 +25,7 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
it "fails if has_css? returns false" do
expect do
expect("<h1>Text</h1>").to have_css('h2')
end.to raise_error(/expected to find visible css "h2" but there were no matches/)
end.to raise_error(/expected to find css "h2" but there were no matches/)
end
it "passes if matched node count equals expected count" do
@ -41,7 +41,7 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
it "fails if matched node count is less than expected minimum count" do
expect do
expect("<h1>Text</h1>").to have_css('p', minimum: 1)
end.to raise_error("expected to find visible css \"p\" at least 1 time but there were no matches")
end.to raise_error("expected to find css \"p\" at least 1 time but there were no matches")
end
it "fails if matched node count is more than expected maximum count" do
@ -100,7 +100,7 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
it "fails if has_css? returns false" do
expect do
expect(page).to have_css('h1#doesnotexist')
end.to raise_error(/expected to find visible css "h1#doesnotexist" but there were no matches/)
end.to raise_error(/expected to find css "h1#doesnotexist" but there were no matches/)
end
end
@ -132,7 +132,7 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
it "fails if has_xpath? returns false" do
expect do
expect("<h1>Text</h1>").to have_xpath('//h2')
end.to raise_error(%r{expected to find visible xpath "//h2" but there were no matches})
end.to raise_error(%r{expected to find xpath "//h2" but there were no matches})
end
end
@ -169,7 +169,7 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
it "fails if has_xpath? returns false" do
expect do
expect(page).to have_xpath("//h1[@id='doesnotexist']")
end.to raise_error(%r{expected to find visible xpath "//h1\[@id='doesnotexist'\]" but there were no matches})
end.to raise_error(%r{expected to find xpath "//h1\[@id='doesnotexist'\]" but there were no matches})
end
end
@ -203,7 +203,7 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
it "fails if has_selector? returns false" do
expect do
expect("<h1>Text</h1>").to have_selector('//h2')
end.to raise_error(%r{expected to find visible xpath "//h2" but there were no matches})
end.to raise_error(%r{expected to find xpath "//h2" but there were no matches})
end
end
@ -233,7 +233,7 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
it "fails if has_selector? returns false" do
expect do
expect(page).to have_selector("//h1[@id='doesnotexist']")
end.to raise_error(%r{expected to find visible xpath "//h1\[@id='doesnotexist'\]" but there were no matches})
end.to raise_error(%r{expected to find xpath "//h1\[@id='doesnotexist'\]" but there were no matches})
end
it "includes text in error message" do
@ -503,7 +503,7 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
it "fails if there is no such button" do
expect do
expect(html).to have_link('No such Link')
end.to raise_error(/expected to find visible link "No such Link"/)
end.to raise_error(/expected to find link "No such Link"/)
end
it "supports compounding" do
@ -638,7 +638,7 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
it "fails if there is no such button" do
expect do
expect(html).to have_button('No such Button')
end.to raise_error(/expected to find visible button "No such Button"/)
end.to raise_error(/expected to find button "No such Button"/)
end
it "supports compounding" do
@ -668,7 +668,7 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
it "fails if there is no such field" do
expect do
expect(html).to have_field('No such Field')
end.to raise_error(/expected to find visible field "No such Field"/)
end.to raise_error(/expected to find field "No such Field"/)
end
it "fails if there is such field but with false value" do
@ -715,7 +715,7 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
it "fails if there is no such field" do
expect do
expect(html).to have_checked_field('no such field')
end.to raise_error(/expected to find visible field "no such field"/)
end.to raise_error(/expected to find field "no such field"/)
end
end
@ -764,7 +764,7 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
it "fails if there is no such field" do
expect do
expect(html).to have_unchecked_field('no such field')
end.to raise_error(/expected to find visible field "no such field"/)
end.to raise_error(/expected to find field "no such field"/)
end
end
@ -807,7 +807,7 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
it "fails if there is no such select" do
expect do
expect(html).to have_select('No such Select box')
end.to raise_error(/expected to find visible select box "No such Select box"/)
end.to raise_error(/expected to find select box "No such Select box"/)
end
it "supports compounding" do
@ -820,6 +820,7 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
it "gives proper description" do
expect(have_table('Lovely table').description).to eq("have visible table \"Lovely table\"")
expect(have_table('Lovely table', caption: 'my caption').description).to eq('have visible table "Lovely table" with caption "my caption"')
end
it "gives proper description when :visible option passed" do
@ -829,14 +830,14 @@ RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
expect(have_table('Lovely table', visible: false).description).to eq("have table \"Lovely table\"")
end
it "passes if there is such a select" do
it "passes if there is such a table" do
expect(html).to have_table('Lovely table')
end
it "fails if there is no such select" do
it "fails if there is no such table" do
expect do
expect(html).to have_table('No such Table')
end.to raise_error(/expected to find visible table "No such Table"/)
end.to raise_error(/expected to find table "No such Table"/)
end
it "supports compounding" do

View File

@ -159,11 +159,12 @@ RSpec.shared_examples "Capybara::Session" do |session, mode|
context '#fill_in with Date' do
before do
session.visit('/form')
session.execute_script <<-JS
fd = session.find(:css, '#form_date')
fd.execute_script <<-JS
window.capybara_formDateFiredEvents = [];
var fd = this;
['focus', 'input', 'change'].forEach(function(eventType) {
document.getElementById('form_date')
.addEventListener(eventType, function() { window.capybara_formDateFiredEvents.push(eventType); });
fd.addEventListener(eventType, function() { window.capybara_formDateFiredEvents.push(eventType); });
});
JS
# work around weird FF issue where it would create an extra focus issue in some cases