Skip to content

Commit 8a69bf5

Browse files
committed
[rb] Support registering custom finders for SearchContext
1 parent 2a57bd1 commit 8a69bf5

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

rb/lib/selenium/webdriver/common/search_context.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ module SearchContext
3535
xpath: 'xpath'
3636
}.freeze
3737

38+
class << self
39+
attr_accessor :extra_finders
40+
41+
def finders
42+
FINDERS.merge(extra_finders || {})
43+
end
44+
end
45+
3846
#
3947
# Find the first element matching the given arguments
4048
#
@@ -57,7 +65,7 @@ module SearchContext
5765
def find_element(*args)
5866
how, what = extract_args(args)
5967

60-
by = FINDERS[how.to_sym]
68+
by = SearchContext.finders[how.to_sym]
6169
raise ArgumentError, "cannot find element by #{how.inspect}" unless by
6270

6371
bridge.find_element_by by, what, ref
@@ -72,7 +80,7 @@ def find_element(*args)
7280
def find_elements(*args)
7381
how, what = extract_args(args)
7482

75-
by = FINDERS[how.to_sym]
83+
by = SearchContext.finders[how.to_sym]
7684
raise ArgumentError, "cannot find elements by #{how.inspect}" unless by
7785

7886
bridge.find_elements_by by, what, ref

rb/spec/unit/selenium/webdriver/search_context_spec.rb

+16
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ def initialize(bridge)
8989
}.to raise_error(ArgumentError, 'cannot find elements by :foo')
9090
end
9191
end
92+
93+
context 'when extra finders are registered' do
94+
around do |example|
95+
described_class.extra_finders = {accessibility_id: 'accessibility id'}
96+
example.call
97+
ensure
98+
described_class.extra_finders = nil
99+
end
100+
101+
it 'finds element' do
102+
allow(bridge).to receive(:find_element_by).with('accessibility id', 'foo', nil).and_return(element)
103+
104+
expect(search_context.find_element(accessibility_id: 'foo')).to eq(element)
105+
expect(bridge).to have_received(:find_element_by).with('accessibility id', 'foo', nil)
106+
end
107+
end
92108
end
93109
end # WebDriver
94110
end # Selenium

0 commit comments

Comments
 (0)