File: error.rb

package info (click to toggle)
ruby-selenium-webdriver 3.142.7%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,592 kB
  • sloc: ruby: 6,740; javascript: 85; makefile: 3
file content (383 lines) | stat: -rw-r--r-- 12,252 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# frozen_string_literal: true

# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

module Selenium
  module WebDriver
    module Error # rubocop:disable Metrics/ModuleLength

      #
      # Returns exception from code (Integer - OSS, String - W3C).
      # @param [Integer, String, nil] code
      #

      def self.for_code(code)
        case code
        when nil, 0
          nil
        when Integer
          Object.const_get(ERRORS.fetch(code).to_s)
        when String
          klass_name = code.split(' ').map(&:capitalize).join.sub(/Error$/, '')
          const_get("#{klass_name}Error", false)
        end
      rescue KeyError, NameError
        WebDriverError
      end

      class WebDriverError < StandardError; end

      class IndexOutOfBoundsError < WebDriverError; end # 1
      class NoCollectionError < WebDriverError; end # 2
      class NoStringError < WebDriverError; end # 3
      class NoStringLengthError < WebDriverError; end # 4
      class NoStringWrapperError < WebDriverError; end # 5
      class NoSuchDriverError < WebDriverError; end # 6

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

      class NoSuchElementError < WebDriverError; end # 7

      #
      # A command to switch to a frame could not be satisfied because the frame could not be found.
      #

      class NoSuchFrameError < WebDriverError; end # 8

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

      class UnknownCommandError < WebDriverError; end # 9

      #
      # A command failed because the referenced element is no longer attached to the DOM.
      #

      class StaleElementReferenceError < WebDriverError; end # 10

      #
      # Raised to indicate that although an element is present on the DOM, it is not visible, and
      # so is not able to be interacted with.
      #

      class ElementNotVisibleError < WebDriverError; end # 11

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

      class InvalidElementStateError < WebDriverError; end # 12

      #
      # An unknown error occurred in the remote end while processing the command.
      #

      class UnknownError < WebDriverError; end # 13
      class ExpectedError < WebDriverError; end # 14

      #
      # An attempt was made to select an element that cannot be selected.
      #

      class ElementNotSelectableError < WebDriverError; end # 15
      class NoSuchDocumentError < WebDriverError; end # 16

      #
      # An error occurred while executing JavaScript supplied by the user.
      #

      class JavascriptError < WebDriverError; end # 17
      class NoScriptResultError < WebDriverError; end # 18

      #
      # An error occurred while searching for an element by XPath.
      #

      class XPathLookupError < WebDriverError; end # 19
      class NoSuchCollectionError < WebDriverError; end # 20

      #
      # An operation did not complete before its timeout expired.
      #

      class TimeOutError < WebDriverError; end # 21

      class NullPointerError < WebDriverError; end # 22
      class NoSuchWindowError < WebDriverError; end # 23

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

      class InvalidCookieDomainError < WebDriverError; end # 24

      #
      # A command to set a cookie's value could not be satisfied.
      #

      class UnableToSetCookieError < WebDriverError; end # 25

      #
      # Raised when an alert dialog is present that has not been dealt with.
      #
      class UnhandledAlertError < WebDriverError; end # 26

      #
      # An attempt was made to operate on a modal dialog when one was not open:
      #
      #   * W3C dialect is NoSuchAlertError
      #   * OSS dialect is NoAlertPresentError
      #
      # We want to allow clients to rescue NoSuchAlertError as a superclass for
      # dialect-agnostic implementation, so NoAlertPresentError should inherit from it.
      #

      class NoSuchAlertError < WebDriverError; end
      class NoAlertPresentError < NoSuchAlertError; end # 27

      #
      # A script did not complete before its timeout expired.
      #

      class ScriptTimeOutError < WebDriverError; end # 28

      #
      # The coordinates provided to an interactions operation are invalid.
      #

      class InvalidElementCoordinatesError < WebDriverError; end # 29

      #
      # Indicates that IME support is not available. This exception is rasied for every IME-related
      # method call if IME support is not available on the machine.
      #

      class IMENotAvailableError < WebDriverError; end # 30

      #
      # Indicates that activating an IME engine has failed.
      #

      class IMEEngineActivationFailedError < WebDriverError; end # 31

      #
      # Argument was an invalid selector.
      #

      class InvalidSelectorError < WebDriverError; end # 32

      #
      # A new session could not be created.
      #

      class SessionNotCreatedError < WebDriverError; end # 33

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

      class MoveTargetOutOfBoundsError < WebDriverError; end # 34

      #
      # Indicates that the XPath selector is invalid
      #

      class InvalidXpathSelectorError < WebDriverError; end
      class InvalidXpathSelectorReturnTyperError < WebDriverError; end

      #
      # A command could not be completed because the element is not pointer or keyboard
      # interactable.
      #

      class ElementNotInteractableError < WebDriverError; end

      #
      # A command could not be completed because TLS certificate is expired
      # or invalid.
      #

      class InsecureCertificateError < WebDriverError; end

      #
      # The arguments passed to a command are either invalid or malformed.
      #

      class InvalidArgumentError < WebDriverError; end

      #
      # No cookie matching the given path name was found amongst the associated cookies of the
      # current browsing context's active document.
      #

      class NoSuchCookieError < WebDriverError; end

      #
      # A screen capture was made impossible.
      #

      class UnableToCaptureScreenError < WebDriverError; end

      #
      # Occurs if the given session id is not in the list of active sessions, meaning the session
      # either does not exist or that it's not active.
      #

      class InvalidSessionIdError < WebDriverError; end

      #
      # A modal dialog was open, blocking this operation.
      #

      class UnexpectedAlertOpenError < WebDriverError; end

      #
      # The requested command matched a known URL but did not match an method for that URL.
      #

      class UnknownMethodError < WebDriverError; end

      #
      # The Element Click command could not be completed because the element receiving the events
      # is obscuring the element that was requested clicked.
      #

      class ElementClickInterceptedError < WebDriverError; end

      #
      # Indicates that a command that should have executed properly cannot be supported for some
      # reason.
      #

      class UnsupportedOperationError < WebDriverError; end

      # Aliases for OSS dialect.
      ScriptTimeoutError  = Class.new(ScriptTimeOutError)
      TimeoutError        = Class.new(TimeOutError)
      NoAlertOpenError    = Class.new(NoAlertPresentError)

      # Aliases for backwards compatibility.
      ObsoleteElementError      = Class.new(StaleElementReferenceError)
      UnhandledError            = Class.new(UnknownError)
      UnexpectedJavascriptError = Class.new(JavascriptError)
      ElementNotDisplayedError  = Class.new(ElementNotVisibleError)

      #
      # @api private
      #

      ERRORS = {
        1 => IndexOutOfBoundsError,
        2 => NoCollectionError,
        3 => NoStringError,
        4 => NoStringLengthError,
        5 => NoStringWrapperError,
        6 => NoSuchDriverError,
        7 => NoSuchElementError,
        8 => NoSuchFrameError,
        9 => UnknownCommandError,
        10 => StaleElementReferenceError,
        11 => ElementNotVisibleError,
        12 => InvalidElementStateError,
        13 => UnknownError,
        14 => ExpectedError,
        15 => ElementNotSelectableError,
        16 => NoSuchDocumentError,
        17 => JavascriptError,
        18 => NoScriptResultError,
        19 => XPathLookupError,
        20 => NoSuchCollectionError,
        21 => TimeOutError,
        22 => NullPointerError,
        23 => NoSuchWindowError,
        24 => InvalidCookieDomainError,
        25 => UnableToSetCookieError,
        26 => UnhandledAlertError,
        27 => NoAlertPresentError,
        28 => ScriptTimeOutError,
        29 => InvalidElementCoordinatesError,
        30 => IMENotAvailableError,
        31 => IMEEngineActivationFailedError,
        32 => InvalidSelectorError,
        33 => SessionNotCreatedError,
        34 => MoveTargetOutOfBoundsError,
        # The following are W3C-specific errors,
        # they don't really need error codes, we just make them up!
        51 => InvalidXpathSelectorError,
        52 => InvalidXpathSelectorReturnTyperError,
        60 => ElementNotInteractableError,
        61 => InvalidArgumentError,
        62 => NoSuchCookieError,
        63 => UnableToCaptureScreenError
      }.freeze

      DEPRECATED_ERRORS = {
        IndexOutOfBoundsError: nil,
        NoCollectionError: nil,
        NoStringError: nil,
        NoStringLengthError: nil,
        NoStringWrapperError: nil,
        NoSuchDriverError: nil,
        ElementNotVisibleError: ElementNotInteractableError,
        InvalidElementStateError: ElementNotInteractableError,
        ElementNotSelectableError: ElementNotInteractableError,
        ExpectedError: nil,
        NoSuchDocumentError: nil,
        NoScriptResultError: nil,
        XPathLookupError: InvalidSelectorError,
        NoSuchCollectionError: nil,
        UnhandledAlertError: UnexpectedAlertOpenError,
        NoAlertPresentError: NoSuchAlertError,
        NoAlertOpenError: NoSuchAlertError,
        ScriptTimeOutError: ScriptTimeoutError,
        InvalidElementCoordinatesError: nil,
        IMENotAvailableError: nil,
        IMEEngineActivationFailedError: nil,
        InvalidXpathSelectorError: InvalidSelectorError,
        InvalidXpathSelectorReturnTyperError: InvalidSelectorError,
        TimeOutError: TimeoutError,
        ObsoleteElementError: StaleElementReferenceError,
        UnhandledError: UnknownError,
        UnexpectedJavascriptError: JavascriptError,
        ElementNotDisplayedError: ElementNotInteractableError
      }.freeze

      DEPRECATED_ERRORS.keys.each do |oss_error|
        remove_const oss_error
      end

      def self.const_missing(const_name)
        super unless DEPRECATED_ERRORS.key?(const_name)
        if DEPRECATED_ERRORS[const_name]
          WebDriver.logger.deprecate("Selenium::WebDriver::Error::#{const_name}",
                                     "#{DEPRECATED_ERRORS[const_name]} (ensure the driver supports W3C WebDriver specification)")
          DEPRECATED_ERRORS[const_name]
        else
          WebDriver.logger.deprecate("Selenium::WebDriver::Error::#{const_name}")
          WebDriverError
        end
      end

    end # Error
  end # WebDriver
end # Selenium