diff --git a/lib/capybara/selector/selector.rb b/lib/capybara/selector/selector.rb index 579cedbf..fd66cd7a 100644 --- a/lib/capybara/selector/selector.rb +++ b/lib/capybara/selector/selector.rb @@ -251,7 +251,7 @@ module Capybara def css(*allowed_filters, &block) if block @format, @expression = :css, block - allowed_filters.flatten.each { |ef| expression_filters[ef] = nil } + allowed_filters.flatten.each { |ef| expression_filters[ef] = Filters::IdentityExpressionFilter.new(ef) } end format == :css ? @expression : nil end diff --git a/spec/selector_spec.rb b/spec/selector_spec.rb index 5d26ad70..e680cabe 100644 --- a/spec/selector_spec.rb +++ b/spec/selector_spec.rb @@ -54,7 +54,23 @@ RSpec.describe Capybara do end Capybara.add_selector :custom_css_selector do - css { |selector| selector } + css(:name) do |selector, name: nil, **| + selector ||= '' + selector += "[name='#{name}']" if name + selector + end + + expression_filter(:placeholder) do |expr, val| + expr + "[placeholder='#{val}']" + end + + expression_filter(:value) do |expr, val| + expr + "[value='#{val}']" + end + + expression_filter(:title) do |expr, val| + expr + "[title='#{val}']" + end end Capybara.add_selector :custom_xpath_selector do @@ -104,6 +120,19 @@ RSpec.describe Capybara do end end + describe 'css based selectors' do + it "supports filters specified in 'css' definition" do + expect(string).to have_selector(:custom_css_selector, 'input', name: 'form[my_text_input]') + expect(string).to have_no_selector(:custom_css_selector, 'input', name: 'form[not_my_text_input]') + end + + it 'supports explicitly defined expression filters' do + expect(string).to have_selector(:custom_css_selector, placeholder: 'my text') + expect(string).to have_no_selector(:custom_css_selector, placeholder: 'not my text') + expect(string).to have_selector(:custom_css_selector, value: 'click me', title: 'submit button') + end + end + describe 'builtin selectors' do context 'when locator is nil' do it 'devolves to just finding element types' do