Skip to content

Commit

Permalink
[rb] add links to documentation for error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Jun 6, 2023
1 parent dc9ab40 commit fb21cac
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
21 changes: 18 additions & 3 deletions rb/lib/selenium/webdriver/common/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,20 @@ def self.for_error(error)
WebDriverError
end

SUPPORT_MSG = 'For documentation on this error, please visit:'
ERROR_URL = 'https://www.selenium.dev/documentation/webdriver/troubleshooting/errors'

class WebDriverError < StandardError; end

#
# An element could not be located on the page using the given search parameters.
#

class NoSuchElementError < WebDriverError; end
class NoSuchElementError < WebDriverError
def initialize(msg = '')
super("#{msg}; #{SUPPORT_MSG} #{ERROR_URL}#no-such-element-exception")
end
end

#
# A command to switch to a frame could not be satisfied because the frame could not be found.
Expand All @@ -58,7 +65,11 @@ class UnknownCommandError < WebDriverError; end
# A command failed because the referenced element is no longer attached to the DOM.
#

class StaleElementReferenceError < WebDriverError; end
class StaleElementReferenceError < WebDriverError
def initialize(msg = '')
super("#{msg}; #{SUPPORT_MSG} #{ERROR_URL}#stale-element-reference-exception")
end
end

#
# A command failed because the referenced shadow root is no longer attached to the DOM.
Expand Down Expand Up @@ -132,7 +143,11 @@ class ScriptTimeoutError < WebDriverError; end
# Argument was an invalid selector.
#

class InvalidSelectorError < WebDriverError; end
class InvalidSelectorError < WebDriverError
def initialize(msg = '')
super("#{msg}; #{SUPPORT_MSG} #{ERROR_URL}#invalid-selector-exception")
end
end

#
# A new session could not be created.
Expand Down
14 changes: 14 additions & 0 deletions rb/spec/integration/selenium/webdriver/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ module WebDriver
expect(driver[:id1]).to be_a(WebDriver::Element)
expect(driver[xpath: '//h1']).to be_a(WebDriver::Element)
end

it 'raises if element not found' do
driver.navigate.to url_for('xhtmlTest.html')
expect {
driver.find_element(id: 'not-there')
}.to raise_error(Error::NoSuchElementError, /errors#no-such-element-exception/)
end

it 'raises if invalid locator' do
driver.navigate.to url_for('xhtmlTest.html')
expect {
driver.find_element(xpath: '*?//-')
}.to raise_error(Error::InvalidSelectorError, /errors#invalid-selector-exception/)
end
end

describe 'many elements' do
Expand Down
11 changes: 11 additions & 0 deletions rb/spec/integration/selenium/webdriver/element_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ module WebDriver
expect { driver.find_element(id: 'other_contents').click }.to raise_error(Error::ElementClickInterceptedError)
end

it 'raises if element stale' do
driver.navigate.to url_for('formPage.html')
button = driver.find_element(id: 'imageButton')
driver.navigate.refresh

expect { button.click }.to raise_exception(Error::StaleElementReferenceError,
/errors#stale-element-reference-exception/)

reset_driver!(time: 1) if %i[safari safari_preview].include? GlobalTestEnv.browser
end

describe '#submit' do
it 'valid submit button' do
driver.navigate.to url_for('formPage.html')
Expand Down

0 comments on commit fb21cac

Please sign in to comment.