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:
parent
e786349694
commit
0bd232b3bb
6 changed files with 12 additions and 8 deletions
|
@ -135,11 +135,11 @@ module Capybara
|
|||
|
||||
def find_selector(locator)
|
||||
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
|
||||
Selector.all.values.find { |sel| sel.match?(locator) }
|
||||
end
|
||||
selector || Selector.all[session_options.default_selector]
|
||||
selector || Selector[session_options.default_selector]
|
||||
end
|
||||
|
||||
def find_nodes_by_selector_format(node, exact)
|
||||
|
|
|
@ -169,12 +169,16 @@ module Capybara
|
|||
@selectors ||= {} # rubocop:disable Naming/MemoizedInstanceVariableName
|
||||
end
|
||||
|
||||
def [](name, &block)
|
||||
all.fetch(name.to_sym, &block)
|
||||
end
|
||||
|
||||
def add(name, &block)
|
||||
all[name.to_sym] = Capybara::Selector.new(name.to_sym, &block)
|
||||
end
|
||||
|
||||
def update(name, &block)
|
||||
all[name.to_sym].instance_eval(&block)
|
||||
self[name.to_sym].instance_eval(&block)
|
||||
end
|
||||
|
||||
def remove(name)
|
||||
|
|
|
@ -30,7 +30,7 @@ Capybara::SpecHelper.spec '#all' do
|
|||
|
||||
it 'should accept an XPath instance', :exact_false do
|
||||
@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)
|
||||
@result = @session.all(@xpath).map(&:value)
|
||||
expect(@result).to include('Smith', 'John', 'John Smith')
|
||||
|
|
|
@ -224,7 +224,7 @@ Capybara::SpecHelper.spec '#find' do
|
|||
|
||||
it 'should accept an XPath instance' do
|
||||
@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(@session.find(@xpath).value).to eq('John')
|
||||
end
|
||||
|
|
|
@ -24,7 +24,7 @@ Capybara::SpecHelper.spec '#first' do
|
|||
|
||||
it 'should accept an XPath instance' do
|
||||
@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(@session.first(@xpath).value).to eq('John')
|
||||
end
|
||||
|
|
|
@ -423,12 +423,12 @@ RSpec.describe Capybara do
|
|||
expression_filter(:random) { |xpath, _| xpath } # do nothing filter
|
||||
end
|
||||
example.run
|
||||
Capybara::Selector.all[:link_or_button].expression_filters.delete(:random)
|
||||
Capybara::Selector[:link_or_button].expression_filters.delete(:random)
|
||||
end
|
||||
|
||||
context 'when modified' 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
|
||||
|
||||
expect(string.find(:link_or_button, 'click me', random: 'blah').value).to eq 'click me'
|
||||
|
|
Loading…
Add table
Reference in a new issue