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

Allow indexing directly on Capybara::Selector

This commit is contained in:
Thomas Walpole 2018-11-16 10:15:26 -08:00
parent e786349694
commit 0bd232b3bb
6 changed files with 12 additions and 8 deletions

View file

@ -135,11 +135,11 @@ module Capybara
def find_selector(locator) def find_selector(locator)
selector = if locator.is_a?(Symbol) selector = if locator.is_a?(Symbol)
Selector.all.fetch(locator) { |sel_type| raise ArgumentError, "Unknown selector type (:#{sel_type})" } Selector[locator] { |sel_type| raise ArgumentError, "Unknown selector type (:#{sel_type})" }
else else
Selector.all.values.find { |sel| sel.match?(locator) } Selector.all.values.find { |sel| sel.match?(locator) }
end end
selector || Selector.all[session_options.default_selector] selector || Selector[session_options.default_selector]
end end
def find_nodes_by_selector_format(node, exact) def find_nodes_by_selector_format(node, exact)

View file

@ -169,12 +169,16 @@ module Capybara
@selectors ||= {} # rubocop:disable Naming/MemoizedInstanceVariableName @selectors ||= {} # rubocop:disable Naming/MemoizedInstanceVariableName
end end
def [](name, &block)
all.fetch(name.to_sym, &block)
end
def add(name, &block) def add(name, &block)
all[name.to_sym] = Capybara::Selector.new(name.to_sym, &block) all[name.to_sym] = Capybara::Selector.new(name.to_sym, &block)
end end
def update(name, &block) def update(name, &block)
all[name.to_sym].instance_eval(&block) self[name.to_sym].instance_eval(&block)
end end
def remove(name) def remove(name)

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.all[:fillable_field].call('Name') @xpath = Capybara::Selector[:fillable_field].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

@ -224,7 +224,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.all[:fillable_field].call('First Name') @xpath = Capybara::Selector[:fillable_field].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.all[:fillable_field].call('First Name') @xpath = Capybara::Selector[:fillable_field].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

@ -423,12 +423,12 @@ RSpec.describe Capybara do
expression_filter(:random) { |xpath, _| xpath } # do nothing filter expression_filter(:random) { |xpath, _| xpath } # do nothing filter
end end
example.run example.run
Capybara::Selector.all[:link_or_button].expression_filters.delete(:random) Capybara::Selector[:link_or_button].expression_filters.delete(:random)
end end
context 'when modified' do context 'when modified' do
it 'should still work' do it 'should still work' do
filter = Capybara::Selector.all[:link_or_button].expression_filters[:random] filter = Capybara::Selector[:link_or_button].expression_filters[:random]
allow(filter).to receive(:apply_filter).and_call_original allow(filter).to receive(:apply_filter).and_call_original
expect(string.find(:link_or_button, 'click me', random: 'blah').value).to eq 'click me' expect(string.find(:link_or_button, 'click me', random: 'blah').value).to eq 'click me'