From 8a69bf5ca9600f5a6258ddc0ca344c779a011dc3 Mon Sep 17 00:00:00 2001 From: Alex Rodionov Date: Sun, 4 Jun 2023 16:19:54 -0700 Subject: [PATCH] [rb] Support registering custom finders for SearchContext --- .../selenium/webdriver/common/search_context.rb | 12 ++++++++++-- .../selenium/webdriver/search_context_spec.rb | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/rb/lib/selenium/webdriver/common/search_context.rb b/rb/lib/selenium/webdriver/common/search_context.rb index 6bbc405333888..dad7896cbc907 100644 --- a/rb/lib/selenium/webdriver/common/search_context.rb +++ b/rb/lib/selenium/webdriver/common/search_context.rb @@ -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 # @@ -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 @@ -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 diff --git a/rb/spec/unit/selenium/webdriver/search_context_spec.rb b/rb/spec/unit/selenium/webdriver/search_context_spec.rb index 88cd544b90d57..167fa51a410d5 100644 --- a/rb/spec/unit/selenium/webdriver/search_context_spec.rb +++ b/rb/spec/unit/selenium/webdriver/search_context_spec.rb @@ -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