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

Support multiple expression types in one selector

This commit is contained in:
Thomas Walpole 2019-04-22 18:04:15 -07:00
parent d6f570ea19
commit dcd7fab897
8 changed files with 24 additions and 21 deletions

View file

@ -11,6 +11,7 @@ module Capybara
session_options:, session_options:,
enable_aria_label: session_options.enable_aria_label, enable_aria_label: session_options.enable_aria_label,
test_id: session_options.test_id, test_id: session_options.test_id,
selector_format: nil,
**options, **options,
&filter_block) &filter_block)
@resolved_node = nil @resolved_node = nil
@ -21,7 +22,8 @@ module Capybara
@selector = Selector.new( @selector = Selector.new(
find_selector(args[0].is_a?(Symbol) ? args.shift : args[0]), find_selector(args[0].is_a?(Symbol) ? args.shift : args[0]),
config: { enable_aria_label: enable_aria_label, test_id: test_id } config: { enable_aria_label: enable_aria_label, test_id: test_id },
format: selector_format
) )
@locator = args.shift @locator = args.shift
@ -371,8 +373,8 @@ module Capybara
node.is_a?(::Capybara::Node::Simple) && node.path == '/' node.is_a?(::Capybara::Node::Simple) && node.path == '/'
end end
def apply_filter?(_filter) def apply_filter?(filter)
true filter.format.nil? || (filter.format == selector_format)
end end
def matches_locator_filter?(node) def matches_locator_filter?(node)

View file

@ -24,6 +24,10 @@ module Capybara
@options.key?(:skip_if) && value == @options[:skip_if] @options.key?(:skip_if) && value == @options[:skip_if]
end end
def format
@options[:format]
end
def matcher? def matcher?
!@matcher.nil? !@matcher.nil?
end end

View file

@ -11,7 +11,7 @@ module Capybara
end end
def matches?(node, value, context = nil, exact:) def matches?(node, value, context = nil, exact:)
apply(node, value, true, context, exact: exact) apply(node, value, true, context, exact: exact, format: context&.default_format)
rescue Capybara::ElementNotFound rescue Capybara::ElementNotFound
false false
end end

View file

@ -203,16 +203,17 @@ module Capybara
attr_reader :errors attr_reader :errors
def initialize(definition, config:) def initialize(definition, config:, format:)
definition = self.class[definition] unless definition.is_a? Definition definition = self.class[definition] unless definition.is_a? Definition
super(definition) super(definition)
@definition = definition @definition = definition
@config = config @config = config
@format = format
@errors = [] @errors = []
end end
def format def format
@definition.default_format @format || @definition.default_format
end end
alias_method :current_format, :format alias_method :current_format, :format
@ -240,14 +241,10 @@ module Capybara
errors << error_msg errors << error_msg
end end
def expression_for(name, locator, config: @config, **options) def expression_for(name, locator, config: @config, format: current_format, **options)
Selector.new(name, config: config).call(locator, **options) Selector.new(name, config: config, format: format).call(locator, **options)
end end
# def expression_for(name, locator, config: @config, format: current_format, **options)
# Selector.new(name, config: config, format: format).call(locator, **options)
# end
# @api private # @api private
def with_filter_errors(errors) def with_filter_errors(errors)
old_errors = @errors old_errors = @errors

View file

@ -30,7 +30,7 @@ Capybara::SpecHelper.spec '#all' do
it 'should accept an XPath instance', :exact_false do it 'should accept an XPath instance', :exact_false do
@session.visit('/form') @session.visit('/form')
@xpath = Capybara::Selector.new(:fillable_field, config: {}).call('Name') @xpath = Capybara::Selector.new(:fillable_field, config: {}, format: :xpath).call('Name')
expect(@xpath).to be_a(::XPath::Union) expect(@xpath).to be_a(::XPath::Union)
@result = @session.all(@xpath).map(&:value) @result = @session.all(@xpath).map(&:value)
expect(@result).to include('Smith', 'John', 'John Smith') expect(@result).to include('Smith', 'John', 'John Smith')

View file

@ -235,7 +235,7 @@ Capybara::SpecHelper.spec '#find' do
it 'should accept an XPath instance' do it 'should accept an XPath instance' do
@session.visit('/form') @session.visit('/form')
@xpath = Capybara::Selector.new(:fillable_field, config: {}).call('First Name') @xpath = Capybara::Selector.new(:fillable_field, config: {}, format: :xpath).call('First Name')
expect(@xpath).to be_a(::XPath::Union) expect(@xpath).to be_a(::XPath::Union)
expect(@session.find(@xpath).value).to eq('John') expect(@session.find(@xpath).value).to eq('John')
end end

