Skip to content

Commit ee29dab

Browse files
committed
Refactor errors implementation
* remove w3c_error.rb * remove useless aliases * simplify .from_code * add some comments to quirky parts of OSS vs. W3C errors
1 parent da07c49 commit ee29dab

File tree

3 files changed

+64
-252
lines changed

3 files changed

+64
-252
lines changed

rb/lib/selenium/webdriver/common.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
require 'selenium/webdriver/common/w3c_error'
1918
require 'selenium/webdriver/common/error'
2019
require 'selenium/webdriver/common/platform'
2120
require 'selenium/webdriver/common/proxy'

rb/lib/selenium/webdriver/common/error.rb

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# encoding: utf-8
2-
#
31
# Licensed to the Software Freedom Conservancy (SFC) under one
42
# or more contributor license agreements. See the NOTICE file
53
# distributed with this work for additional information
@@ -20,14 +18,27 @@
2018
module Selenium
2119
module WebDriver
2220
module Error
23-
class WebDriverError < StandardError; end
2421

2522
#
26-
# Indicates that a command that should have executed properly cannot be supported for some
27-
# reason.
23+
# Returns exception from code (Integer - OSS, String - W3C).
24+
# @param [Integer, String, nil] code
2825
#
2926

30-
class UnsupportedOperationError < WebDriverError; end
27+
def self.for_code(code)
28+
case code
29+
when nil, 0
30+
nil
31+
when Integer
32+
ERRORS.fetch(code)
33+
when String
34+
klass_name = code.split(' ').map(&:capitalize).join.sub(/Error$/, '')
35+
const_get("#{klass_name}Error")
36+
end
37+
rescue NameError
38+
WebDriverError
39+
end
40+
41+
class WebDriverError < StandardError; end
3142

3243
class IndexOutOfBoundsError < WebDriverError; end # 1
3344
class NoCollectionError < WebDriverError; end # 2
@@ -107,6 +118,7 @@ class NoSuchCollectionError < WebDriverError; end # 20
107118
#
108119

109120
class TimeOutError < WebDriverError; end # 21
121+
110122
class NullPointerError < WebDriverError; end # 22
111123
class NoSuchWindowError < WebDriverError; end # 23
112124

@@ -117,7 +129,7 @@ class NoSuchWindowError < WebDriverError; end # 23
117129
class InvalidCookieDomainError < WebDriverError; end # 24
118130

119131
#
120-
# A command to set a cookies value could not be satisfied.
132+
# A command to set a cookie's value could not be satisfied.
121133
#
122134

123135
class UnableToSetCookieError < WebDriverError; end # 25
@@ -128,10 +140,17 @@ class UnableToSetCookieError < WebDriverError; end # 25
128140
class UnhandledAlertError < WebDriverError; end # 26
129141

130142
#
131-
# Indicates that a user has tried to access an alert when one is not present.
143+
# An attempt was made to operate on a modal dialog when one was not open:
144+
#
145+
# * W3C dialect is NoSuchAlertError
146+
# * OSS dialect is NoAlertPresentError
147+
#
148+
# We want to allow clients to rescue NoSuchAlertError as a superclass for
149+
# dialect-agnostic implementation, so NoAlertPresentError should inherit from it.
132150
#
133151

134-
class NoAlertPresentError < WebDriverError; end # 27
152+
class NoSuchAlertError < WebDriverError; end
153+
class NoAlertPresentError < NoSuchAlertError; end # 27
135154

136155
#
137156
# A script did not complete before its timeout expired.
@@ -171,7 +190,7 @@ class InvalidSelectorError < WebDriverError; end # 32
171190
class SessionNotCreatedError < WebDriverError; end # 33
172191

173192
#
174-
# The target for mouse interaction is not in the browsers viewport and cannot be brought
193+
# The target for mouse interaction is not in the browser's viewport and cannot be brought
175194
# into that viewport.
176195
#
177196

@@ -181,48 +200,42 @@ class MoveTargetOutOfBoundsError < WebDriverError; end # 34
181200
# Indicates that the XPath selector is invalid
182201
#
183202

184-
class InvalidXpathSelectorError < WebDriverError; end # 51
185-
class InvalidXpathSelectorReturnTyperError < WebDriverError; end # 52
203+
class InvalidXpathSelectorError < WebDriverError; end
204+
class InvalidXpathSelectorReturnTyperError < WebDriverError; end
186205

187206
#
188207
# A command could not be completed because the element is not pointer or keyboard
189208
# interactable.
190209
#
191210

