UNPKG

5.79 kBJavaScriptView Raw
1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18/**
19 * @fileoverview Contains several classes for handling commands.
20 */
21
22'use strict'
23
24/**
25 * Describes a command to execute.
26 * @final
27 */
28class Command {
29 /** @param {string} name The name of this command. */
30 constructor(name) {
31 /** @private {string} */
32 this.name_ = name
33
34 /** @private {!Object<*>} */
35 this.parameters_ = {}
36 }
37
38 /** @return {string} This command's name. */
39 getName() {
40 return this.name_
41 }
42
43 /**
44 * Sets a parameter to send with this command.
45 * @param {string} name The parameter name.
46 * @param {*} value The parameter value.
47 * @return {!Command} A self reference.
48 */
49 setParameter(name, value) {
50 this.parameters_[name] = value
51 return this
52 }
53
54 /**
55 * Sets the parameters for this command.
56 * @param {!Object<*>} parameters The command parameters.
57 * @return {!Command} A self reference.
58 */
59 setParameters(parameters) {
60 this.parameters_ = parameters
61 return this
62 }
63
64 /**
65 * Returns a named command parameter.
66 * @param {string} key The parameter key to look up.
67 * @return {*} The parameter value, or undefined if it has not been set.
68 */
69 getParameter(key) {
70 return this.parameters_[key]
71 }
72
73 /**
74 * @return {!Object<*>} The parameters to send with this command.
75 */
76 getParameters() {
77 return this.parameters_
78 }
79}
80
81/**
82 * Enumeration of predefined names command names that all command processors
83 * will support.
84 * @enum {string}
85 */
86// TODO: Delete obsolete command names.
87const Name = {
88 GET_SERVER_STATUS: 'getStatus',
89
90 NEW_SESSION: 'newSession',
91 GET_SESSIONS: 'getSessions',
92
93 CLOSE: 'close',
94 QUIT: 'quit',
95
96 GET_CURRENT_URL: 'getCurrentUrl',
97 GET: 'get',
98 GO_BACK: 'goBack',
99 GO_FORWARD: 'goForward',
100 REFRESH: 'refresh',
101
102 ADD_COOKIE: 'addCookie',
103 GET_COOKIE: 'getCookie',
104 GET_ALL_COOKIES: 'getCookies',
105 DELETE_COOKIE: 'deleteCookie',
106 DELETE_ALL_COOKIES: 'deleteAllCookies',
107
108 GET_ACTIVE_ELEMENT: 'getActiveElement',
109 FIND_ELEMENT: 'findElement',
110 FIND_ELEMENTS: 'findElements',
111 FIND_ELEMENTS_RELATIVE: 'findElementsRelative',
112 FIND_CHILD_ELEMENT: 'findChildElement',
113 FIND_CHILD_ELEMENTS: 'findChildElements',
114
115 CLEAR_ELEMENT: 'clearElement',
116 CLICK_ELEMENT: 'clickElement',
117 SEND_KEYS_TO_ELEMENT: 'sendKeysToElement',
118 SUBMIT_ELEMENT: 'submitElement',
119
120 GET_CURRENT_WINDOW_HANDLE: 'getCurrentWindowHandle',
121 GET_WINDOW_HANDLES: 'getWindowHandles',
122 GET_WINDOW_POSITION: 'getWindowPosition',
123 SET_WINDOW_POSITION: 'setWindowPosition',
124 GET_WINDOW_SIZE: 'getWindowSize',
125 SET_WINDOW_SIZE: 'setWindowSize',
126 GET_WINDOW_RECT: 'getWindowRect',
127 SET_WINDOW_RECT: 'setWindowRect',
128 MAXIMIZE_WINDOW: 'maximizeWindow',
129 MINIMIZE_WINDOW: 'minimizeWindow',
130 FULLSCREEN_WINDOW: 'fullscreenWindow',
131
132 SWITCH_TO_WINDOW: 'switchToWindow',
133 SWITCH_TO_NEW_WINDOW: 'newWindow',
134 SWITCH_TO_FRAME: 'switchToFrame',
135 SWITCH_TO_FRAME_PARENT: 'switchToFrameParent',
136 GET_PAGE_SOURCE: 'getPageSource',
137 GET_TITLE: 'getTitle',
138
139 EXECUTE_SCRIPT: 'executeScript',
140 EXECUTE_ASYNC_SCRIPT: 'executeAsyncScript',
141
142 GET_ELEMENT_TEXT: 'getElementText',
143 GET_COMPUTED_ROLE: 'getAriaRole',
144 GET_COMPUTED_LABEL: 'getAccessibleName',
145 GET_ELEMENT_TAG_NAME: 'getElementTagName',
146 IS_ELEMENT_SELECTED: 'isElementSelected',
147 IS_ELEMENT_ENABLED: 'isElementEnabled',
148 IS_ELEMENT_DISPLAYED: 'isElementDisplayed',
149 GET_ELEMENT_LOCATION: 'getElementLocation',
150 GET_ELEMENT_LOCATION_IN_VIEW: 'getElementLocationOnceScrolledIntoView',
151 GET_ELEMENT_RECT: 'getElementRect',
152 GET_ELEMENT_SIZE: 'getElementSize',
153 GET_ELEMENT_ATTRIBUTE: 'getElementAttribute',
154 GET_ELEMENT_VALUE_OF_CSS_PROPERTY: 'getElementValueOfCssProperty',
155 GET_ELEMENT_PROPERTY: 'getElementProperty',
156
157 SCREENSHOT: 'screenshot',
158 TAKE_ELEMENT_SCREENSHOT: 'takeElementScreenshot',
159 SET_SCRIPT_TIMEOUT: 'setScriptTimeout',
160
161 PRINT_PAGE: 'printPage',
162
163 GET_TIMEOUT: 'getTimeout',
164 SET_TIMEOUT: 'setTimeout',
165
166 ACCEPT_ALERT: 'acceptAlert',
167 DISMISS_ALERT: 'dismissAlert',
168 GET_ALERT_TEXT: 'getAlertText',
169 SET_ALERT_TEXT: 'setAlertValue',
170
171 GET_AVAILABLE_LOG_TYPES: 'getAvailableLogTypes',
172 GET_LOG: 'getLog',
173 GET_SESSION_LOGS: 'getSessionLogs',
174
175 // Non-standard commands used by the standalone Selenium server.
176 UPLOAD_FILE: 'uploadFile',
177
178 ACTIONS: 'actions',
179 CLEAR_ACTIONS: 'clearActions',
180}
181
182/**
183 * Handles the execution of WebDriver {@link Command commands}.
184 * @record
185 */
186class Executor {
187 /**
188 * Executes the given {@code command}. If there is an error executing the
189 * command, the provided callback will be invoked with the offending error.
190 * Otherwise, the callback will be invoked with a null Error and non-null
191 * response object.
192 *
193 * @param {!Command} command The command to execute.
194 * @return {!Promise<?>} A promise that will be fulfilled with the command
195 * result.
196 */
197 execute(command) {} // eslint-disable-line
198}
199
200// PUBLIC API
201
202module.exports = {
203 Command: Command,
204 Name: Name,
205 Executor: Executor,
206}