Skip to content

Commit

Permalink
Support multiple expression types in one selector
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Apr 23, 2019
1 parent d6f570e commit dcd7fab
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 21 deletions.
8 changes: 5 additions & 3 deletions lib/capybara/queries/selector_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def initialize(*args,
session_options:,
enable_aria_label: session_options.enable_aria_label,
test_id: session_options.test_id,
selector_format: nil,
**options,
&filter_block)
@resolved_node = nil
Expand All @@ -21,7 +22,8 @@ def initialize(*args,

@selector = Selector.new(
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
Expand Down Expand Up @@ -371,8 +373,8 @@ def simple_root?(node)
node.is_a?(::Capybara::Node::Simple) && node.path == '/'
end

def apply_filter?(_filter)
true
def apply_filter?(filter)
filter.format.nil? || (filter.format == selector_format)
end

def matches_locator_filter?(node)
Expand Down
4 changes: 4 additions & 0 deletions lib/capybara/selector/filters/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def skip?(value)
@options.key?(:skip_if) && value == @options[:skip_if]
end

def format
@options[:format]
end

def matcher?
!@matcher.nil?
end
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/selector/filters/locator_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(block, **options)
end

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
false
end
Expand Down
13 changes: 5 additions & 8 deletions lib/capybara/selector/selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,17 @@ def for(locator)

attr_reader :errors

def initialize(definition, config:)
def initialize(definition, config:, format:)
definition = self.class[definition] unless definition.is_a? Definition
super(definition)
@definition = definition
@config = config
@format = format
@errors = []
end

def format
@definition.default_format
@format || @definition.default_format
end
alias_method :current_format, :format

Expand Down Expand Up @@ -240,14 +241,10 @@ def add_error(error_msg)
errors << error_msg
end

def expression_for(name, locator, config: @config, **options)
Selector.new(name, config: config).call(locator, **options)
def expression_for(name, locator, config: @config, format: current_format, **options)
Selector.new(name, config: config, format: format).call(locator, **options)
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
def with_filter_errors(errors)
old_errors = @errors
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/spec/session/all_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

it 'should accept an XPath instance', :exact_false do
@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)
@result = @session.all(@xpath).map(&:value)
expect(@result).to include('Smith', 'John', 'John Smith')
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/spec/session/find_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@

it 'should accept an XPath instance' do
@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(@session.find(@xpath).value).to eq('John')
end
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/spec/session/first_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

it 'should accept an XPath instance' do
@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(@session.first(@xpath).value).to eq('John')
end
Expand Down
12 changes: 6 additions & 6 deletions spec/selector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
Capybara.add_selector :test do
xpath(:something, :other) { |_locator| XPath.descendant }
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)
end
Expand All @@ -176,7 +176,7 @@
Capybara.add_selector :test do
xpath { |_locator, valid3:, valid4: nil| "#{valid3} #{valid4}" }
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)
end
Expand All @@ -185,7 +185,7 @@
Capybara.add_selector :test do
xpath(:valid1) { |_locator, valid3:, valid4: nil| "#{valid3} #{valid4}" }
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).not_to include(:valid3, :valid4)
Expand All @@ -208,7 +208,7 @@
Capybara.add_selector :test do
css(:name, :other_name) { |_locator| '' }
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)
end
Expand All @@ -217,7 +217,7 @@
Capybara.add_selector :test do
css { |_locator, valid3:, valid4: nil| "#{valid3} #{valid4}" }
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)
end
Expand All @@ -226,7 +226,7 @@
Capybara.add_selector :test do
css(:valid1) { |_locator, valid3:, valid4: nil| "#{valid3} #{valid4}" }
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).not_to include(:valid3, :valid4)
Expand Down

0 comments on commit dcd7fab

Please sign in to comment.