@oracle/oraclejet-selenium-driver 19.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,334 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var seleniumWebdriver = require('selenium-webdriver');
6
+
7
+ /**
8
+ * @private
9
+ */
10
+ const defaultConfigName = '';
11
+ /**
12
+ * @private
13
+ */
14
+ const defaultCapabilities = new seleniumWebdriver.Capabilities({
15
+ autoWebview: true,
16
+ browserName: 'chrome'
17
+ });
18
+ /**
19
+ * @private
20
+ */
21
+ const defaultTimeouts = {
22
+ implicit: 0,
23
+ pageLoad: 30000,
24
+ script: 30000
25
+ };
26
+ /**
27
+ * @private
28
+ */
29
+ const configs = {};
30
+ /**
31
+ * @private
32
+ */
33
+ let currentInstance = {
34
+ configName: undefined,
35
+ driver: undefined
36
+ };
37
+ /**
38
+ * The delay, in milliseconds, that the manager will wait after the driver is
39
+ * release to see if there are pending requests for a driver instance. If no
40
+ * requests exist, then the driver is closed, and subsequent requests will
41
+ * receive a new instance.
42
+ * @private
43
+ */
44
+ const quitDelay = 500;
45
+ /**
46
+ * @private
47
+ */
48
+ let builder;
49
+ /**
50
+ * The NodeJS.Timer used to cleanup the driver instance if no subsequent test
51
+ * asks for the driver. The type (NodeJS.Timer) cannot be defined here because
52
+ * typedoc cannot resolve the NodeJS namespace, so we infer it by calling
53
+ * setTimeout instead.
54
+ * @private
55
+ */
56
+ let releaseTimer = setTimeout(() => true, 0);
57
+ /**
58
+ * Manage instances of [WebDriver](index._internal_.WebDriver.html)
59
+ * to be used by the tests. DriverManager
60
+ * allows a single place where the WebDriver instance can be configured and
61
+ * reused. Traditionally, tests instantiate the WebDriver instance on their own,
62
+ * passing configurations such as the browser to use:
63
+ * ```javascript
64
+ * let driver = new Builder().withCapabilities({
65
+ * browserName: "chrome"
66
+ * }).build()
67
+ * ```
68
+ * This boilerplate code then has to be copied to every test. Additionally, if
69
+ * the capabilities need to change (say, running against a different browser),
70
+ * that change must be applied to all test files.
71
+ *
72
+ * DriverManager addresses this problem by centralizing the place whereby
73
+ * the configuration is done, and persists that configuration for all
74
+ * subsequent requests for the WebDriver instance. The configuration only needs
75
+ * to be set once in a "setup" file, and tests retrieve the configured instance
76
+ * without needing to define anything else.
77
+ *
78
+ * A sample <code>mocha-setup.ts</code> file which configures DriverManager
79
+ * ```javascript
80
+ * import { DriverManager } from "@oracle/oraclejet-selenium-driver/DriverManager";
81
+ *
82
+ * DriverManager.registerConfig({
83
+ * capabilities: new Capabilities({
84
+ * browserName: "chrome"
85
+ * })
86
+ * })
87
+ * ```
88
+ * This setup test script should be run before any other tests.
89
+ * ```bash
90
+ * $ node node_modules/mocha/bin/mocha --require=ts-node/register mocha-setup.ts other-test.spec.ts ...
91
+ * ```
92
+ * Test files are agnostic of the driver configuration, and simply get the instance
93
+ * by calling <code>getDriver()</code>
94
+ * ```javascript
95
+ * import { DriverManager } from "@oracle/oraclejet-selenium-driver/DriverManager";
96
+ *
97
+ * describe("My test suite", function() {
98
+ * let driver: WebDriver;
99
+ *
100
+ * before(async function() {
101
+ * driver = await DriverManager.getDriver();
102
+ * })
103
+ * it("open a page to test", async function() {
104
+ * await driver.get("...")
105
+ * })
106
+ * after(async function() {
107
+ * await DriverManager.releaseDriver(driver);
108
+ * })
109
+ * })
110
+ * ```
111
+ *
112
+ * ## Set default timeouts for WebDriver from Mocha.
113
+ * When Mocha is used to start the WebDriver tests, there are two sets of timeout
114
+ * values--one from Mocha and one from WebDriver. This causes some confusion as
115
+ * the command-line argument <code>--timeout=nnn</code> is typically the only one
116
+ * set and assumed to be the only timeout value in play. However, WebDriver has
117
+ * its own set of timeout values whose defaults are sometimes longer than what's
118
+ * set for Mocha. When this happens, WebDriver may be waiting on a condition to
119
+ * timeout, but Mocha has errored the test because its own timeout was exceeded.
120
+ * To ensure that WebDriver timeout conditions are properly reported to the test
121
+ * runner (Mocha), its timeout values must be set to a value shorter than the
122
+ * runner's. This is typically done in the setup test, and set to some factor of
123
+ * the Mocha timeout.
124
+ *
125
+ * ### Set WebDriver timeout to 1/4 of Mocha
126
+ * ```javascript
127
+ * const mochaTimeout = this.timeouts();
128
+ * const wdTimeout = mochaTimeout / 4;
129
+ * DriverManager.registerConfig({
130
+ * timeouts: {
131
+ * pageLoad: wdTimeout,
132
+ * script: wdTimeout,
133
+ * implicit: wdTimeout
134
+ * }
135
+ * });
136
+ * ```
137
+ *
138
+ */
139
+ class DriverManager {
140
+ /**
141
+ * Optionally set the {@link Builder}
142
+ * instance used by DriverManager to create WebDriver.
143
+ * The Builder allows different settings for thigns such as the remote server,
144
+ * proxies, and other runtime options often needed when running in distributed
145
+ * environments.
146
+ * The instance can be preconfigured with capabilities, and any additional
147
+ * capabilities from [[registerConfig]] will also be applied during the
148
+ * creation process. If no Builder is explicitly passed, a default one will
149
+ * be used.
150
+ * If setting a custom Builder, this function must be called before the first
151
+ * test calls [[getDriver]], and must only be called once per test run. If called
152
+ * multiple times or after [[getDriver]], an error will be thrown.
153
+ * @param b A builder instance
154
+ */
155
+ static setBuilder(b) {
156
+ if (builder) {
157
+ throw Error('DriverManager Builder instance has already been set, and cannot be set again');
158
+ }
159
+ builder = b;
160
+ }
161
+ /**
162
+ * Register a configuration for WebDriver instances. Configurations consist of
163
+ * {@link Capabilities} and/or {@link Timeouts}.
164
+ * This configuration is used by [[getDriver]] to retrieve a configured WebDriver
165
+ * instance.
166
+ * @param config The driver configuration
167
+ * @param name An optional name to assocaite with the config. If no name is given,
168
+ * the config will be the default. If a name is given, its configuration is merged with the default
169
+ * with its values taking precedence.
170
+ *
171
+ * ### Register a default config
172
+ * ```javascript
173
+ * DriverManager.registerConfig(
174
+ * {
175
+ * capabilities: new Capabilities({
176
+ * browserName: "chrome"
177
+ * }),
178
+ * timeouts: {
179
+ * implicit: 5000
180
+ * }
181
+ * }
182
+ * );
183
+ * ```
184
+ * ### Register a Firefox config. These capabilities override any matching ones set in the default
185
+ * ```javascript
186
+ * DriverManager.registerConfig(
187
+ * {
188
+ * capabilities: new Capabilities({
189
+ * browserName: "firefox",
190
+ * hideAlerts: true
191
+ * })
192
+ * },
193
+ * "firefox-no-alerts"
194
+ * );
195
+ * ```
196
+ */
197
+ static registerConfig(config, name) {
198
+ configs[name || defaultConfigName] = config;
199
+ }
200
+ /**
201
+ * Get a {@link WebDriver}
202
+ * instance for a given configuration. If no configName is given, the returned
203
+ * driver will use the default configuration.
204
+ * @see [[registerConfig]]
205
+ *
206
+ * ### Get driver with default capabilities
207
+ * ```javascript
208
+ * let driver = await DriverManager.getDriver();
209
+ * ```
210
+ * ### Configure and get Firefox driver
211
+ * ```javascript
212
+ * // mocha-setup.ts
213
+ * DriverManager.registerConfig(
214
+ * {
215
+ * browserName: "firefox"
216
+ * },
217
+ * "firefox-config");
218
+ * // test.spec.ts
219
+ * let driver = await DriverManager.getDriver("firefox-config");
220
+ * ```
221
+ * @param configName An optional configuration name, registered through
222
+ * [[registerConfig]], whose set will be applied to the driver instance. If no
223
+ * name is given, the default configuration with the "chrome" browser
224
+ * will be used. If the given configName doesn't exist, an error will be thrown.
225
+ * @return A Promise that resolves to a WebDriver instance, configured with
226
+ * custom capabilities for the given configName, or the default capabilities
227
+ * if none is specified.
228
+ */
229
+ static async getDriver(configName) {
230
+ clearTimeout(releaseTimer);
231
+ configName = configName || defaultConfigName;
232
+ if (configName === currentInstance.configName) {
233
+ // Test if current driver is still valid
234
+ try {
235
+ if (currentInstance.driver) {
236
+ await currentInstance.driver.getCurrentUrl();
237
+ return currentInstance.driver;
238
+ }
239
+ }
240
+ catch {
241
+ // no driver or already quit
242
+ }
243
+ }
244
+ await quitCurrentDriver();
245
+ return createDriver(configName);
246
+ }
247
+ /**
248
+ * Gets the current driver instance, if one exists, otherwise create and
249
+ * return the default one. This method is useful for test setups which evaluate
250
+ * the outcome of the previous test to capture screenshots on failures. After-
251
+ * scripts call <code>getCurrentDriver</code> to get the instance of the driver
252
+ * that experienced the test failure, then capture the screenshot from it.
253
+ */
254
+ static async getCurrentDriver() {
255
+ return DriverManager.getDriver(currentInstance.configName);
256
+ }
257
+ /**
258
+ * Release the WebDriver instance from use. Called when each test is done
259
+ * with its driver usage, typically, in the <code>after/afterAll</code> function.
260
+ * If `immediate` is not given, this method will delay a brief time before telling
261
+ * the driver to quit, allowing subsequent tests to reuse the same instance.
262
+ * If a call to {@link getDriver} is called before the timeout is reached,
263
+ * then the release is aborted.
264
+ * If `immediate` is true, then a Promise<void> will be returned and the driver
265
+ * told to quit immediately.
266
+ * @param driver The WebDriver instance
267
+ * @param immediate Immediately release the driver without delay
268
+ * @returns Promise<void> A Promise that will resolve when the driver is released if
269
+ * `immediate` is true, otherwise, void
270
+ */
271
+ static releaseDriver(driver, immediate = false) {
272
+ clearTimeout(releaseTimer);
273
+ return new Promise((resolve) => {
274
+ if (immediate) {
275
+ resolve(quitDriver(driver));
276
+ }
277
+ else {
278
+ releaseTimer = setTimeout(() => quitDriver(driver), quitDelay);
279
+ resolve();
280
+ }
281
+ });
282
+ }
283
+ }
284
+ DriverManager.registerConfig({ capabilities: defaultCapabilities, timeouts: defaultTimeouts });
285
+ /**
286
+ * Create a WebDriver instance from the given configuration name. The config should
287
+ * already be registered via [registerConfig].
288
+ * @param configName The configuration name used to retrieve the configuration
289
+ * object for the driver instance
290
+ * @return A Promise which resolves to the configured WebDriver instance
291
+ * @private
292
+ */
293
+ async function createDriver(configName) {
294
+ const config = configs[configName];
295
+ if (!config) {
296
+ throw Error(`No driver configuration exists for "${configName}"`);
297
+ }
298
+ // Merge default capabilities with custom ones
299
+ const caps = new seleniumWebdriver.Capabilities(configs[defaultConfigName].capabilities);
300
+ if (config.capabilities) {
301
+ caps.merge(config.capabilities);
302
+ }
303
+ // Merge default timeouts with custom ones
304
+ const timeouts = Object.assign({}, configs[defaultConfigName].timeouts, config.timeouts);
305
+ if (!builder) {
306
+ builder = new seleniumWebdriver.Builder();
307
+ }
308
+ const driver = builder.withCapabilities(caps).build();
309
+ currentInstance = { configName, driver };
310
+ await driver.manage().setTimeouts(timeouts);
311
+ return driver;
312
+ }
313
+ /**
314
+ * @private
315
+ */
316
+ function quitCurrentDriver() {
317
+ return quitDriver(currentInstance.driver);
318
+ }
319
+ /**
320
+ * @private
321
+ */
322
+ function quitDriver(driver) {
323
+ if (driver) {
324
+ currentInstance = {
325
+ configName: undefined,
326
+ driver: undefined
327
+ };
328
+ return driver.quit().catch(console.warn);
329
+ }
330
+ return Promise.resolve();
331
+ }
332
+
333
+ exports.DriverManager = DriverManager;
334
+ //# sourceMappingURL=DriverManager.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DriverManager.cjs","sources":["../../src/DriverManager/DriverManager.ts"],"sourcesContent":["import { Builder, Capabilities, WebDriver } from 'selenium-webdriver';\n\ninterface Timeouts {\n /**\n * The timeout value for script execution. Default 30000\n */\n script?: number;\n /**\n * The timeout value for page loading (and to be ready). Default 30000\n */\n pageLoad?: number;\n /**\n * The timeout value for finding an element on the page. Default 0\n */\n implicit?: number;\n}\nexport interface DriverConfiguration {\n /**\n * The WebDriver Capabilties for the configuration\n */\n capabilities?: Capabilities | Record<string, any>;\n /**\n * The WebDriver timeout values for the configuration\n */\n timeouts?: Timeouts;\n}\n\n/**\n * @private\n */\nconst defaultConfigName = '';\n/**\n * @private\n */\nconst defaultCapabilities = new Capabilities({\n autoWebview: true,\n browserName: 'chrome'\n});\n/**\n * @private\n */\nconst defaultTimeouts: Timeouts = {\n implicit: 0,\n pageLoad: 30000,\n script: 30000\n};\n/**\n * @private\n */\nconst configs: Record<string, DriverConfiguration> = {};\n/**\n * @private\n */\nlet currentInstance: { configName: string | undefined; driver: WebDriver | undefined } = {\n configName: undefined,\n driver: undefined\n};\n\n/**\n * The delay, in milliseconds, that the manager will wait after the driver is\n * release to see if there are pending requests for a driver instance. If no\n * requests exist, then the driver is closed, and subsequent requests will\n * receive a new instance.\n * @private\n */\nconst quitDelay = 500;\n/**\n * @private\n */\nlet builder: Builder;\n/**\n * The NodeJS.Timer used to cleanup the driver instance if no subsequent test\n * asks for the driver. The type (NodeJS.Timer) cannot be defined here because\n * typedoc cannot resolve the NodeJS namespace, so we infer it by calling\n * setTimeout instead.\n * @private\n */\nlet releaseTimer = setTimeout(() => true, 0);\n\n/**\n * Manage instances of [WebDriver](index._internal_.WebDriver.html)\n * to be used by the tests. DriverManager\n * allows a single place where the WebDriver instance can be configured and\n * reused. Traditionally, tests instantiate the WebDriver instance on their own,\n * passing configurations such as the browser to use:\n * ```javascript\n * let driver = new Builder().withCapabilities({\n * browserName: \"chrome\"\n * }).build()\n * ```\n * This boilerplate code then has to be copied to every test. Additionally, if\n * the capabilities need to change (say, running against a different browser),\n * that change must be applied to all test files.\n *\n * DriverManager addresses this problem by centralizing the place whereby\n * the configuration is done, and persists that configuration for all\n * subsequent requests for the WebDriver instance. The configuration only needs\n * to be set once in a \"setup\" file, and tests retrieve the configured instance\n * without needing to define anything else.\n *\n * A sample <code>mocha-setup.ts</code> file which configures DriverManager\n * ```javascript\n * import { DriverManager } from \"@oracle/oraclejet-selenium-driver/DriverManager\";\n *\n * DriverManager.registerConfig({\n * capabilities: new Capabilities({\n * browserName: \"chrome\"\n * })\n * })\n * ```\n * This setup test script should be run before any other tests.\n * ```bash\n * $ node node_modules/mocha/bin/mocha --require=ts-node/register mocha-setup.ts other-test.spec.ts ...\n * ```\n * Test files are agnostic of the driver configuration, and simply get the instance\n * by calling <code>getDriver()</code>\n * ```javascript\n * import { DriverManager } from \"@oracle/oraclejet-selenium-driver/DriverManager\";\n *\n * describe(\"My test suite\", function() {\n * let driver: WebDriver;\n *\n * before(async function() {\n * driver = await DriverManager.getDriver();\n * })\n * it(\"open a page to test\", async function() {\n * await driver.get(\"...\")\n * })\n * after(async function() {\n * await DriverManager.releaseDriver(driver);\n * })\n * })\n * ```\n *\n * ## Set default timeouts for WebDriver from Mocha.\n * When Mocha is used to start the WebDriver tests, there are two sets of timeout\n * values--one from Mocha and one from WebDriver. This causes some confusion as\n * the command-line argument <code>--timeout=nnn</code> is typically the only one\n * set and assumed to be the only timeout value in play. However, WebDriver has\n * its own set of timeout values whose defaults are sometimes longer than what's\n * set for Mocha. When this happens, WebDriver may be waiting on a condition to\n * timeout, but Mocha has errored the test because its own timeout was exceeded.\n * To ensure that WebDriver timeout conditions are properly reported to the test\n * runner (Mocha), its timeout values must be set to a value shorter than the\n * runner's. This is typically done in the setup test, and set to some factor of\n * the Mocha timeout.\n *\n * ### Set WebDriver timeout to 1/4 of Mocha\n * ```javascript\n * const mochaTimeout = this.timeouts();\n * const wdTimeout = mochaTimeout / 4;\n * DriverManager.registerConfig({\n * timeouts: {\n * pageLoad: wdTimeout,\n * script: wdTimeout,\n * implicit: wdTimeout\n * }\n * });\n * ```\n *\n */\nexport class DriverManager {\n /**\n * Optionally set the {@link Builder}\n * instance used by DriverManager to create WebDriver.\n * The Builder allows different settings for thigns such as the remote server,\n * proxies, and other runtime options often needed when running in distributed\n * environments.\n * The instance can be preconfigured with capabilities, and any additional\n * capabilities from [[registerConfig]] will also be applied during the\n * creation process. If no Builder is explicitly passed, a default one will\n * be used.\n * If setting a custom Builder, this function must be called before the first\n * test calls [[getDriver]], and must only be called once per test run. If called\n * multiple times or after [[getDriver]], an error will be thrown.\n * @param b A builder instance\n */\n public static setBuilder(b: Builder) {\n if (builder) {\n throw Error('DriverManager Builder instance has already been set, and cannot be set again');\n }\n builder = b;\n }\n /**\n * Register a configuration for WebDriver instances. Configurations consist of\n * {@link Capabilities} and/or {@link Timeouts}.\n * This configuration is used by [[getDriver]] to retrieve a configured WebDriver\n * instance.\n * @param config The driver configuration\n * @param name An optional name to assocaite with the config. If no name is given,\n * the config will be the default. If a name is given, its configuration is merged with the default\n * with its values taking precedence.\n *\n * ### Register a default config\n * ```javascript\n * DriverManager.registerConfig(\n * {\n * capabilities: new Capabilities({\n * browserName: \"chrome\"\n * }),\n * timeouts: {\n * implicit: 5000\n * }\n * }\n * );\n * ```\n * ### Register a Firefox config. These capabilities override any matching ones set in the default\n * ```javascript\n * DriverManager.registerConfig(\n * {\n * capabilities: new Capabilities({\n * browserName: \"firefox\",\n * hideAlerts: true\n * })\n * },\n * \"firefox-no-alerts\"\n * );\n * ```\n */\n public static registerConfig(config: DriverConfiguration, name?: string): void {\n configs[name || defaultConfigName] = config;\n }\n\n /**\n * Get a {@link WebDriver}\n * instance for a given configuration. If no configName is given, the returned\n * driver will use the default configuration.\n * @see [[registerConfig]]\n *\n * ### Get driver with default capabilities\n * ```javascript\n * let driver = await DriverManager.getDriver();\n * ```\n * ### Configure and get Firefox driver\n * ```javascript\n * // mocha-setup.ts\n * DriverManager.registerConfig(\n * {\n * browserName: \"firefox\"\n * },\n * \"firefox-config\");\n * // test.spec.ts\n * let driver = await DriverManager.getDriver(\"firefox-config\");\n * ```\n * @param configName An optional configuration name, registered through\n * [[registerConfig]], whose set will be applied to the driver instance. If no\n * name is given, the default configuration with the \"chrome\" browser\n * will be used. If the given configName doesn't exist, an error will be thrown.\n * @return A Promise that resolves to a WebDriver instance, configured with\n * custom capabilities for the given configName, or the default capabilities\n * if none is specified.\n */\n public static async getDriver(configName?: string): Promise<WebDriver> {\n clearTimeout(releaseTimer);\n configName = configName || defaultConfigName;\n\n if (configName === currentInstance.configName) {\n // Test if current driver is still valid\n try {\n if (currentInstance.driver) {\n await currentInstance.driver.getCurrentUrl();\n return currentInstance.driver;\n }\n } catch {\n // no driver or already quit\n }\n }\n\n await quitCurrentDriver();\n return createDriver(configName);\n }\n\n /**\n * Gets the current driver instance, if one exists, otherwise create and\n * return the default one. This method is useful for test setups which evaluate\n * the outcome of the previous test to capture screenshots on failures. After-\n * scripts call <code>getCurrentDriver</code> to get the instance of the driver\n * that experienced the test failure, then capture the screenshot from it.\n */\n public static async getCurrentDriver(): Promise<WebDriver> {\n return DriverManager.getDriver(currentInstance.configName);\n }\n\n /**\n * Release the WebDriver instance from use. Called when each test is done\n * with its driver usage, typically, in the <code>after/afterAll</code> function.\n * If `immediate` is not given, this method will delay a brief time before telling\n * the driver to quit, allowing subsequent tests to reuse the same instance.\n * If a call to {@link getDriver} is called before the timeout is reached,\n * then the release is aborted.\n * If `immediate` is true, then a Promise<void> will be returned and the driver\n * told to quit immediately.\n * @param driver The WebDriver instance\n * @param immediate Immediately release the driver without delay\n * @returns Promise<void> A Promise that will resolve when the driver is released if\n * `immediate` is true, otherwise, void\n */\n public static releaseDriver(driver: WebDriver, immediate = false): Promise<void> {\n clearTimeout(releaseTimer);\n return new Promise((resolve) => {\n if (immediate) {\n resolve(quitDriver(driver));\n } else {\n releaseTimer = setTimeout(() => quitDriver(driver), quitDelay);\n resolve();\n }\n });\n }\n}\nDriverManager.registerConfig({ capabilities: defaultCapabilities, timeouts: defaultTimeouts });\n\n/**\n * Create a WebDriver instance from the given configuration name. The config should\n * already be registered via [registerConfig].\n * @param configName The configuration name used to retrieve the configuration\n * object for the driver instance\n * @return A Promise which resolves to the configured WebDriver instance\n * @private\n */\nasync function createDriver(configName: string) {\n const config = configs[configName];\n if (!config) {\n throw Error(`No driver configuration exists for \"${configName}\"`);\n }\n // Merge default capabilities with custom ones\n const caps = new Capabilities(configs[defaultConfigName].capabilities);\n if (config.capabilities) {\n caps.merge(config.capabilities);\n }\n // Merge default timeouts with custom ones\n const timeouts = Object.assign({}, configs[defaultConfigName].timeouts, config.timeouts);\n if (!builder) {\n builder = new Builder();\n }\n\n const driver = builder.withCapabilities(caps).build();\n currentInstance = { configName, driver };\n await driver.manage().setTimeouts(timeouts);\n return driver;\n}\n\n/**\n * @private\n */\nfunction quitCurrentDriver() {\n return quitDriver(currentInstance.driver);\n}\n\n/**\n * @private\n */\nfunction quitDriver(driver?: WebDriver) {\n if (driver) {\n currentInstance = {\n configName: undefined,\n driver: undefined\n };\n return driver.quit().catch(console.warn);\n }\n return Promise.resolve();\n}\n"],"names":["Capabilities","Builder"],"mappings":";;;;;;AA2BA;;AAEG;AACH,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAC7B;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der, Capabilities, WebDriver } from 'selenium-webdriver';
2
+ interface Timeouts {
3
+ /**
4
+ * The timeout value for script execution. Default 30000
5
+ */
6
+ script?: number;
7
+ /**
8
+ * The timeout value for page loading (and to be ready). Default 30000
9
+ */
10
+ pageLoad?: number;
11
+ /**
12
+ * The timeout value for finding an element on the page. Default 0
13
+ */
14
+ implicit?: number;
15
+ }
16
+ export interface DriverConfiguration {
17
+ /**
18
+ * The WebDriver Capabilties for the configuration
19
+ */
20
+ capabilities?: Capabilities | Record<string, any>;
21
+ /**
22
+ * The WebDriver timeout values for the configuration
23
+ */
24
+ timeouts?: Timeouts;
25
+ }
26
+ /**
27
+ * Manage instances of [WebDriver](index._internal_.WebDriver.html)
28
+ * to be used by the tests. DriverManager
29
+ * allows a single place where the WebDriver instance can be configured and
30
+ * reused. Traditionally, tests instantiate the WebDriver instance on their own,
31
+ * passing configurations such as the browser to use:
32
+ * ```javascript
33
+ * let driver = new Builder().withCapabilities({
34
+ * browserName: "chrome"
35
+ * }).build()
36
+ * ```
37
+ * This boilerplate code then has to be copied to every test. Additionally, if
38
+ * the capabilities need to change (say, running against a different browser),
39
+ * that change must be applied to all test files.
40
+ *
41
+ * DriverManager addresses this problem by centralizing the place whereby
42
+ * the configuration is done, and persists that configuration for all
43
+ * subsequent requests for the WebDriver instance. The configuration only needs
44
+ * to be set once in a "setup" file, and tests retrieve the configured instance
45
+ * without needing to define anything else.
46
+ *
47
+ * A sample <code>mocha-setup.ts</code> file which configures DriverManager
48
+ * ```javascript
49
+ * import { DriverManager } from "@oracle/oraclejet-selenium-driver/DriverManager";
50
+ *
51
+ * DriverManager.registerConfig({
52
+ * capabilities: new Capabilities({
53
+ * browserName: "chrome"
54
+ * })
55
+ * })
56
+ * ```
57
+ * This setup test script should be run before any other tests.
58
+ * ```bash
59
+ * $ node node_modules/mocha/bin/mocha --require=ts-node/register mocha-setup.ts other-test.spec.ts ...
60
+ * ```
61
+ * Test files are agnostic of the driver configuration, and simply get the instance
62
+ * by calling <code>getDriver()</code>
63
+ * ```javascript
64
+ * import { DriverManager } from "@oracle/oraclejet-selenium-driver/DriverManager";
65
+ *
66
+ * describe("My test suite", function() {
67
+ * let driver: WebDriver;
68
+ *
69
+ * before(async function() {
70
+ * driver = await DriverManager.getDriver();
71
+ * })
72
+ * it("open a page to test", async function() {
73
+ * await driver.get("...")
74
+ * })
75
+ * after(async function() {
76
+ * await DriverManager.releaseDriver(driver);
77
+ * })
78
+ * })
79
+ * ```
80
+ *
81
+ * ## Set default timeouts for WebDriver from Mocha.
82
+ * When Mocha is used to start the WebDriver tests, there are two sets of timeout
83
+ * values--one from Mocha and one from WebDriver. This causes some confusion as
84
+ * the command-line argument <code>--timeout=nnn</code> is typically the only one
85
+ * set and assumed to be the only timeout value in play. However, WebDriver has
86
+ * its own set of timeout values whose defaults are sometimes longer than what's
87
+ * set for Mocha. When this happens, WebDriver may be waiting on a condition to
88
+ * timeout, but Mocha has errored the test because its own timeout was exceeded.
89
+ * To ensure that WebDriver timeout conditions are properly reported to the test
90
+ * runner (Mocha), its timeout values must be set to a value shorter than the
91
+ * runner's. This is typically done in the setup test, and set to some factor of
92
+ * the Mocha timeout.
93
+ *
94
+ * ### Set WebDriver timeout to 1/4 of Mocha
95
+ * ```javascript
96
+ * const mochaTimeout = this.timeouts();
97
+ * const wdTimeout = mochaTimeout / 4;
98
+ * DriverManager.registerConfig({
99
+ * timeouts: {
100
+ * pageLoad: wdTimeout,
101
+ * script: wdTimeout,
102
+ * implicit: wdTimeout
103
+ * }
104
+ * });
105
+ * ```
106
+ *
107
+ */
108
+ export declare class DriverManager {
109
+ /**
110
+ * Optionally set the {@link Builder}
111
+ * instance used by DriverManager to create WebDriver.
112
+ * The Builder allows different settings for thigns such as the remote server,
113
+ * proxies, and other runtime options often needed when running in distributed
114
+ * environments.
115
+ * The instance can be preconfigured with capabilities, and any additional
116
+ * capabilities from [[registerConfig]] will also be applied during the
117
+ * creation process. If no Builder is explicitly passed, a default one will
118
+ * be used.
119
+ * If setting a custom Builder, this function must be called before the first
120
+ * test calls [[getDriver]], and must only be called once per test run. If called
121
+ * multiple times or after [[getDriver]], an error will be thrown.
122
+ * @param b A builder instance
123
+ */
124
+ static setBuilder(b: Builder): void;
125
+ /**
126
+ * Register a configuration for WebDriver instances. Configurations consist of
127
+ * {@link Capabilities} and/or {@link Timeouts}.
128
+ * This configuration is used by [[getDriver]] to retrieve a configured WebDriver
129
+ * instance.
130
+ * @param config The driver configuration
131
+ * @param name An optional name to assocaite with the config. If no name is given,
132
+ * the config will be the default. If a name is given, its configuration is merged with the default
133
+ * with its values taking precedence.
134
+ *
135
+ * ### Register a default config
136
+ * ```javascript
137
+ * DriverManager.registerConfig(
138
+ * {
139
+ * capabilities: new Capabilities({
140
+ * browserName: "chrome"
141
+ * }),
142
+ * timeouts: {
143
+ * implicit: 5000
144
+ * }
145
+ * }
146
+ * );
147
+ * ```
148
+ * ### Register a Firefox config. These capabilities override any matching ones set in the default
149
+ * ```javascript
150
+ * DriverManager.registerConfig(
151
+ * {
152
+ * capabilities: new Capabilities({
153
+ * browserName: "firefox",
154
+ * hideAlerts: true
155
+ * })
156
+ * },
157
+ * "firefox-no-alerts"
158
+ * );
159
+ * ```
160
+ */
161
+ static registerConfig(config: DriverConfiguration, name?: string): void;
162
+ /**
163
+ * Get a {@link WebDriver}
164
+ * instance for a given configuration. If no configName is given, the returned
165
+ * driver will use the default configuration.
166
+ * @see [[registerConfig]]
167
+ *
168
+ * ### Get driver with default capabilities
169
+ * ```javascript
170
+ * let driver = await DriverManager.getDriver();
171
+ * ```
172
+ * ### Configure and get Firefox driver
173
+ * ```javascript
174
+ * // mocha-setup.ts
175
+ * DriverManager.registerConfig(
176
+ * {
177
+ * browserName: "firefox"
178
+ * },
179
+ * "firefox-config");
180
+ * // test.spec.ts
181
+ * let driver = await DriverManager.getDriver("firefox-config");
182
+ * ```
183
+ * @param configName An optional configuration name, registered through
184
+ * [[registerConfig]], whose set will be applied to the driver instance. If no
185
+ * name is given, the default configuration with the "chrome" browser
186
+ * will be used. If the given configName doesn't exist, an error will be thrown.
187
+ * @return A Promise that resolves to a WebDriver instance, configured with
188
+ * custom capabilities for the given configName, or the default capabilities
189
+ * if none is specified.
190
+ */
191
+ static getDriver(configName?: string): Promise<WebDriver>;
192
+ /**
193
+ * Gets the current driver instance, if one exists, otherwise create and
194
+ * return the default one. This method is useful for test setups which evaluate
195
+ * the outcome of the previous test to capture screenshots on failures. After-
196
+ * scripts call <code>getCurrentDriver</code> to get the instance of the driver
197
+ * that experienced the test failure, then capture the screenshot from it.
198
+ */
199
+ static getCurrentDriver(): Promise<WebDriver>;
200
+ /**
201
+ * Release the WebDriver instance from use. Called when each test is done
202
+ * with its driver usage, typically, in the <code>after/afterAll</code> function.
203
+ * If `immediate` is not given, this method will delay a brief time before telling
204
+ * the driver to quit, allowing subsequent tests to reuse the same instance.
205
+ * If a call to {@link getDriver} is called before the timeout is reached,
206
+ * then the release is aborted.
207
+ * If `immediate` is true, then a Promise<void> will be returned and the driver
208
+ * told to quit immediately.
209
+ * @param driver The WebDriver instance
210
+ * @param immediate Immediately release the driver without delay
211
+ * @returns Promise<void> A Promise that will resolve when the driver is released if
212
+ * `immediate` is true, otherwise, void
213
+ */
214
+ static releaseDriver(driver: WebDriver, immediate?: boolean): Promise<void>;
215
+ }
216
+ export {};