diff --git a/lib/capybara/queries/selector_query.rb b/lib/capybara/queries/selector_query.rb index 90454ef7..a41ef3a6 100644 --- a/lib/capybara/queries/selector_query.rb +++ b/lib/capybara/queries/selector_query.rb @@ -50,6 +50,15 @@ module Capybara end desc << " with id #{options[:id]}" if options[:id] desc << " with classes [#{Array(options[:class]).join(',')}]" if options[:class] + desc << case options[:style] + when String + " with style attribute #{options[:style].inspect}" + when Regexp + " with style attribute matching #{options[:style].inspect}" + when Hash + " with styles #{options[:style].inspect}" + else '' + end desc << selector.description(node_filters: show_for[:node], **options) desc << ' that also matches the custom filter block' if @filter_block && show_for[:node] desc << " within #{@resolved_node.inspect}" if describe_within? diff --git a/lib/capybara/spec/session/has_css_spec.rb b/lib/capybara/spec/session/has_css_spec.rb index b3749641..411fe518 100644 --- a/lib/capybara/spec/session/has_css_spec.rb +++ b/lib/capybara/spec/session/has_css_spec.rb @@ -41,14 +41,26 @@ Capybara::SpecHelper.spec '#has_css?' do context ':style option' do it 'should support String' do expect(@session).to have_css('p', style: 'line-height: 25px;') + + expect do + expect(@session).to have_css('p', style: 'display: not_valid') + end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /style attribute "display: not_valid"/) end it 'should support Regexp' do expect(@session).to have_css('p', style: /-height: 2/) + + expect do + expect(@session).to have_css('p', style: /not_valid/) + end.to raise_error(RSpec::Expectations::ExpectationNotMetError, %r{style attribute matching /not_valid/}) end it 'should support Hash', requires: [:css] do expect(@session).to have_css('p', style: { 'line-height': '25px' }) + + expect do + expect(@session).to have_css('p', style: { 'line-height': '30px' }) + end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /with styles \{:"line-height"=>"30px"\}/) end end