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 | 'use strict';
|
27 |
|
28 | const fs = require('fs');
|
29 | const path = require('path');
|
30 |
|
31 | const cmd = require('./command');
|
32 | const devmode = require('./devmode');
|
33 | const error = require('./error');
|
34 | const logging = require('./logging');
|
35 | const promise = require('./promise');
|
36 | const Session = require('./session').Session;
|
37 | const WebElement = require('./webdriver').WebElement;
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function headersToString(headers) {
|
46 | let ret = [];
|
47 | headers.forEach(function(value, name) {
|
48 | ret.push(`${name.toLowerCase()}: ${value}`);
|
49 | });
|
50 | return ret.join('\n');
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | class Request {
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 | constructor(method, path, opt_data) {
|
67 | this.method = method;
|
68 | this.path = path;
|
69 | this.data = opt_data;
|
70 | this.headers = new Map(
|
71 | [['Accept', 'application/json; charset=utf-8']]);
|
72 | }
|
73 |
|
74 |
|
75 | toString() {
|
76 | let ret = `${this.method} ${this.path} HTTP/1.1\n`;
|
77 | ret += headersToString(this.headers) + '\n\n';
|
78 | if (this.data) {
|
79 | ret += JSON.stringify(this.data);
|
80 | }
|
81 | return ret;
|
82 | }
|
83 | }
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | class Response {
|
91 | |
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | constructor(status, headers, body) {
|
98 | this.status = status;
|
99 | this.body = body;
|
100 | this.headers = new Map;
|
101 | for (let header in headers) {
|
102 | this.headers.set(header.toLowerCase(), headers[header]);
|
103 | }
|
104 | }
|
105 |
|
106 |
|
107 | toString() {
|
108 | let ret = `HTTP/1.1 ${this.status}\n${headersToString(this.headers)}\n\n`;
|
109 | if (this.body) {
|
110 | ret += this.body;
|
111 | }
|
112 | return ret;
|
113 | }
|
114 | }
|
115 |
|
116 |
|
117 | const DEV_ROOT = '../../../../buck-out/gen/javascript/';
|
118 |
|
119 |
|
120 | const Atom = {
|
121 | GET_ATTRIBUTE: devmode
|
122 | ? path.join(__dirname, DEV_ROOT, 'webdriver/atoms/getAttribute.js')
|
123 | : path.join(__dirname, 'atoms/getAttribute.js'),
|
124 | IS_DISPLAYED: devmode
|
125 | ? path.join(__dirname, DEV_ROOT, 'atoms/fragments/is-displayed.js')
|
126 | : path.join(__dirname, 'atoms/isDisplayed.js'),
|
127 | };
|
128 |
|
129 |
|
130 | const ATOMS = new Map();
|
131 | const LOG = logging.getLogger('webdriver.http');
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | function loadAtom(file) {
|
139 | if (ATOMS.has(file)) {
|
140 | return ATOMS.get(file);
|
141 | }
|
142 | let contents = new Promise((resolve, reject) => {
|
143 | LOG.finest(() => `Loading atom ${file}`);
|
144 | fs.readFile(file, 'utf8', function(err, data) {
|
145 | if (err) {
|
146 | reject(err);
|
147 | } else {
|
148 | resolve(data);
|
149 | }
|
150 | });
|
151 | });
|
152 | ATOMS.set(file, contents);
|
153 | return contents;
|
154 | }
|
155 |
|
156 |
|
157 | function post(path) { return resource('POST', path); }
|
158 | function del(path) { return resource('DELETE', path); }
|
159 | function get(path) { return resource('GET', path); }
|
160 | function resource(method, path) { return {method: method, path: path}; }
|
161 |
|
162 |
|
163 |
|
164 | var CommandSpec;
|
165 |
|
166 |
|
167 |
|
168 | var CommandTransformer;
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | function toExecuteAtomCommand(command, atom, ...params) {
|
177 | return loadAtom(atom).then(atom => {
|
178 | return new cmd.Command(cmd.Name.EXECUTE_SCRIPT)
|
179 | .setParameter('sessionId', command.getParameter('sessionId'))
|
180 | .setParameter('script', `return (${atom}).apply(null, arguments)`)
|
181 | .setParameter('args', params.map(param => command.getParameter(param)));
|
182 | });
|
183 | }
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | const COMMAND_MAP = new Map([
|
189 | [cmd.Name.GET_SERVER_STATUS, get('/status')],
|
190 | [cmd.Name.NEW_SESSION, post('/session')],
|
191 | [cmd.Name.GET_SESSIONS, get('/sessions')],
|
192 | [cmd.Name.DESCRIBE_SESSION, get('/session/:sessionId')],
|
193 | [cmd.Name.QUIT, del('/session/:sessionId')],
|
194 | [cmd.Name.CLOSE, del('/session/:sessionId/window')],
|
195 | [cmd.Name.GET_CURRENT_WINDOW_HANDLE, get('/session/:sessionId/window_handle')],
|
196 | [cmd.Name.GET_WINDOW_HANDLES, get('/session/:sessionId/window_handles')],
|
197 | [cmd.Name.GET_CURRENT_URL, get('/session/:sessionId/url')],
|
198 | [cmd.Name.GET, post('/session/:sessionId/url')],
|
199 | [cmd.Name.GO_BACK, post('/session/:sessionId/back')],
|
200 | [cmd.Name.GO_FORWARD, post('/session/:sessionId/forward')],
|
201 | [cmd.Name.REFRESH, post('/session/:sessionId/refresh')],
|
202 | [cmd.Name.ADD_COOKIE, post('/session/:sessionId/cookie')],
|
203 | [cmd.Name.GET_ALL_COOKIES, get('/session/:sessionId/cookie')],
|
204 | [cmd.Name.DELETE_ALL_COOKIES, del('/session/:sessionId/cookie')],
|
205 | [cmd.Name.DELETE_COOKIE, del('/session/:sessionId/cookie/:name')],
|
206 | [cmd.Name.FIND_ELEMENT, post('/session/:sessionId/element')],
|
207 | [cmd.Name.FIND_ELEMENTS, post('/session/:sessionId/elements')],
|
208 | [cmd.Name.GET_ACTIVE_ELEMENT, post('/session/:sessionId/element/active')],
|
209 | [cmd.Name.FIND_CHILD_ELEMENT, post('/session/:sessionId/element/:id/element')],
|
210 | [cmd.Name.FIND_CHILD_ELEMENTS, post('/session/:sessionId/element/:id/elements')],
|
211 | [cmd.Name.CLEAR_ELEMENT, post('/session/:sessionId/element/:id/clear')],
|
212 | [cmd.Name.CLICK_ELEMENT, post('/session/:sessionId/element/:id/click')],
|
213 | [cmd.Name.SEND_KEYS_TO_ELEMENT, post('/session/:sessionId/element/:id/value')],
|
214 | [cmd.Name.SUBMIT_ELEMENT, post('/session/:sessionId/element/:id/submit')],
|
215 | [cmd.Name.GET_ELEMENT_TEXT, get('/session/:sessionId/element/:id/text')],
|
216 | [cmd.Name.GET_ELEMENT_TAG_NAME, get('/session/:sessionId/element/:id/name')],
|
217 | [cmd.Name.IS_ELEMENT_SELECTED, get('/session/:sessionId/element/:id/selected')],
|
218 | [cmd.Name.IS_ELEMENT_ENABLED, get('/session/:sessionId/element/:id/enabled')],
|
219 | [cmd.Name.IS_ELEMENT_DISPLAYED, get('/session/:sessionId/element/:id/displayed')],
|
220 | [cmd.Name.GET_ELEMENT_LOCATION, get('/session/:sessionId/element/:id/location')],
|
221 | [cmd.Name.GET_ELEMENT_SIZE, get('/session/:sessionId/element/:id/size')],
|
222 | [cmd.Name.GET_ELEMENT_ATTRIBUTE, get('/session/:sessionId/element/:id/attribute/:name')],
|
223 | [cmd.Name.GET_ELEMENT_VALUE_OF_CSS_PROPERTY, get('/session/:sessionId/element/:id/css/:propertyName')],
|
224 | [cmd.Name.ELEMENT_EQUALS, get('/session/:sessionId/element/:id/equals/:other')],
|
225 | [cmd.Name.TAKE_ELEMENT_SCREENSHOT, get('/session/:sessionId/element/:id/screenshot')],
|
226 | [cmd.Name.SWITCH_TO_WINDOW, post('/session/:sessionId/window')],
|
227 | [cmd.Name.MAXIMIZE_WINDOW, post('/session/:sessionId/window/current/maximize')],
|
228 | [cmd.Name.GET_WINDOW_POSITION, get('/session/:sessionId/window/current/position')],
|
229 | [cmd.Name.SET_WINDOW_POSITION, post('/session/:sessionId/window/current/position')],
|
230 | [cmd.Name.GET_WINDOW_SIZE, get('/session/:sessionId/window/current/size')],
|
231 | [cmd.Name.SET_WINDOW_SIZE, post('/session/:sessionId/window/current/size')],
|
232 | [cmd.Name.SWITCH_TO_FRAME, post('/session/:sessionId/frame')],
|
233 | [cmd.Name.GET_PAGE_SOURCE, get('/session/:sessionId/source')],
|
234 | [cmd.Name.GET_TITLE, get('/session/:sessionId/title')],
|
235 | [cmd.Name.EXECUTE_SCRIPT, post('/session/:sessionId/execute')],
|
236 | [cmd.Name.EXECUTE_ASYNC_SCRIPT, post('/session/:sessionId/execute_async')],
|
237 | [cmd.Name.SCREENSHOT, get('/session/:sessionId/screenshot')],
|
238 | [cmd.Name.SET_TIMEOUT, post('/session/:sessionId/timeouts')],
|
239 | [cmd.Name.MOVE_TO, post('/session/:sessionId/moveto')],
|
240 | [cmd.Name.CLICK, post('/session/:sessionId/click')],
|
241 | [cmd.Name.DOUBLE_CLICK, post('/session/:sessionId/doubleclick')],
|
242 | [cmd.Name.MOUSE_DOWN, post('/session/:sessionId/buttondown')],
|
243 | [cmd.Name.MOUSE_UP, post('/session/:sessionId/buttonup')],
|
244 | [cmd.Name.MOVE_TO, post('/session/:sessionId/moveto')],
|
245 | [cmd.Name.SEND_KEYS_TO_ACTIVE_ELEMENT, post('/session/:sessionId/keys')],
|
246 | [cmd.Name.TOUCH_SINGLE_TAP, post('/session/:sessionId/touch/click')],
|
247 | [cmd.Name.TOUCH_DOUBLE_TAP, post('/session/:sessionId/touch/doubleclick')],
|
248 | [cmd.Name.TOUCH_DOWN, post('/session/:sessionId/touch/down')],
|
249 | [cmd.Name.TOUCH_UP, post('/session/:sessionId/touch/up')],
|
250 | [cmd.Name.TOUCH_MOVE, post('/session/:sessionId/touch/move')],
|
251 | [cmd.Name.TOUCH_SCROLL, post('/session/:sessionId/touch/scroll')],
|
252 | [cmd.Name.TOUCH_LONG_PRESS, post('/session/:sessionId/touch/longclick')],
|
253 | [cmd.Name.TOUCH_FLICK, post('/session/:sessionId/touch/flick')],
|
254 | [cmd.Name.ACCEPT_ALERT, post('/session/:sessionId/accept_alert')],
|
255 | [cmd.Name.DISMISS_ALERT, post('/session/:sessionId/dismiss_alert')],
|
256 | [cmd.Name.GET_ALERT_TEXT, get('/session/:sessionId/alert_text')],
|
257 | [cmd.Name.SET_ALERT_TEXT, post('/session/:sessionId/alert_text')],
|
258 | [cmd.Name.SET_ALERT_CREDENTIALS, post('/session/:sessionId/alert/credentials')],
|
259 | [cmd.Name.GET_LOG, post('/session/:sessionId/log')],
|
260 | [cmd.Name.GET_AVAILABLE_LOG_TYPES, get('/session/:sessionId/log/types')],
|
261 | [cmd.Name.GET_SESSION_LOGS, post('/logs')],
|
262 | [cmd.Name.UPLOAD_FILE, post('/session/:sessionId/file')],
|
263 | ]);
|
264 |
|
265 |
|
266 | /** @const {!Map<string, (CommandSpec|CommandTransformer)>} */
|
267 | const W3C_COMMAND_MAP = new Map([
|
268 | [cmd.Name.GET_ACTIVE_ELEMENT, get('/session/:sessionId/element/active')],
|
269 | [cmd.Name.GET_ELEMENT_ATTRIBUTE, (cmd) => {
|
270 | return toExecuteAtomCommand(cmd, Atom.GET_ATTRIBUTE, 'id', 'name');
|
271 | }],
|
272 | [cmd.Name.IS_ELEMENT_DISPLAYED, (cmd) => {
|
273 | return toExecuteAtomCommand(cmd, Atom.IS_DISPLAYED, 'id');
|
274 | }],
|
275 | [cmd.Name.MAXIMIZE_WINDOW, post('/session/:sessionId/window/maximize')],
|
276 | [cmd.Name.GET_WINDOW_POSITION, get('/session/:sessionId/window/position')],
|
277 | [cmd.Name.SET_WINDOW_POSITION, post('/session/:sessionId/window/position')],
|
278 | [cmd.Name.GET_WINDOW_SIZE, get('/session/:sessionId/window/size')],
|
279 | [cmd.Name.SET_WINDOW_SIZE, post('/session/:sessionId/window/size')],
|
280 | ]);
|
281 |
|
282 |
|
283 | /**
|
284 | * Handles sending HTTP messages to a remote end.
|
285 | *
|
286 | * @interface
|
287 | */
|
288 | class Client {
|
289 |
|
290 | |
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 | send(httpRequest) {}
|
300 | }
|
301 |
|
302 |
|
303 | const CLIENTS =
|
304 | new WeakMap;
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 | function doSend(executor, request) {
|
314 | const client = CLIENTS.get(executor);
|
315 | if (promise.isPromise(client)) {
|
316 | return client.then(client => {
|
317 | CLIENTS.set(executor, client);
|
318 | return client.send(request);
|
319 | });
|
320 | } else {
|
321 | return client.send(request);
|
322 | }
|
323 | }
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 | function buildRequest(customCommands, w3c, command) {
|
335 | LOG.finest(() => `Translating command: ${command.getName()}`);
|
336 | let spec = customCommands && customCommands.get(command.getName());
|
337 | if (spec) {
|
338 | return toHttpRequest(spec);
|
339 | }
|
340 |
|
341 | if (w3c) {
|
342 | spec = W3C_COMMAND_MAP.get(command.getName());
|
343 | if (typeof spec === 'function') {
|
344 | LOG.finest(() => `Transforming command for W3C: ${command.getName()}`);
|
345 | return spec(command)
|
346 | .then(newCommand => buildRequest(customCommands, w3c, newCommand));
|
347 | } else if (spec) {
|
348 | return toHttpRequest(spec);
|
349 | }
|
350 | }
|
351 |
|
352 | spec = COMMAND_MAP.get(command.getName());
|
353 | if (spec) {
|
354 | return toHttpRequest(spec);
|
355 | }
|
356 | return Promise.reject(
|
357 | new error.UnknownCommandError(
|
358 | 'Unrecognized command: ' + command.getName()));
|
359 |
|
360 | |
361 |
|
362 |
|
363 |
|
364 | function toHttpRequest(resource) {
|
365 | LOG.finest(() => `Building HTTP request: ${JSON.stringify(resource)}`);
|
366 | let parameters = command.getParameters();
|
367 | let path = buildPath(resource.path, parameters);
|
368 | return Promise.resolve(new Request(resource.method, path, parameters));
|
369 | }
|
370 | }
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 | class Executor {
|
387 | |
388 |
|
389 |
|
390 |
|
391 |
|
392 | constructor(client) {
|
393 | CLIENTS.set(this, client);
|
394 |
|
395 | |
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 | this.w3c = false;
|
403 |
|
404 |
|
405 | this.customCommands_ = null;
|
406 |
|
407 |
|
408 | this.log_ = logging.getLogger('webdriver.http.Executor');
|
409 | }
|
410 |
|
411 | |
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 | defineCommand(name, method, path) {
|
425 | if (!this.customCommands_) {
|
426 | this.customCommands_ = new Map;
|
427 | }
|
428 | this.customCommands_.set(name, {method, path});
|
429 | }
|
430 |
|
431 |
|
432 | execute(command) {
|
433 | let request = buildRequest(this.customCommands_, this.w3c, command);
|
434 | return request.then(request => {
|
435 | this.log_.finer(() => `>>> ${request.method} ${request.path}`);
|
436 | return doSend(this, request).then(response => {
|
437 | this.log_.finer(() => `>>>\n${request}\n<<<\n${response}`);
|
438 |
|
439 | let parsed =
|
440 | parseHttpResponse( (response), this.w3c);
|
441 |
|
442 | if (command.getName() === cmd.Name.NEW_SESSION
|
443 | || command.getName() === cmd.Name.DESCRIBE_SESSION) {
|
444 | if (!parsed || !parsed['sessionId']) {
|
445 | throw new error.WebDriverError(
|
446 | 'Unable to parse new session response: ' + response.body);
|
447 | }
|
448 |
|
449 |
|
450 |
|
451 |
|
452 | if (command.getName() === cmd.Name.NEW_SESSION) {
|
453 | this.w3c = this.w3c || !('status' in parsed);
|
454 | }
|
455 |
|
456 | return new Session(parsed['sessionId'], parsed['value']);
|
457 | }
|
458 |
|
459 | if (parsed
|
460 | && typeof parsed === 'object'
|
461 | && 'value' in parsed) {
|
462 | let value = parsed['value'];
|
463 | return typeof value === 'undefined' ? null : value;
|
464 | }
|
465 | return parsed;
|
466 | });
|
467 | });
|
468 | }
|
469 | }
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 | function tryParse(str) {
|
477 | try {
|
478 | return JSON.parse(str);
|
479 | } catch (ignored) {
|
480 |
|
481 | }
|
482 | }
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 | function parseHttpResponse(httpResponse, w3c) {
|
495 | let parsed = tryParse(httpResponse.body);
|
496 | if (parsed !== undefined) {
|
497 | if (w3c) {
|
498 | if (httpResponse.status > 399) {
|
499 | error.throwDecodedError(parsed);
|
500 | }
|
501 |
|
502 | if (httpResponse.status < 200) {
|
503 |
|
504 |
|
505 | throw new error.WebDriverError(
|
506 | `Unexpected HTTP response:\n${httpResponse}`);
|
507 | }
|
508 | } else {
|
509 | error.checkLegacyResponse(parsed);
|
510 | }
|
511 | return parsed;
|
512 | }
|
513 |
|
514 | let value = httpResponse.body.replace(/\r\n/g, '\n');
|
515 |
|
516 |
|
517 |
|
518 | if (httpResponse.status == 404) {
|
519 | throw new error.UnsupportedOperationError(value);
|
520 | } else if (httpResponse.status >= 400) {
|
521 | throw new error.WebDriverError(value);
|
522 | }
|
523 |
|
524 | return value || null;
|
525 | }
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 | function buildPath(path, parameters) {
|
538 | let pathParameters = path.match(/\/:(\w+)\b/g);
|
539 | if (pathParameters) {
|
540 | for (let i = 0; i < pathParameters.length; ++i) {
|
541 | let key = pathParameters[i].substring(2);
|
542 | if (key in parameters) {
|
543 | let value = parameters[key];
|
544 | if (WebElement.isId(value)) {
|
545 |
|
546 |
|
547 | value = WebElement.extractId(value);
|
548 | }
|
549 | path = path.replace(pathParameters[i], '/' + value);
|
550 | delete parameters[key];
|
551 | } else {
|
552 | throw new error.InvalidArgumentError(
|
553 | 'Missing required parameter: ' + key);
|
554 | }
|
555 | }
|
556 | }
|
557 | return path;
|
558 | }
|
559 |
|
560 |
|
561 |
|
562 |
|
563 | exports.Executor = Executor;
|
564 | exports.Client = Client;
|
565 | exports.Request = Request;
|
566 | exports.Response = Response;
|
567 | exports.buildPath = buildPath;
|