Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Node.js + mocha + selenium-webdriver
var SeleniumServer = require('selenium-webdriver/remote').SeleniumServer;
var webdriver = require('selenium-webdriver');
var promise = webdriver.promise;
var path = require('path');
var SELENIUM_SERVER_JAR = path.resolve(__dirname, 'selenium-server-standalone-2.45.0.jar');
function DriverProvider() {
this._server = null;
this._driver = null;
DriverProvider.prototype = {
startServer: function () {
this._server = new SeleniumServer(SELENIUM_SERVER_JAR, {port: 4444});
return this._server.start();
stopServer: function () {
return this._server.stop();
buildDriver: function () {
this._driver = new webdriver.Builder()
if (!this._driver) {
throw new Error('Unable to build driver');
return promise.fulfilled(this._driver);
destroyDriver: function () {
return this._driver ?
this._driver.quit() :
promise.rejected(new Error('No driver was found'));
getDriver: function () {
return this._driver;
startUp: function () {
var flow = promise.controlFlow();
return promise.all([
tearDown: function () {
var flow = promise.controlFlow();
return promise.all([
module.exports = exports = DriverProvider;
var DriverProvider = require('./driver-provider');
var test = require('selenium-webdriver/testing');
test.describe('Page', function () {
var driverProvider = new DriverProvider();
test.before(function () {
return driverProvider.startUp();
test.after(function () {
return driverProvider.tearDown();
});'should be valid', function () {
var driver = driverProvider.getDriver();
// ...actual tests here

mistakster commented Mar 18, 2015

DriverProvider class helps to start up and tear down selenium server easily. Its methods startUp() and tearDown() can contain any number of tasks which have to be completed in particular order. Each task function must return a promise.

The testing library selenium-webdriver/testing mirrors mocha’s methods to run asynchronous code in synchronous way and operate with promises rather than callbacks.

mekdev commented Jul 11, 2015

@mistakster Thanks you for this example this is really cool.
I am currently trying something similar as well but separating out driver and server into 2 different builders.

I was wondering why we bind both startServer() and buildDriver() and if its helps to separate them out.


This way we will start n number of servers everytime we want to build a driver.
If we want to run 2 tests in parallel say IE and FF can't we instantiate 2 drivers and run them on the same server ?


mistakster commented Dec 23, 2015

@mekdev, sorry for my late reply.

I was wondering why we bind both startServer() and buildDriver()

These methods are binded to the DriverProvider instance. This instance can handle only one server and one driver instance. If you want to run 2 (or more) browser then you need more complicated logic in the buildDriver() and getDriver() methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment