Chris@14
|
1 <?php
|
Chris@14
|
2 /**
|
Chris@14
|
3 * Copyright 2004-2017 Facebook. All Rights Reserved.
|
Chris@14
|
4 *
|
Chris@14
|
5 * Licensed under the Apache License, Version 2.0 (the "License");
|
Chris@14
|
6 * you may not use this file except in compliance with the License.
|
Chris@14
|
7 * You may obtain a copy of the License at
|
Chris@14
|
8 *
|
Chris@14
|
9 * http://www.apache.org/licenses/LICENSE-2.0
|
Chris@14
|
10 *
|
Chris@14
|
11 * Unless required by applicable law or agreed to in writing, software
|
Chris@14
|
12 * distributed under the License is distributed on an "AS IS" BASIS,
|
Chris@14
|
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
Chris@14
|
14 * See the License for the specific language governing permissions and
|
Chris@14
|
15 * limitations under the License.
|
Chris@14
|
16 *
|
Chris@14
|
17 * @package WebDriver
|
Chris@14
|
18 *
|
Chris@14
|
19 * @author Justin Bishop <jubishop@gmail.com>
|
Chris@14
|
20 * @author Anthon Pang <apang@softwaredevelopment.ca>
|
Chris@14
|
21 */
|
Chris@14
|
22
|
Chris@14
|
23 namespace WebDriver;
|
Chris@14
|
24
|
Chris@14
|
25 /**
|
Chris@14
|
26 * WebDriver\Exception class
|
Chris@14
|
27 *
|
Chris@14
|
28 * @package WebDriver
|
Chris@14
|
29 */
|
Chris@14
|
30 abstract class Exception extends \Exception
|
Chris@14
|
31 {
|
Chris@14
|
32 /**
|
Chris@14
|
33 * Response status codes
|
Chris@14
|
34 *
|
Chris@14
|
35 * @link http://code.google.com/p/selenium/wiki/JsonWireProtocol#Response_Status_Codes
|
Chris@14
|
36 */
|
Chris@14
|
37 const SUCCESS = 0;
|
Chris@14
|
38 const NO_SUCH_DRIVER = 6;
|
Chris@14
|
39 const NO_SUCH_ELEMENT = 7;
|
Chris@14
|
40 const NO_SUCH_FRAME = 8;
|
Chris@14
|
41 const UNKNOWN_COMMAND = 9;
|
Chris@14
|
42 const STALE_ELEMENT_REFERENCE = 10;
|
Chris@14
|
43 const ELEMENT_NOT_VISIBLE = 11;
|
Chris@14
|
44 const INVALID_ELEMENT_STATE = 12;
|
Chris@14
|
45 const UNKNOWN_ERROR = 13;
|
Chris@14
|
46 const ELEMENT_IS_NOT_SELECTABLE = 15;
|
Chris@14
|
47 const JAVASCRIPT_ERROR = 17;
|
Chris@14
|
48 const XPATH_LOOKUP_ERROR = 19;
|
Chris@14
|
49 const TIMEOUT = 21;
|
Chris@14
|
50 const NO_SUCH_WINDOW = 23;
|
Chris@14
|
51 const INVALID_COOKIE_DOMAIN = 24;
|
Chris@14
|
52 const UNABLE_TO_SET_COOKIE = 25;
|
Chris@14
|
53 const UNEXPECTED_ALERT_OPEN = 26;
|
Chris@14
|
54 const NO_ALERT_OPEN_ERROR = 27;
|
Chris@14
|
55 const SCRIPT_TIMEOUT = 28;
|
Chris@14
|
56 const INVALID_ELEMENT_COORDINATES = 29;
|
Chris@14
|
57 const IME_NOT_AVAILABLE = 30;
|
Chris@14
|
58 const IME_ENGINE_ACTIVATION_FAILED = 31;
|
Chris@14
|
59 const INVALID_SELECTOR = 32;
|
Chris@14
|
60 const SESSION_NOT_CREATED = 33;
|
Chris@14
|
61 const MOVE_TARGET_OUT_OF_BOUNDS = 34;
|
Chris@14
|
62
|
Chris@14
|
63 // obsolete
|
Chris@14
|
64 const INDEX_OUT_OF_BOUNDS = 1;
|
Chris@14
|
65 const NO_COLLECTION = 2;
|
Chris@14
|
66 const NO_STRING = 3;
|
Chris@14
|
67 const NO_STRING_LENGTH = 4;
|
Chris@14
|
68 const NO_STRING_WRAPPER = 5;
|
Chris@14
|
69 const OBSOLETE_ELEMENT = 10;
|
Chris@14
|
70 const ELEMENT_NOT_DISPLAYED = 11;
|
Chris@14
|
71 const UNHANDLED = 13;
|
Chris@14
|
72 const EXPECTED = 14;
|
Chris@14
|
73 const ELEMENT_NOT_SELECTABLE = 15;
|
Chris@14
|
74 const NO_SUCH_DOCUMENT = 16;
|
Chris@14
|
75 const UNEXPECTED_JAVASCRIPT = 17;
|
Chris@14
|
76 const NO_SCRIPT_RESULT = 18;
|
Chris@14
|
77 const NO_SUCH_COLLECTION = 20;
|
Chris@14
|
78 const NULL_POINTER = 22;
|
Chris@14
|
79 const NO_MODAL_DIALOG_OPEN_ERROR = 27;
|
Chris@14
|
80
|
Chris@14
|
81 // user-defined
|
Chris@14
|
82 const CURL_EXEC = -1;
|
Chris@14
|
83 const OBSOLETE_COMMAND = -2;
|
Chris@14
|
84 const NO_PARAMETERS_EXPECTED = -3;
|
Chris@14
|
85 const JSON_PARAMETERS_EXPECTED = -4;
|
Chris@14
|
86 const UNEXPECTED_PARAMETERS = -5;
|
Chris@14
|
87 const INVALID_REQUEST = -6;
|
Chris@14
|
88 const UNKNOWN_LOCATOR_STRATEGY = -7;
|
Chris@14
|
89
|
Chris@14
|
90 private static $errs = array(
|
Chris@14
|
91 // self::SUCCESS => array('Success', 'This should never be thrown!'),
|
Chris@14
|
92
|
Chris@14
|
93 self::NO_SUCH_DRIVER => array('NoSuchDriver', 'A session is either terminated or not started'),
|
Chris@14
|
94 self::NO_SUCH_ELEMENT => array('NoSuchElement', 'An element could not be located on the page using the given search parameters.'),
|
Chris@14
|
95 self::NO_SUCH_FRAME => array('NoSuchFrame', 'A request to switch to a frame could not be satisfied because the frame could not be found.'),
|
Chris@14
|
96 self::UNKNOWN_COMMAND => array('UnknownCommand', 'The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource.'),
|
Chris@14
|
97 self::STALE_ELEMENT_REFERENCE => array('StaleElementReference', 'An element command failed because the referenced element is no longer attached to the DOM.'),
|
Chris@14
|
98 self::ELEMENT_NOT_VISIBLE => array('ElementNotVisible', 'An element command could not be completed because the element is not visible on the page.'),
|
Chris@14
|
99 self::INVALID_ELEMENT_STATE => array('InvalidElementState', 'An element command could not be completed because the element is in an invalid state (e.g., attempting to click a disabled element).'),
|
Chris@14
|
100 self::UNKNOWN_ERROR => array('UnknownError', 'An unknown server-side error occurred while processing the command.'),
|
Chris@14
|
101 self::ELEMENT_IS_NOT_SELECTABLE => array('ElementIsNotSelectable', 'An attempt was made to select an element that cannot be selected.'),
|
Chris@14
|
102 self::JAVASCRIPT_ERROR => array('JavaScriptError', 'An error occurred while executing user supplied JavaScript.'),
|
Chris@14
|
103 self::XPATH_LOOKUP_ERROR => array('XPathLookupError', 'An error occurred while searching for an element by XPath.'),
|
Chris@14
|
104 self::TIMEOUT => array('Timeout', 'An operation did not complete before its timeout expired.'),
|
Chris@14
|
105 self::NO_SUCH_WINDOW => array('NoSuchWindow', 'A request to switch to a different window could not be satisfied because the window could not be found.'),
|
Chris@14
|
106 self::INVALID_COOKIE_DOMAIN => array('InvalidCookieDomain', 'An illegal attempt was made to set a cookie under a different domain than the current page.'),
|
Chris@14
|
107 self::UNABLE_TO_SET_COOKIE => array('UnableToSetCookie', 'A request to set a cookie\'s value could not be satisfied.'),
|
Chris@14
|
108 self::UNEXPECTED_ALERT_OPEN => array('UnexpectedAlertOpen', 'A modal dialog was open, blocking this operation'),
|
Chris@14
|
109 self::NO_ALERT_OPEN_ERROR => array('NoAlertOpenError', 'An attempt was made to operate on a modal dialog when one was not open.'),
|
Chris@14
|
110 self::SCRIPT_TIMEOUT => array('ScriptTimeout', 'A script did not complete before its timeout expired.'),
|
Chris@14
|
111 self::INVALID_ELEMENT_COORDINATES => array('InvalidElementCoordinates', 'The coordinates provided to an interactions operation are invalid.'),
|
Chris@14
|
112 self::IME_NOT_AVAILABLE => array('IMENotAvailable', 'IME was not available.'),
|
Chris@14
|
113 self::IME_ENGINE_ACTIVATION_FAILED => array('IMEEngineActivationFailed', 'An IME engine could not be started.'),
|
Chris@14
|
114 self::INVALID_SELECTOR => array('InvalidSelector', 'Argument was an invalid selector (e.g., XPath/CSS).'),
|
Chris@14
|
115 self::SESSION_NOT_CREATED => array('SessionNotCreated', 'A new session could not be created (e.g., a required capability could not be set).'),
|
Chris@14
|
116 self::MOVE_TARGET_OUT_OF_BOUNDS => array('MoveTargetOutOfBounds', 'Target provided for a move action is out of bounds.'),
|
Chris@14
|
117
|
Chris@14
|
118 self::CURL_EXEC => array('CurlExec', 'curl_exec() error.'),
|
Chris@14
|
119 self::OBSOLETE_COMMAND => array('ObsoleteCommand', 'This WebDriver command is obsolete.'),
|
Chris@14
|
120 self::NO_PARAMETERS_EXPECTED => array('NoParametersExpected', 'This HTTP request method expects no parameters.'),
|
Chris@14
|
121 self::JSON_PARAMETERS_EXPECTED => array('JsonParameterExpected', 'This POST request expects a JSON parameter (array).'),
|
Chris@14
|
122 self::UNEXPECTED_PARAMETERS => array('UnexpectedParameters', 'This command does not expect this number of parameters.'),
|
Chris@14
|
123 self::INVALID_REQUEST => array('InvalidRequest', 'This command does not support this HTTP request method.'),
|
Chris@14
|
124 self::UNKNOWN_LOCATOR_STRATEGY => array('UnknownLocatorStrategy', 'This locator strategy is not supported.'),
|
Chris@14
|
125 );
|
Chris@14
|
126
|
Chris@14
|
127 /**
|
Chris@14
|
128 * Factory method to create WebDriver\Exception objects
|
Chris@14
|
129 *
|
Chris@14
|
130 * @param integer $code Code
|
Chris@14
|
131 * @param string $message Message
|
Chris@14
|
132 * @param \Exception $previousException Previous exception
|
Chris@14
|
133 *
|
Chris@14
|
134 * @return \Exception
|
Chris@14
|
135 */
|
Chris@14
|
136 public static function factory($code, $message = null, $previousException = null)
|
Chris@14
|
137 {
|
Chris@14
|
138 // unknown error
|
Chris@14
|
139 if (!isset(self::$errs[$code])) {
|
Chris@14
|
140 if (trim($message) === '') {
|
Chris@14
|
141 $message = 'Unknown Error';
|
Chris@14
|
142 }
|
Chris@14
|
143
|
Chris@14
|
144 return new \Exception($message, $code, $previousException);
|
Chris@14
|
145 }
|
Chris@14
|
146
|
Chris@14
|
147 $errorDefinition = self::$errs[$code];
|
Chris@14
|
148
|
Chris@14
|
149 if (trim($message) === '') {
|
Chris@14
|
150 $message = $errorDefinition[1];
|
Chris@14
|
151 }
|
Chris@14
|
152
|
Chris@14
|
153 $className = __CLASS__ . '\\' . $errorDefinition[0];
|
Chris@14
|
154
|
Chris@14
|
155 return new $className($message, $code, $previousException);
|
Chris@14
|
156 }
|
Chris@14
|
157 }
|