Skip to content

Commit d228f6f

Browse files
committed
Remove OSS dialect errors
As a part of initiative to remove OSS dialect and support only W3C, all the errors not compliant to specification were removed. It seems like server/grid passes through all the errors from drivers without altering them (with the only exception of custom error when trying to upload multiple files to server using POST /se/file - it's handled just fine by Ruby bindings).
1 parent 30a2028 commit d228f6f

File tree

5 files changed

+48
-258
lines changed

5 files changed

+48
-258
lines changed

Diff for: rb/lib/selenium/webdriver/common/error.rb

+28-157
Original file line numberDiff line numberDiff line change
@@ -20,188 +20,119 @@ module WebDriver
2020
module Error
2121

2222
#
23-
# Returns exception from code (Integer - OSS, String - W3C).
24-
# @param [Integer, String, nil] code
25-
#
26-
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", false)
36-
end
37-
rescue KeyError, NameError
23+
# Returns exception from its string representation.
24+
# @param [String, nil] error
25+
#
26+
27+
def self.for_error(error)
28+
return if error.nil?
29+
30+
klass_name = error.split(' ').map(&:capitalize).join.sub(/Error$/, '')
31+
const_get("#{klass_name}Error", false)
32+
rescue NameError
3833
WebDriverError
3934
end
4035

4136
class WebDriverError < StandardError; end
4237

43-
class IndexOutOfBoundsError < WebDriverError; end # 1
44-
class NoCollectionError < WebDriverError; end # 2
45-
class NoStringError < WebDriverError; end # 3
46-
class NoStringLengthError < WebDriverError; end # 4
47-
class NoStringWrapperError < WebDriverError; end # 5
48-
class NoSuchDriverError < WebDriverError; end # 6
49-
5038
#
5139
# An element could not be located on the page using the given search parameters.
5240
#
5341

54-
class NoSuchElementError < WebDriverError; end # 7
42+
class NoSuchElementError < WebDriverError; end
5543

5644
#
5745
# A command to switch to a frame could not be satisfied because the frame could not be found.
5846
#
5947

60-
class NoSuchFrameError < WebDriverError; end # 8
48+
class NoSuchFrameError < WebDriverError; end
6149

6250
#
6351
# A command could not be executed because the remote end is not aware of it.
6452
#
6553

66-
class UnknownCommandError < WebDriverError; end # 9
54+
class UnknownCommandError < WebDriverError; end
6755

6856
#
6957
# A command failed because the referenced element is no longer attached to the DOM.
7058
#
7159

72-
class StaleElementReferenceError < WebDriverError; end # 10
73-
74-
#
75-
# Raised to indicate that although an element is present on the DOM, it is not visible, and
76-
# so is not able to be interacted with.
77-
#
78-
79-
class ElementNotVisibleError < WebDriverError; end # 11
60+
class StaleElementReferenceError < WebDriverError; end
8061

8162
#
8263
# The target element is in an invalid state, rendering it impossible to interact with, for
8364
# example if you click a disabled element.
8465
#
8566

86-
class InvalidElementStateError < WebDriverError; end # 12
67+
class InvalidElementStateError < WebDriverError; end
8768

8869
#
8970
# An unknown error occurred in the remote end while processing the command.
9071
#
9172

92-
class UnknownError < WebDriverError; end # 13
93-
class ExpectedError < WebDriverError; end # 14
94-
95-
#
96-
# An attempt was made to select an element that cannot be selected.
97-
#
98-
99-
class ElementNotSelectableError < WebDriverError; end # 15
100-
class NoSuchDocumentError < WebDriverError; end # 16
73+
class UnknownError < WebDriverError; end
10174

10275
#
10376
# An error occurred while executing JavaScript supplied by the user.
10477
#
10578

106-
class JavascriptError < WebDriverError; end # 17
107-
class NoScriptResultError < WebDriverError; end # 18
79+
class JavascriptError < WebDriverError; end
10880

10981
#
110-
# An error occurred while searching for an element by XPath.
82+
# An operation did not complete before its timeout expired.
11183
#
11284

113-
class XPathLookupError < WebDriverError; end # 19
114-
class NoSuchCollectionError < WebDriverError; end # 20
85+
class TimeOutError < WebDriverError; end
11586

11687
#
117-
# An operation did not complete before its timeout expired.
88+
# A command to switch to a window could not be satisfied because
89+
# the window could not be found.
11890
#
11991

120-
class TimeOutError < WebDriverError; end # 21
121-
122-
class NullPointerError < WebDriverError; end # 22
123-
class NoSuchWindowError < WebDriverError; end # 23
92+
class NoSuchWindowError < WebDriverError; end
12493

12594
#
12695
# An illegal attempt was made to set a cookie under a different domain than the current page.
12796
#
12897

129-
class InvalidCookieDomainError < WebDriverError; end # 24
98+
class InvalidCookieDomainError < WebDriverError; end
13099

131100
#
132101
# A command to set a cookie's value could not be satisfied.
133102
#
134103

135-
class UnableToSetCookieError < WebDriverError; end # 25
136-
137-
#
138-
# Raised when an alert dialog is present that has not been dealt with.
139-
#
140-
class UnhandledAlertError < WebDriverError; end # 26
104+
class UnableToSetCookieError < WebDriverError; end
141105

142106
#
143107
# An attempt was made to operate on a modal dialog when one was not open:
144108
#
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.
150-
#
151109

152110
class NoSuchAlertError < WebDriverError; end
153-
class NoAlertPresentError < NoSuchAlertError; end # 27
154111

155112
#
156113
# A script did not complete before its timeout expired.
157114
#
158115

159-
class ScriptTimeOutError < WebDriverError; end # 28
160-
161-
#
162-
# The coordinates provided to an interactions operation are invalid.
163-
#
164-
165-
class InvalidElementCoordinatesError < WebDriverError; end # 29
166-
167-
#
168-
# Indicates that IME support is not available. This exception is rasied for every IME-related
169-
# method call if IME support is not available on the machine.
170-
#
171-
172-
class IMENotAvailableError < WebDriverError; end # 30
173-
174-
#
175-
# Indicates that activating an IME engine has failed.
176-
#
177-
178-
class IMEEngineActivationFailedError < WebDriverError; end # 31
116+
class ScriptTimeoutError < WebDriverError; end
179117

180118
#
181119
# Argument was an invalid selector.
182120
#
183121

184-
class InvalidSelectorError < WebDriverError; end # 32
122+
class InvalidSelectorError < WebDriverError; end
185123

186124
#
187125
# A new session could not be created.
188126
#
189127

190-
class SessionNotCreatedError < WebDriverError; end # 33
128+
class SessionNotCreatedError < WebDriverError; end
191129

192130
#
193131
# The target for mouse interaction is not in the browser's viewport and cannot be brought
194132
# into that viewport.
195133
#
196134

197-
class MoveTargetOutOfBoundsError < WebDriverError; end # 34
198-
199-
#
200-
# Indicates that the XPath selector is invalid
201-
#
202-
203-
class InvalidXpathSelectorError < WebDriverError; end
204-
class InvalidXpathSelectorReturnTyperError < WebDriverError; end
135+
class MoveTargetOutOfBoundsError < WebDriverError; end
205136

206137
#
207138
# A command could not be completed because the element is not pointer or keyboard
@@ -269,66 +200,6 @@ class ElementClickInterceptedError < WebDriverError; end
269200

270201
class UnsupportedOperationError < WebDriverError; end
271202

272-
# Aliases for OSS dialect.
273-
ScriptTimeoutError = ScriptTimeOutError
274-
TimeoutError = TimeOutError
275-
NoAlertOpenError = NoAlertPresentError
276-
277-
# Aliases for backwards compatibility.
278-
ObsoleteElementError = StaleElementReferenceError
279-
UnhandledError = UnknownError
280-
UnexpectedJavascriptError = JavascriptError
281-
ElementNotDisplayedError = ElementNotVisibleError
282-
283-
#
284-
# @api private
285-
#
286-
287-
ERRORS = {
288-
1 => IndexOutOfBoundsError,
289-
2 => NoCollectionError,
290-
3 => NoStringError,
291-
4 => NoStringLengthError,
292-
5 => NoStringWrapperError,
293-
6 => NoSuchDriverError,
294-
7 => NoSuchElementError,
295-
8 => NoSuchFrameError,
296-
9 => UnknownCommandError,
297-
10 => StaleElementReferenceError,
298-
11 => ElementNotVisibleError,
299-
12 => InvalidElementStateError,
300-
13 => UnknownError,
301-
14 => ExpectedError,
302-
15 => ElementNotSelectableError,
303-
16 => NoSuchDocumentError,
304-
17 => JavascriptError,
305-
18 => NoScriptResultError,
306-
19 => XPathLookupError,
307-
20 => NoSuchCollectionError,
308-
21 => TimeOutError,
309-
22 => NullPointerError,
310-
23 => NoSuchWindowError,
311-
24 => InvalidCookieDomainError,
312-
25 => UnableToSetCookieError,
313-
26 => UnhandledAlertError,
314-
27 => NoAlertPresentError,
315-
28 => ScriptTimeOutError,
316-
29 => InvalidElementCoordinatesError,
317-
30 => IMENotAvailableError,
318-
31 => IMEEngineActivationFailedError,
319-
32 => InvalidSelectorError,
320-
33 => SessionNotCreatedError,
321-
34 => MoveTargetOutOfBoundsError,
322-
# The following are W3C-specific errors,
323-
# they don't really need error codes, we just make them up!
324-
51 => InvalidXpathSelectorError,
325-
52 => InvalidXpathSelectorReturnTyperError,
326-
60 => ElementNotInteractableError,
327-
61 => InvalidArgumentError,
328-
62 => NoSuchCookieError,
329-
63 => UnableToCaptureScreenError
330-
}.freeze
331-
332203
end # Error
333204
end # WebDriver
334205
end # Selenium

Diff for: rb/lib/selenium/webdriver/remote/response.rb

+16-42
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
module Selenium
1919
module WebDriver
2020
module Remote
21+
22+
#
2123
# @api private
22-
class Response
23-
STACKTRACE_KEY = 'stackTrace'.freeze
24+
#
2425

26+
class Response
2527
attr_reader :code, :payload
26-
attr_writer :payload
2728

2829
def initialize(code, payload = nil)
2930
@code = code
@@ -33,32 +34,16 @@ def initialize(code, payload = nil)
3334
end
3435

3536
def error
36-
klass = Error.for_code(status) || return
37+
error, message, backtrace = process_error
38+
klass = Error.for_error(error) || return
3739

38-
ex = klass.new(error_message)
40+
ex = klass.new(message)
3941
ex.set_backtrace(caller)
40-
add_backtrace ex
42+
add_backtrace ex, backtrace
4143

4244
ex
4345
end
4446

45-
def error_message
46-
val = value
47-
48-
case val
49-
when Hash
50-
msg = val['message']
51-
return 'unknown error' unless msg
52-
msg << ": #{val['alert']['text'].inspect}" if val['alert'].is_a?(Hash) && val['alert']['text']
53-
msg << " (#{val['class']})" if val['class']
54-
msg
55-
when String
56-
val
57-
else
58-
"unknown error, status=#{status}: #{val.inspect}"
59-
end
60-
end
61-
6247
def [](key)
6348
@payload[key]
6449
end
@@ -72,12 +57,7 @@ def assert_ok
7257
raise Error::ServerError, self
7358
end
7459

75-
def add_backtrace(ex)
76-
return unless error_payload.is_a?(Hash)
77-
78-
server_trace = error_payload[STACKTRACE_KEY] ||
79-
error_payload[STACKTRACE_KEY.downcase] ||
80-
error_payload.dig('value', STACKTRACE_KEY)
60+
def add_backtrace(ex, server_trace)
8161
return unless server_trace
8262

8363
backtrace = case server_trace
@@ -107,20 +87,14 @@ def backtrace_from_remote(server_trace)
10787
end.compact
10888
end
10989

110-
def error_payload
111-
# Even errors are wrapped in 'value' for w3c
112-
# Grab 'value' key for error, leave original payload alone and let the bridge process
113-
@error_payload ||= !@payload.key?('sessionId') ? @payload['value'] : @payload
114-
end
115-
116-
def status
117-
return unless error_payload.is_a? Hash
118-
@status ||= error_payload['status'] || error_payload['error']
119-
end
90+
def process_error
91+
return unless self['value'].is_a?(Hash)
12092

121-
def value
122-
return unless error_payload.is_a? Hash
123-
@value ||= error_payload['value'] || error_payload['message']
93+
[
94+
self['value']['error'],
95+
self['value']['message'],
96+
self['value']['stacktrace']
97+
]
12498
end
12599
end # Response
126100
end # Remote

Diff for: rb/spec/integration/selenium/webdriver/element_spec.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ module WebDriver
7575
expect(element.attribute('value')).to be_empty
7676

7777
file = Tempfile.new('file-upload')
78-
path = WebDriver::Platform.windows_path(file.path) if WebDriver::Platform.windows?
78+
path = file.path
79+
path = WebDriver::Platform.windows_path(path) if WebDriver::Platform.windows?
7980

8081
element.send_keys path
8182

0 commit comments

Comments
 (0)