View file

@ -24,7 +24,7 @@ Capybara::SpecHelper.spec '#first' do
it 'should accept an XPath instance' do it 'should accept an XPath instance' do
@session.visit('/form') @session.visit('/form')
@xpath = Capybara::Selector.new(:fillable_field, config: {}).call('First Name') @xpath = Capybara::Selector.new(:fillable_field, config: {}, format: :xpath).call('First Name')
expect(@xpath).to be_a(::XPath::Union) expect(@xpath).to be_a(::XPath::Union)
expect(@session.first(@xpath).value).to eq('John') expect(@session.first(@xpath).value).to eq('John')
end end

View file

@ -167,7 +167,7 @@ RSpec.describe Capybara do
Capybara.add_selector :test do Capybara.add_selector :test do
xpath(:something, :other) { |_locator| XPath.descendant } xpath(:something, :other) { |_locator| XPath.descendant }
end end
selector = Capybara::Selector.new :test, config: nil selector = Capybara::Selector.new :test, config: nil, format: nil
expect(selector.expression_filters.keys).to include(:something, :other) expect(selector.expression_filters.keys).to include(:something, :other)
end end
@ -176,7 +176,7 @@ RSpec.describe Capybara do
Capybara.add_selector :test do Capybara.add_selector :test do
xpath { |_locator, valid3:, valid4: nil| "#{valid3} #{valid4}" } xpath { |_locator, valid3:, valid4: nil| "#{valid3} #{valid4}" }
end end
selector = Capybara::Selector.new :test, config: nil selector = Capybara::Selector.new :test, config: nil, format: nil
expect(selector.expression_filters.keys).to include(:valid3, :valid4) expect(selector.expression_filters.keys).to include(:valid3, :valid4)
end end
@ -185,7 +185,7 @@ RSpec.describe Capybara do
Capybara.add_selector :test do Capybara.add_selector :test do
xpath(:valid1) { |_locator, valid3:, valid4: nil| "#{valid3} #{valid4}" } xpath(:valid1) { |_locator, valid3:, valid4: nil| "#{valid3} #{valid4}" }
end end
selector = Capybara::Selector.new :test, config: nil selector = Capybara::Selector.new :test, config: nil, format: nil
expect(selector.expression_filters.keys).to include(:valid1) expect(selector.expression_filters.keys).to include(:valid1)
expect(selector.expression_filters.keys).not_to include(:valid3, :valid4) expect(selector.expression_filters.keys).not_to include(:valid3, :valid4)
@ -208,7 +208,7 @@ RSpec.describe Capybara do
Capybara.add_selector :test do Capybara.add_selector :test do
css(:name, :other_name) { |_locator| '' } css(:name, :other_name) { |_locator| '' }
end end
selector = Capybara::Selector.new :test, config: nil selector = Capybara::Selector.new :test, config: nil, format: nil
expect(selector.expression_filters.keys).to include(:name, :other_name) expect(selector.expression_filters.keys).to include(:name, :other_name)
end end
@ -217,7 +217,7 @@ RSpec.describe Capybara do
Capybara.add_selector :test do Capybara.add_selector :test do
css { |_locator, valid3:, valid4: nil| "#{valid3} #{valid4}" } css { |_locator, valid3:, valid4: nil| "#{valid3} #{valid4}" }
end end
selector = Capybara::Selector.new :test, config: nil selector = Capybara::Selector.new :test, config: nil, format: nil
expect(selector.expression_filters.keys).to include(:valid3, :valid4) expect(selector.expression_filters.keys).to include(:valid3, :valid4)
end end
@ -226,7 +226,7 @@ RSpec.describe Capybara do
Capybara.add_selector :test do Capybara.add_selector :test do
css(:valid1) { |_locator, valid3:, valid4: nil| "#{valid3} #{valid4}" } css(:valid1) { |_locator, valid3:, valid4: nil| "#{valid3} #{valid4}" }
end end
selector = Capybara::Selector.new :test, config: nil selector = Capybara::Selector.new :test, config: nil, format: nil
expect(selector.expression_filters.keys).to include(:valid1) expect(selector.expression_filters.keys).to include(:valid1)
expect(selector.expression_filters.keys).not_to include(:valid3, :valid4) expect(selector.expression_filters.keys).not_to include(:valid3, :valid4)