Create a gist now

Instantly share code, notes, and snippets.

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()
.usingServer(this._server.address())
.forBrowser(webdriver.Browser.FIREFOX)
.build();
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([
flow.execute(this.startServer.bind(this)),
flow.execute(this.buildDriver.bind(this))
]);
},
tearDown: function () {
var flow = promise.controlFlow();
return promise.all([
flow.execute(this.destroyDriver.bind(this)),
flow.execute(this.stopServer.bind(this))
]);
}
};
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();
});
test.it('should be valid', function () {
var driver = driverProvider.getDriver();
// ...actual tests here
});
});
@mistakster
Owner

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
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.

    flow.execute(this.startServer.bind(this)),
    flow.execute(this.buildDriver.bind(this))

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
Owner

@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