Add normalize_ws support to :text filter of selector queries
This commit is contained in:
parent
34d51979b2
commit
e01cf4cbe0
|
@ -4,7 +4,7 @@ module Capybara
|
||||||
module Queries
|
module Queries
|
||||||
class SelectorQuery < Queries::BaseQuery
|
class SelectorQuery < Queries::BaseQuery
|
||||||
attr_reader :expression, :selector, :locator, :options
|
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
|
VALID_MATCH = %i[first smart prefer_exact one].freeze
|
||||||
|
|
||||||
def initialize(*args,
|
def initialize(*args,
|
||||||
|
@ -338,10 +338,14 @@ module Capybara
|
||||||
matches_text_regexp?(node, regexp)
|
matches_text_regexp?(node, regexp)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def normalize_ws
|
||||||
|
options.fetch(:normalize_ws, session_options.default_normalize_ws)
|
||||||
|
end
|
||||||
|
|
||||||
def matches_text_regexp?(node, regexp)
|
def matches_text_regexp?(node, regexp)
|
||||||
text_visible = visible
|
text_visible = visible
|
||||||
text_visible = :all if text_visible == :hidden
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -78,6 +78,23 @@ Capybara::SpecHelper.spec '#has_selector?' do
|
||||||
expect(@session).to have_selector(:css, 'p a#foo', 'extra')
|
expect(@session).to have_selector(:css, 'p a#foo', 'extra')
|
||||||
end.to raise_error ArgumentError, /extra/
|
end.to raise_error ArgumentError, /extra/
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
context 'with exact_text' do
|
context 'with exact_text' do
|
||||||
|
|
Loading…
Reference in New Issue