diff --git a/lib/capybara/queries/selector_query.rb b/lib/capybara/queries/selector_query.rb index 0108960da..28489df8a 100644 --- a/lib/capybara/queries/selector_query.rb +++ b/lib/capybara/queries/selector_query.rb @@ -70,7 +70,8 @@ def description(only_applied = false) # rubocop:disable Style/OptionalBooleanPar desc << 'non-visible ' if visible == :hidden end - desc << "#{label} #{locator.inspect}" + desc << label.to_s + desc << " #{locator.inspect}" unless locator.nil? if show_for[:any] desc << " with#{' exact' if exact_text == true} text #{options[:text].inspect}" if options[:text] diff --git a/lib/capybara/spec/session/find_spec.rb b/lib/capybara/spec/session/find_spec.rb index 9882b5fd8..9e2b38a05 100644 --- a/lib/capybara/spec/session/find_spec.rb +++ b/lib/capybara/spec/session/find_spec.rb @@ -233,6 +233,14 @@ end.to raise_error(Capybara::ElementNotFound, 'Unable to find xpath "//div[@id=\\"nosuchthing\\"]"') end + context 'without locator' do + it 'should not output `nil` in the ElementNotFound message if nothing was found' do + expect do + @session.find(:label, text: 'no such thing').to be_nil + end.to raise_error(Capybara::ElementNotFound, 'Unable to find label') + end + end + it 'should accept an XPath instance' do @session.visit('/form') @xpath = Capybara::Selector.new(:fillable_field, config: {}, format: :xpath).call('First Name') diff --git a/lib/capybara/spec/session/node_wrapper_spec.rb b/lib/capybara/spec/session/node_wrapper_spec.rb index e30134704..8494da389 100644 --- a/lib/capybara/spec/session/node_wrapper_spec.rb +++ b/lib/capybara/spec/session/node_wrapper_spec.rb @@ -34,6 +34,6 @@ def to_capybara_node(); @element end end.to raise_error(/^expected to find css "#second" within #