UNPKG

5.68 kBTypeScriptView Raw
1import * as webdriver from "./index";
2
3/**
4 * Converts a headers map to a HTTP header block string.
5 * @param {!Map<string, string>} headers The map to convert.
6 * @return {string} The headers as a string.
7 */
8export function headersToString(headers: any): string;
9
10/**
11 * Represents a HTTP request message. This class is a 'partial' request and only
12 * defines the path on the server to send a request to. It is each client's
13 * responsibility to build the full URL for the final request.
14 * @final
15 */
16export class Request {
17 /**
18 * @param {string} method The HTTP method to use for the request.
19 * @param {string} path The path on the server to send the request to.
20 * @param {Object=} opt_data This request's non-serialized JSON payload data.
21 */
22 constructor(method: string, path: string, opt_data?: Object);
23
24 /** @override */
25 toString(): string;
26}
27
28/**
29 * Represents a HTTP response message.
30 * @final
31 */
32export class Response {
33 /**
34 * @param {number} status The response code.
35 * @param {!Object<string>} headers The response headers. All header names
36 * will be converted to lowercase strings for consistent lookups.
37 * @param {string} body The response body.
38 */
39 constructor(status: number, headers: Object, body: string);
40
41 /** @override */
42 toString(): string;
43}
44
45export function post(path: string): any;
46export function del(path: string): any;
47export function get(path: string): any;
48export function resource(method: string, path: string): any;
49
50/**
51 * A basic HTTP client used to send messages to a remote end.
52 */
53export class HttpClient {
54 /**
55 * @param {string} serverUrl URL for the WebDriver server to send commands to.
56 * @param {http.Agent=} opt_agent The agent to use for each request.
57 * Defaults to `http.globalAgent`.
58 * @param {?string=} opt_proxy The proxy to use for the connection to the
59 * server. Default is to use no proxy.
60 */
61 constructor(serverUrl: string, opt_agent?: any, opt_proxy?: string);
62
63 /**
64 * Sends a request to the server. The client will automatically follow any
65 * redirects returned by the server, fulfilling the returned promise with
66 * the final response.
67 *
68 * @param {!HttpRequest} httpRequest The request to send.
69 * @return {!Promise<HttpResponse>} A promise that will be fulfilled
70 * with the server's response.
71 */
72 send(httpRequest: Request): Promise<Response>;
73}
74
75/**
76 * Sends a single HTTP request.
77 * @param {!Object} options The request options.
78 * @param {function(!HttpResponse)} onOk The function to call if the
79 * request succeeds.
80 * @param {function(!Error)} onError The function to call if the request fails.
81 * @param {?string=} opt_data The data to send with the request.
82 * @param {?string=} opt_proxy The proxy server to use for the request.
83 */
84export function sendRequest(
85 options: Object,
86 onOk: any,
87 onError: any,
88 opt_data?: string,
89 opt_proxy?: string,
90): any;
91
92/**
93 * A command executor that communicates with the server using HTTP + JSON.
94 *
95 * By default, each instance of this class will use the legacy wire protocol
96 * from [Selenium project][json]. The executor will automatically switch to the
97 * [W3C wire protocol][w3c] if the remote end returns a compliant response to
98 * a new session command.
99 *
100 * [json]: https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol
101 * [w3c]: https://w3c.github.io/webdriver/webdriver-spec.html
102 */
103export class Executor {
104 /**
105 * @param {!(HttpClient|IThenable<!HttpClient>)} client The client to use for sending
106 * requests to the server, or a promise-like object that will resolve to
107 * to the client.
108 */
109 constructor(client: HttpClient | Promise<HttpClient>);
110
111 /**
112 * Defines a new command for use with this executor. When a command is sent,
113 * the {@code path} will be preprocessed using the command's parameters; any
114 * path segments prefixed with ':' will be replaced by the parameter of the
115 * same name. For example, given '/person/:name' and the parameters
116 * '{name: 'Bob'}', the final command path will be '/person/Bob'.
117 *
118 * @param {string} name The command name.
119 * @param {string} method The HTTP method to use when sending this command.
120 * @param {string} path The path to send the command to, relative to
121 * the WebDriver server's command root and of the form
122 * '/path/:variable/segment'.
123 */
124 defineCommand(name: string, method: string, path: string): void;
125
126 /** @override */
127 execute(command: any): any;
128}
129
130/**
131 * @param {string} str .
132 * @return {?} .
133 */
134export function tryParse(str: string): any;
135
136/**
137 * Callback used to parse {@link HttpResponse} objects from a
138 * {@link HttpClient}.
139 * @param {!HttpResponse} httpResponse The HTTP response to parse.
140 * @param {boolean} w3c Whether the response should be processed using the
141 * W3C wire protocol.
142 * @return {{value: ?}} The parsed response.
143 * @throws {WebDriverError} If the HTTP response is an error.
144 */
145export function parseHttpResponse(httpResponse: Response, w3c: boolean): any;
146
147/**
148 * Builds a fully qualified path using the given set of command parameters. Each
149 * path segment prefixed with ':' will be replaced by the value of the
150 * corresponding parameter. All parameters spliced into the path will be
151 * removed from the parameter map.
152 * @param {string} path The original resource path.
153 * @param {!Object<*>} parameters The parameters object to splice into the path.
154 * @return {string} The modified path.
155 */
156export function buildPath(path: string, parameters: Object): string;