Add normalize_ws support to :text filter of selector queries

This commit is contained in:
Thomas Walpole 2018-08-27 10:00:07 -07:00
parent 34d51979b2
commit e01cf4cbe0
2 changed files with 23 additions and 2 deletions

View File

@ -4,7 +4,7 @@ module Capybara
module Queries
class SelectorQuery < Queries::BaseQuery
attr_reader :expression, :selector, :locator, :options
VALID_KEYS = COUNT_KEYS + %i[text id class visible exact exact_text match wait filter_set]
VALID_KEYS = COUNT_KEYS + %i[text id class visible exact exact_text normalize_ws match wait filter_set]
VALID_MATCH = %i[first smart prefer_exact one].freeze
def initialize(*args,
@ -338,10 +338,14 @@ module Capybara
matches_text_regexp?(node, regexp)
end
def normalize_ws
options.fetch(:normalize_ws, session_options.default_normalize_ws)
end
def matches_text_regexp?(node, regexp)
text_visible = visible
text_visible = :all if text_visible == :hidden
!!node.text(text_visible).match(regexp)
!!node.text(text_visible, normalize_ws: normalize_ws).match(regexp)
end
end
end

View File

@ -78,6 +78,23 @@ Capybara::SpecHelper.spec '#has_selector?' do
expect(@session).to have_selector(:css, 'p a#foo', 'extra')
end.to raise_error ArgumentError, /extra/
end
context 'with whitespace normalization' do
context 'Capybara.default_normalize_ws = false' do
it 'should support normalize_ws option' do
Capybara.default_normalize_ws = false
expect(@session).not_to have_selector(:id, 'second', text: 'text with whitespace')
expect(@session).to have_selector(:id, 'second', text: 'text with whitespace', normalize_ws: true)
end
end
context 'Capybara.default_normalize_ws = true' do
it 'should support normalize_ws option' do
Capybara.default_normalize_ws = true
expect(@session).to have_selector(:id, 'second', text: 'text with whitespace')
expect(@session).not_to have_selector(:id, 'second', text: 'text with whitespace', normalize_ws: false)
end
end
end
end
context 'with exact_text' do