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

Fix CSS based default expression filters

This commit is contained in:
Thomas Walpole 2018-10-12 10:23:34 -07:00
parent cf92934ac9
commit 51ec106095
2 changed files with 31 additions and 2 deletions

View file

@ -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

View file

@ -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