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 | /** |
49 | * Creates a command executor that uses WebDriver's JSON wire protocol. |
50 | * @param {(string|!webdriver.promise.Promise.<string>)} url The server's URL, |
51 | * or a promise that will resolve to that URL. |
52 | * @returns {!webdriver.CommandExecutor} The new command executor. |
53 | */ |
54 | exports.createExecutor = function(url) { |
55 | return new DeferredExecutor(promise.when(url, function(url) { |
56 | var client = new HttpClient(url); |
57 | return new HttpExecutor(client); |
58 | })); |
59 | }; |