diff --git a/rb/lib/selenium/webdriver/common/error.rb b/rb/lib/selenium/webdriver/common/error.rb index 69adfe4e2fe35..7e3de39f2d6f4 100644 --- a/rb/lib/selenium/webdriver/common/error.rb +++ b/rb/lib/selenium/webdriver/common/error.rb @@ -37,17 +37,29 @@ def self.for_error(error) SUPPORT_MSG = 'For documentation on this error, please visit:' ERROR_URL = 'https://www.selenium.dev/documentation/webdriver/troubleshooting/errors' - class WebDriverError < StandardError; end + URLS = { + NoSuchElementError: "#{ERROR_URL}#no-such-element-exception", + StaleElementReferenceError: "#{ERROR_URL}#stale-element-reference-exception", + InvalidSelectorError: "#{ERROR_URL}#invalid-selector-exception", + NoSuchDriverError: "#{ERROR_URL}/driver_location" + }.freeze + + class WebDriverError < StandardError + def initialize(msg = '') + # Remove this conditional when all the error pages have been documented + super(URLS[class_name] ? "#{msg}; #{SUPPORT_MSG} #{URLS[class_name]}" : msg) + end + + def class_name + self.class.name&.split('::')&.last&.to_sym + end + end # # An element could not be located on the page using the given search parameters. # - class NoSuchElementError < WebDriverError - def initialize(msg = '') - super("#{msg}; #{SUPPORT_MSG} #{ERROR_URL}#no-such-element-exception") - end - end + class NoSuchElementError < WebDriverError; end # # A command to switch to a frame could not be satisfied because the frame could not be found. @@ -67,7 +79,7 @@ class UnknownCommandError < WebDriverError; end class StaleElementReferenceError < WebDriverError def initialize(msg = '') - super("#{msg}; #{SUPPORT_MSG} #{ERROR_URL}#stale-element-reference-exception") + super("#{msg}; #{SUPPORT_MSG} #{URLS[:StaleElementReferenceError]}") end end @@ -143,11 +155,7 @@ class ScriptTimeoutError < WebDriverError; end # Argument was an invalid selector. # - class InvalidSelectorError < WebDriverError - def initialize(msg = '') - super("#{msg}; #{SUPPORT_MSG} #{ERROR_URL}#invalid-selector-exception") - end - end + class InvalidSelectorError < WebDriverError; end # # A new session could not be created. @@ -232,11 +240,7 @@ class UnsupportedOperationError < WebDriverError; end # Indicates that driver was not specified and could not be located. # - class NoSuchDriverError < WebDriverError - def initialize(msg = '') - super("#{msg}; #{SUPPORT_MSG} #{ERROR_URL}/driver_location") - end - end + class NoSuchDriverError < WebDriverError; end end # Error end # WebDriver end # Selenium diff --git a/rb/sig/lib/selenium/webdriver/common/error.rbs b/rb/sig/lib/selenium/webdriver/common/error.rbs index cf084eaa6a4c3..16c7caf9d033a 100644 --- a/rb/sig/lib/selenium/webdriver/common/error.rbs +++ b/rb/sig/lib/selenium/webdriver/common/error.rbs @@ -7,7 +7,10 @@ module Selenium ERROR_URL: String + URLS: Hash[Symbol, String] + class WebDriverError < StandardError + def class_name: -> Symbol? end class NoSuchElementError < WebDriverError diff --git a/rb/spec/integration/selenium/webdriver/error_spec.rb b/rb/spec/integration/selenium/webdriver/error_spec.rb index ab168263e2b6e..ad5fd3ae0ff65 100644 --- a/rb/spec/integration/selenium/webdriver/error_spec.rb +++ b/rb/spec/integration/selenium/webdriver/error_spec.rb @@ -27,7 +27,7 @@ module WebDriver expect { driver.find_element(id: 'nonexistent') - }.to raise_error(WebDriver::Error::NoSuchElementError) + }.to raise_error(WebDriver::Error::NoSuchElementError, /#no-such-element-exception/) end end end # WebDriver diff --git a/rb/spec/unit/selenium/webdriver/common/driver_finder_spec.rb b/rb/spec/unit/selenium/webdriver/common/driver_finder_spec.rb index fafa0803f61f1..175c191c09763 100644 --- a/rb/spec/unit/selenium/webdriver/common/driver_finder_spec.rb +++ b/rb/spec/unit/selenium/webdriver/common/driver_finder_spec.rb @@ -63,8 +63,7 @@ module WebDriver expect { described_class.new(Options.chrome, Service.chrome).driver_path }.to output(/Exception occurred: this error/).to_stderr_from_any_process - }.to raise_error(WebDriver::Error::NoSuchDriverError, - /Unable to obtain chromedriver; For documentation on this error/) + }.to raise_error(WebDriver::Error::NoSuchDriverError, /driver_location/) end it 'creates arguments' do