NOTE: This module is not quite ready! Only a few commands work, and it's hard-wired to use Phantomjs. More to follow though.
;var webdriver =By = webdriverByuntil = webdriveruntil;var driver =;;
There are four improvements that we'd like to make:
- avoid having to nest every WebDriver command in a simple function (for example,
() => driver.getTitle());
- be able to use promise-related libraries like Chai as Promised;
- be able to dispense with
catch()in Mocha by returning a promise;
- be able to use
untilwithout having to repeat the declarations at the top of every test file.
The first three points should all be possible, but since the promises that WebDriver returns are not 'compatible' with those used in other libraries -- such as Mocha and Chai as Promised -- they all fail.
For example, to use WebDriver commands like
getTitle() we are obliged to nest them in a simple function:
However, what we should be able to do is simply pass a reference to the WebDriver function, like this:
Although WebDriver claims to return promises for commands the type of promises being used are not compatible with other libraries -- like Mocha -- and what should be a chain of promises turns out not to be (i.e., the promises are not being 'followed'). To get this to work we have to wrap calls to WebDriver functions with a more 'standard' promise:
To make this wrapping easier, the
webdriver-mocha module wraps all of the WebDriver commands with 'proper' promises and therefore allows the more direct syntax.
Note that doing this wrapping makes it possible to use Chai as Promised, which won't work with the promises that
WebDriver is using. It also means that we can take advantage of a key benefit of Mocha which is that if a test returns a promise then Mocha will handle rejected promises. This means we can dispense with calling
done() to terminate the test, and avoid the need to provide the
Easy Inclusion in Mocha
Rather than having to declare the imports and variables for WebDriver at the top of each test file we'd like to incude them as globals to Mocha. This module also facilitates that, making
Note that since this module takes responsibility for creating
driver it also takes responsibility for calling
driver.quit() when all tests are complete.
The example we began with can now be expressed like this:
let chai = ;chai;chai;;;
Note the addition of Chai as Promised, the removal of
driver.quit(), and -- thanks to the
return statement at the start of the test -- the omission of the two
webdriver-mocha module has given us:
- access to
untilin exactly the same way as many snippets of code to be found on the web, making it easy to include them, and;
- the use of 'proper' promises on the commands, improving the integration with other libraries that use promises, such as Chai as Promised, and of course Mocha itself.
Using With Linting Tools
Be sure to add
until as globals in your linting tool. For example, for ESLint we have:
See the two
.eslintrc files in this project for an example.