# executors.js

1 | // Copyright 2013 Software Freedom Conservancy. All Rights Reserved.
2 | //
3 | // Licensed under the Apache License, Version 2.0 (the "License");
4 | // you may not use this file except in compliance with the License.
5 | // You may obtain a copy of the License at
6 | //
7 | // http://www.apache.org/licenses/LICENSE-2.0
8 | //
9 | // Unless required by applicable law or agreed to in writing, software
10 | // distributed under the License is distributed on an "AS IS" BASIS,
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | // See the License for the specific language governing permissions and
13 | // limitations under the License.
14 |
15 | /**
16 | * @fileoverview Various utilities for working with
17 | * {@link webdriver.CommandExecutor} implementations.
18 | */
19 |
20 | var HttpClient = require('./http').HttpClient,
21 | HttpExecutor = require('./http').Executor,
22 | promise = require('./_base').require('webdriver.promise');
23 |
24 |
25 |
26 | /**
27 | * Wraps a promised {@link webdriver.CommandExecutor}, ensuring no commands
28 | * are executed until the wrapped executor has been fully built.
29 | * @param {!webdriver.promise.Promise.<!webdriver.CommandExecutor>} delegate The
30 | * promised delegate.
31 | * @constructor
32 | * @implements {webdriver.CommandExecutor}
33 | */
34 | var DeferredExecutor = function(delegate) {
35 |
36 | /** @override */
37 | this.execute = function(command, callback) {
38 | delegate.then(function(executor) {
39 | executor.execute(command, callback);
40 | }, callback);
41 | };
42 | };
43 |
44 |
45 | // PUBLIC API
46 |
47 |
48 | exports.DeferredExecutor = DeferredExecutor;
49 |
50 | /**
51 | * Creates a command executor that uses WebDriver's JSON wire protocol.
52 | * @param {(string|!webdriver.promise.Promise.<string>)} url The server's URL,
53 | * or a promise that will resolve to that URL.
54 | * @returns {!webdriver.CommandExecutor} The new command executor.
55 | */
56 | exports.createExecutor = function(url) {
57 | return new DeferredExecutor(promise.when(url, function(url) {
58 | var client = new HttpClient(url);
59 | return new HttpExecutor(client);
60 | }));
61 | };