192-
class ElementNotInteractableError < WebDriverError; end # 60
211+
class ElementNotInteractableError < WebDriverError; end
193212

194213
#
195214
# The arguments passed to a command are either invalid or malformed.
196215
#
197216

198-
class InvalidArgumentError < WebDriverError; end # 61
217+
class InvalidArgumentError < WebDriverError; end
199218

200219
#
201220
# No cookie matching the given path name was found amongst the associated cookies of the
202-
# current browsing contexts active document.
221+
# current browsing context's active document.
203222
#
204223

205-
class NoSuchCookieError < WebDriverError; end # 62
224+
class NoSuchCookieError < WebDriverError; end
206225

207226
#
208227
# A screen capture was made impossible.
209228
#
210229

211-
class UnableToCaptureScreenError < WebDriverError; end # 63
230+
class UnableToCaptureScreenError < WebDriverError; end
212231

213232
#
214233
# Occurs if the given session id is not in the list of active sessions, meaning the session
215-
# either does not exist or that its not active.
234+
# either does not exist or that it's not active.
216235
#
217236

218237
class InvalidSessionIdError < WebDriverError; end
219238

220-
#
221-
# An attempt was made to operate on a modal dialog when one was not open.
222-
#
223-
224-
class NoSuchAlertError < WebDriverError; end
225-
226239
#
227240
# A modal dialog was open, blocking this operation.
228241
#
@@ -242,26 +255,37 @@ class UnknownMethodError < WebDriverError; end
242255

243256
class ElementClickInterceptedError < WebDriverError; end
244257

245-
# aliased for backwards compatibility
246-
NoAlertPresentError = NoSuchAlertError
247-
ScriptTimeOutError = ScriptTimeoutError
258+
#
259+
# Indicates that a command that should have executed properly cannot be supported for some
260+
# reason.
261+
#
262+
263+
class UnsupportedOperationError < WebDriverError; end
264+
265+
# Aliases for OSS dialect.
266+
ScriptTimeoutError = ScriptTimeOutError
267+
NoAlertOpenError = NoAlertPresentError
268+
269+
# Aliases for backwards compatibility.
248270
ObsoleteElementError = StaleElementReferenceError
249271
UnhandledError = UnknownError
250272
UnexpectedJavascriptError = JavascriptError
251-
NoAlertOpenError = NoAlertPresentError
252273
ElementNotDisplayedError = ElementNotVisibleError
253274

275+
#
254276
# @api private
277+
#
278+
255279
ERRORS = {
256-
1 => IndexOutOfBoundsError,
257-
2 => NoCollectionError,
258-
3 => NoStringError,
259-
4 => NoStringLengthError,
260-
5 => NoStringWrapperError,
261-
6 => NoSuchDriverError,
262-
7 => NoSuchElementError,
263-
8 => NoSuchFrameError,
264-
9 => UnknownCommandError,
280+
1 => IndexOutOfBoundsError,
281+
2 => NoCollectionError,
282+
3 => NoStringError,
283+
4 => NoStringLengthError,
284+
5 => NoStringWrapperError,
285+
6 => NoSuchDriverError,
286+
7 => NoSuchElementError,
287+
8 => NoSuchFrameError,
288+
9 => UnknownCommandError,
265289
10 => StaleElementReferenceError,
266290
11 => ElementNotVisibleError,
267291
12 => InvalidElementStateError,
@@ -287,6 +311,8 @@ class ElementClickInterceptedError < WebDriverError; end
287311
32 => InvalidSelectorError,
288312
33 => SessionNotCreatedError,
289313
34 => MoveTargetOutOfBoundsError,
314+
# The following are W3C-specific errors,
315+
# they don't really need error codes, we just make them up!
290316
51 => InvalidXpathSelectorError,
291317
52 => InvalidXpathSelectorReturnTyperError,
292318
60 => ElementNotInteractableError,
@@ -295,17 +321,6 @@ class ElementClickInterceptedError < WebDriverError; end
295321
63 => UnableToCaptureScreenError
296322
}.freeze
297323

298-
class << self
299-
def for_code(code)
300-
return if [nil, 0].include? code
301-
return ERRORS.fetch(code) if code.is_a? Integer
302-
303-
klass_name = code.split(' ').map(&:capitalize).join
304-
Error.const_get("#{klass_name.gsub('Error', '')}Error")
305-
rescue NameError
306-
WebDriverError
307-
end
308-
end
309324
end # Error
310325
end # WebDriver
311-
end # Selenium
326+
end # Selenium

0 commit comments

Comments
 (0)