Skip to content

Commit

Permalink
[rb] Support registering custom finders for SearchContext
Browse files Browse the repository at this point in the history
  • Loading branch information
p0deje committed May 15, 2024
1 parent 991a653 commit 02381bf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
12 changes: 10 additions & 2 deletions rb/lib/selenium/webdriver/common/search_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ module SearchContext
xpath: 'xpath'
}.freeze

class << self
attr_accessor :extra_finders

def finders
FINDERS.merge(extra_finders || {})
end
end

#
# Find the first element matching the given arguments
#
Expand All @@ -57,7 +65,7 @@ module SearchContext
def find_element(*args)
how, what = extract_args(args)

by = FINDERS[how.to_sym]
by = SearchContext.finders[how.to_sym]
raise ArgumentError, "cannot find element by #{how.inspect}" unless by

bridge.find_element_by by, what, ref
Expand All @@ -72,7 +80,7 @@ def find_element(*args)
def find_elements(*args)
how, what = extract_args(args)

by = FINDERS[how.to_sym]
by = SearchContext.finders[how.to_sym]
raise ArgumentError, "cannot find elements by #{how.inspect}" unless by

bridge.find_elements_by by, what, ref
Expand Down
16 changes: 16 additions & 0 deletions rb/spec/unit/selenium/webdriver/search_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ def initialize(bridge)
}.to raise_error(ArgumentError, 'cannot find elements by :foo')
end
end

context 'when extra finders are registered' do
around do |example|
described_class.extra_finders = {accessibility_id: 'accessibility id'}
example.call
ensure
described_class.extra_finders = nil
end

it 'finds element' do
allow(bridge).to receive(:find_element_by).with('accessibility id', 'foo', nil).and_return(element)

expect(search_context.find_element(accessibility_id: 'foo')).to eq(element)
expect(bridge).to have_received(:find_element_by).with('accessibility id', 'foo', nil)
end
end
end
end # WebDriver
end # Selenium

0 comments on commit 02381bf

Please sign in to comment.