UNPKG

8.99 kBTypeScriptView Raw
1import * as webdriver from "./index";
2
3/**
4 * A record object that defines the configuration options for a DriverService
5 * instance.
6 *
7 * @record
8 */
9export interface ServiceOptions {}
10
11/**
12 * Manages the life and death of a native executable WebDriver server.
13 *
14 * It is expected that the driver server implements the
15 * https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol.
16 * Furthermore, the managed server should support multiple concurrent sessions,
17 * so that this class may be reused for multiple clients.
18 */
19export class DriverService {
20 /**
21 * @param {string} executable Path to the executable to run.
22 * @param {!ServiceOptions} options Configuration options for the service.
23 */
24 constructor(executable: string, options: ServiceOptions);
25
26 /**
27 * @return {!promise.Promise<string>} A promise that resolves to
28 * the server's address.
29 * @throws {Error} If the server has not been started.
30 */
31 address(): Promise<string>;
32
33 /**
34 * Returns whether the underlying process is still running. This does not take
35 * into account whether the process is in the process of shutting down.
36 * @return {boolean} Whether the underlying service process is running.
37 */
38 isRunning(): boolean;
39
40 /**
41 * Starts the server if it is not already running.
42 * @param {number=} opt_timeoutMs How long to wait, in milliseconds, for the
43 * server to start accepting requests. Defaults to 30 seconds.
44 * @return {!promise.Promise<string>} A promise that will resolve
45 * to the server's base URL when it has started accepting requests. If the
46 * timeout expires before the server has started, the promise will be
47 * rejected.
48 */
49 start(opt_timeoutMs?: number): Promise<string>;
50
51 /**
52 * Stops the service if it is not currently running. This function will kill
53 * the server immediately. To synchronize with the active control flow, use
54 * {@link #stop()}.
55 * @return {!promise.Promise} A promise that will be resolved when
56 * the server has been stopped.
57 */
58 kill(): Promise<any>;
59}
60
61export namespace DriverService {
62 /**
63 * Creates {@link DriverService} objects that manage a WebDriver server in a
64 * child process.
65 */
66 class Builder {
67 /**
68 * @param {string} exe Path to the executable to use. This executable must
69 * accept the `--port` flag for defining the port to start the server on.
70 * @throws {Error} If the provided executable path does not exist.
71 */
72 constructor(exe: string);
73
74 /**
75 * Define additional command line arguments to use when starting the server.
76 *
77 * @param {...CommandLineFlag} var_args The arguments to include.
78 * @return {!THIS} A self reference.
79 * @template THIS
80 */
81 addArguments(...var_args: string[]): this;
82
83 /**
84 * Sets the host name to access the server on. If specified, the
85 * {@linkplain #setLoopback() loopback} setting will be ignored.
86 *
87 * @param {string} hostname
88 * @return {!DriverService.Builder} A self reference.
89 */
90 setHostname(hostname: string): this;
91
92 /**
93 * Sets whether the service should be accessed at this host's loopback
94 * address.
95 *
96 * @param {boolean} loopback
97 * @return {!DriverService.Builder} A self reference.
98 */
99 setLoopback(loopback: boolean): this;
100
101 /**
102 * Sets the base path for WebDriver REST commands (e.g. "/wd/hub").
103 * By default, the driver will accept commands relative to "/".
104 *
105 * @param {?string} basePath The base path to use, or `null` to use the
106 * default.
107 * @return {!DriverService.Builder} A self reference.
108 */
109 setPath(basePath: string | null): this;
110
111 /**
112 * Sets the port to start the server on.
113 *
114 * @param {number} port The port to use, or 0 for any free port.
115 * @return {!DriverService.Builder} A self reference.
116 * @throws {Error} If an invalid port is specified.
117 */
118 setPort(port: number): this;
119
120 /**
121 * Defines the environment to start the server under. This setting will be
122 * inherited by every browser session started by the server. By default, the
123 * server will inherit the enviroment of the current process.
124 *
125 * @param {(Map<string, string>|Object<string, string>|null)} env The desired
126 * environment to use, or `null` if the server should inherit the
127 * current environment.
128 * @return {!DriverService.Builder} A self reference.
129 */
130 setEnvironment(env: Map<string, string> | { [name: string]: string } | null): this;
131
132 /**
133 * IO configuration for the spawned server process. For more information,
134 * refer to the documentation of `child_process.spawn`.
135 *
136 * @param {StdIoOptions} config The desired IO configuration.
137 * @return {!DriverService.Builder} A self reference.
138 * @see https://nodejs.org/dist/latest-v4.x/docs/api/child_process.html#child_process_options_stdio
139 */
140 setStdio(config: any): this;
141
142 /**
143 * Creates a new DriverService using this instance's current configuration.
144 *
145 * @return {!DriverService} A new driver service.
146 */
147 build(): DriverService;
148 }
149}
150
151/**
152 * Manages the life and death of the
153 * <a href="http://selenium-release.storage.googleapis.com/index.html">
154 * standalone Selenium server</a>.
155 */
156export class SeleniumServer extends DriverService {
157 /**
158 * @param {string} jar Path to the Selenium server jar.
159 * @param {SeleniumServer.Options=} opt_options Configuration options for the
160 * server.
161 * @throws {Error} If the path to the Selenium jar is not specified or if an
162 * invalid port is specified.
163 */
164 constructor(jar: string, opt_options?: SeleniumServer.Options);
165}
166
167export namespace SeleniumServer {
168 /**
169 * Options for the Selenium server
170 */
171 interface Options {
172 /** Whether the server should only be accessed on this host's loopback address.*/
173 loopback?: boolean | undefined;
174
175 /** The port to start the server on (must be > 0). If the port is provided
176 as a promise, the service will wait for the promise to resolve before starting. */
177 port?: number | Promise<number> | undefined;
178
179 /** The arguments to pass to the service. If a promise is provided, the
180 service will wait for it to resolve before starting. */
181 args?: string[] | Promise<string[]> | undefined;
182
183 /** The arguments to pass to the JVM. If a promise is provided, the service
184 will wait for it to resolve before starting. */
185 jvmArgs?: string[] | Promise<string[]> | undefined;
186
187 /** The environment variables that should be visible to the server process.
188 Defaults to inheriting the current process's environment.*/
189 env?: { [key: string]: string } | undefined;
190
191 /** IO configuration for the spawned server process. For more information,
192 refer to the documentation of `child_process.spawn`*/
193 stdio?: string | Array<string | number> | undefined;
194 }
195}
196
197/**
198 * A {@link webdriver.FileDetector} that may be used when running
199 * against a remote
200 * [Selenium server](http://selenium-release.storage.googleapis.com/index.html).
201 *
202 * When a file path on the local machine running this script is entered with
203 * {@link webdriver.WebElement#sendKeys WebElement#sendKeys}, this file detector
204 * will transfer the specified file to the Selenium server's host; the sendKeys
205 * command will be updated to use the transfered file's path.
206 *
207 * __Note:__ This class depends on a non-standard command supported on the
208 * Java Selenium server. The file detector will fail if used with a server that
209 * only supports standard WebDriver commands (such as the ChromeDriver).
210 *
211 * @final
212 */
213export class FileDetector extends webdriver.FileDetector {
214 /** */
215 constructor();
216
217 /**
218 * Prepares a `file` for use with the remote browser. If the provided path
219 * does not reference a normal file (i.e. it does not exist or is a
220 * directory), then the promise returned by this method will be resolved
221 * with the original file path. Otherwise, this method will upload the file
222 * to the remote server, which will return the file's path on the remote
223 * system so it may be referenced in subsequent commands.
224 *
225 * @param {!webdriver.WebDriver} driver The driver for the current browser.
226 * @param {string} file The path of the file to process.
227 * @return {!Promise<string>} A promise for the processed
228 * file path.
229 */
230 handleFile(driver: webdriver.WebDriver, file: string): Promise<string>;
231}