executors.js

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 Various utilities for working with
20 * {@link webdriver.CommandExecutor} implementations.
21 */
22
23var HttpClient = require('./http').HttpClient,
24 HttpExecutor = require('./http').Executor,
25 promise = require('./_base').require('webdriver.promise');
26
27
28
29/**
30 * Wraps a promised {@link webdriver.CommandExecutor}, ensuring no commands
31 * are executed until the wrapped executor has been fully built.
32 * @param {!webdriver.promise.Promise.<!webdriver.CommandExecutor>} delegate The
33 * promised delegate.
34 * @constructor
35 * @implements {webdriver.CommandExecutor}
36 */
37var DeferredExecutor = function(delegate) {
38
39 /** @override */
40 this.execute = function(command, callback) {
41 delegate.then(function(executor) {
42 executor.execute(command, callback);
43 }, callback);
44 };
45};
46
47
48// PUBLIC API
49
50
51exports.DeferredExecutor = DeferredExecutor;
52
53/**
54 * Creates a command executor that uses WebDriver's JSON wire protocol.
55 * @param {(string|!webdriver.promise.Promise.<string>)} url The server's URL,
56 * or a promise that will resolve to that URL.
57 * @param {string=} opt_proxy (optional) The URL of the HTTP proxy for the
58 * client to use.
59 * @returns {!webdriver.CommandExecutor} The new command executor.
60 */
61exports.createExecutor = function(url, opt_proxy) {
62 return new DeferredExecutor(promise.when(url, function(url) {
63 var client = new HttpClient(url, null, opt_proxy);
64 return new HttpExecutor(client);
65 }));
66};