Static
getGets the current driver instance, if one exists, otherwise create and
return the default one. This method is useful for test setups which evaluate
the outcome of the previous test to capture screenshots on failures. After-
scripts call getCurrentDriver
to get the instance of the driver
that experienced the test failure, then capture the screenshot from it.
Static
getGet a WebDriver instance for a given configuration. If no configName is given, the returned driver will use the default configuration.
Optional
configName: stringAn optional configuration name, registered through [[registerConfig]], whose set will be applied to the driver instance. If no name is given, the default configuration with the "chrome" browser will be used. If the given configName doesn't exist, an error will be thrown.
A Promise that resolves to a WebDriver instance, configured with custom capabilities for the given configName, or the default capabilities if none is specified.
[[registerConfig]]
let driver = await DriverManager.getDriver();
// mocha-setup.ts
DriverManager.registerConfig(
{
browserName: "firefox"
},
"firefox-config");
// test.spec.ts
let driver = await DriverManager.getDriver("firefox-config");
Static
registerRegister a configuration for WebDriver instances. Configurations consist of Capabilities and/or Timeouts. This configuration is used by [[getDriver]] to retrieve a configured WebDriver instance.
The driver configuration
Optional
name: stringAn optional name to assocaite with the config. If no name is given, the config will be the default.
DriverManager.registerConfig(
{
capabilities: new Capabilities({
browserName: "chrome"
}),
timeouts: {
implicit: 5000
}
}
);
DriverManager.registerConfig(
{
capabilities: new Capabilities({
browserName: "firefox",
hideAlerts: true
})
},
"firefox-no-alerts"
);
Static
releaseRelease the WebDriver instance from use. Called when each test is done
with its driver usage, typicall, in the Mocha after
function.
The WebDriver instance
Static
setOptionally set the Builder instance used by DriverManager to create WebDriver. The Builder allows different settings for thigns such as the remote server, proxies, and other runtime options often needed when running in distributed environments. The instance can be preconfigured with capabilities, and any additional capabilities from [[registerConfig]] will also be applied during the creation process. If no Builder is explicitly passed, a default one will be used. If setting a custom Builder, this function must be called before the first test calls [[getDriver]], and must only be called once per test run. If called multiple times or after [[getDriver]], an error will be thrown.
A builder instance
Generated using TypeDoc
Manage instances of WebDriver to be used by the tests. DriverManager allows a single place where the WebDriver instance can be configured and reused. Traditionally, tests instantiate the WebDriver instance on their own, passing configurations such as the browser to use:
This boilerplate code then has to be copied to every test. Additionally, if the capabilities need to change (say, running against a different browser), that change must be applied to all test files.
DriverManager addresses this problem by centralizing the place whereby the configuration is done, and persists that configuration for all subsequent requests for the WebDriver instance. The configuration only needs to be set once in a "setup" file, and tests retrieve the configured instance without needing to define anything else.
A sample
mocha-setup.ts
file which configures DriverManagerThis setup test script should be run before any other tests.
Test files are agnostic of the driver configuration, and simply get the instance by calling
getDriver()
Set default timeouts for WebDriver from Mocha.
When Mocha is used to start the WebDriver tests, there are two sets of timeout values--one from Mocha and one from WebDriver. This causes some confusion as the command-line argument
--timeout=nnn
is typically the only one set and assumed to be the only timeout value in play. However, WebDriver has its own set of timeout values whose defaults are sometimes longer than what's set for Mocha. When this happens, WebDriver may be waiting on a condition to timeout, but Mocha has errored the test because its own timeout was exceeded. To ensure that WebDriver timeout conditions are properly reported to the test runner (Mocha), its timeout values must be set to a value shorter than the runner's. This is typically done in the setup test, and set to some factor of the Mocha timeout.Set WebDriver timeout to 1/4 of Mocha