Skip to content

Commit

Permalink
Add locator_filter support
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Nov 14, 2018
1 parent fb2f6e3 commit a47b9fd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
7 changes: 7 additions & 0 deletions lib/capybara/queries/selector_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def applied_description

def matches_filters?(node, node_filter_errors = [])
return true if (@resolved_node&.== node) && options[:allow_self]
return false unless matches_locator_filter?(node)

applied_filters << :system
return false unless matches_system_filters?(node)
Expand Down Expand Up @@ -305,6 +306,12 @@ def simple_root?(node)
node.is_a?(::Capybara::Node::Simple) && node.path == '/'
end

def matches_locator_filter?(node)
return true if @selector.locator_filter.nil?

@selector.locator_filter.call(node, @locator)
end

def matches_system_filters?(node)
matches_id_filter?(node) &&
matches_class_filter?(node) &&
Expand Down
10 changes: 3 additions & 7 deletions lib/capybara/selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
end

Capybara.add_selector(:id) do
xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }
xpath { |id| XPath.descendant[builder.attribute_conditions(id: id)] }
locator_filter { |node, id| id.is_a?(Regexp) ? node[:id] =~ id : true }
end

Capybara.add_selector(:field) do
Expand Down Expand Up @@ -118,12 +119,7 @@
end

expression_filter(:download, valid_values: [true, false, String]) do |expr, download|
mod = case download
when true then XPath.attr(:download)
when false then !XPath.attr(:download)
when String then XPath.attr(:download) == download
end
expr[mod]
expr[builder.attribute_conditions(download: download)]
end

describe_expression_filters do |**options|
Expand Down
8 changes: 7 additions & 1 deletion lib/capybara/selector/selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Capybara
# * Locator: A CSS selector
#
# * **:id** - Select element by id
# * Locator: The id of the element to match
# * Locator: (String, Regexp) The id of the element to match ()
#
# * **:field** - Select field elements (input [not of type submit, image, or hidden], textarea, select)
# * Locator: Matches against the id, Capybara.test_id attribute, name, or placeholder
Expand Down Expand Up @@ -191,6 +191,7 @@ def initialize(name, &block)
@format = nil
@expression = nil
@expression_filters = {}
@locator_filter = nil
@default_visibility = nil
@config = {
enable_aria_label: false,
Expand Down Expand Up @@ -347,6 +348,11 @@ def match?(locator)

def_delegators :@filter_set, :node_filter, :expression_filter, :filter

def locator_filter(&block)
@locator_filter = block if block
@locator_filter
end

def filter_set(name, filters_to_use = nil)
@filter_set.import(name, filters_to_use)
end
Expand Down
3 changes: 3 additions & 0 deletions spec/selector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@
describe ':id selector' do
it 'finds by locator' do
expect(string.find(:id, 'my_text_input')[:name]).to eq 'form[my_text_input]'
expect(string.find(:id, /my_text_input/)[:name]).to eq 'form[my_text_input]'
expect(string.find(:id, /_text_/)[:name]).to eq 'form[my_text_input]'
expect(string.find(:id, /i[nmo]/)[:name]).to eq 'form[my_text_input]'
end
end

Expand Down

0 comments on commit a47b9fd

Please sign in to comment.