Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
feat(protractor/runner): allow multiple browser in test
Browse files Browse the repository at this point in the history
  • Loading branch information
hankduan committed Nov 26, 2014
1 parent 289dbb9 commit 0bbfd2b
Show file tree
Hide file tree
Showing 21 changed files with 504 additions and 239 deletions.
10 changes: 7 additions & 3 deletions lib/driverProviders/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ Each file exports a function which takes in the configuration as a parameter and
DriverProvider.prototype.setupEnv

/**
* @return {webdriver.WebDriver} The setup driver instance.
* @return {webdriver.WebDriver} A new setup driver instance.
*/
DriverProvider.prototype.getDriver
DriverProvider.prototype.getNewDriver

/**
* @return {q.promise} A promise which will resolve when the environment
Expand All @@ -37,6 +37,10 @@ DriverProvider.prototype.updateJob
Requirements
------------

- `setupEnv` and `getDriver` will be called before the test framework is loaded, so any pre-work which might cause timeouts on the first test should be done there.
- `setupEnv` will be called before the test framework is loaded, so any
pre-work which might cause timeouts on the first test should be done there.
`getNewDriver` will be called once right after `setupEnv` to generate the
initial driver, and possibly during the middle of the test if users request
additional browsers.

- `teardownEnv` should call the driver's `quit` method.
39 changes: 13 additions & 26 deletions lib/driverProviders/direct.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ var webdriver = require('selenium-webdriver'),
firefox = require('selenium-webdriver/firefox'),
q = require('q'),
fs = require('fs'),
path = require('path');
path = require('path'),
util = require('util'),
DriverProvider = require('./driverProvider');

var DirectDriverProvider = function(config) {
this.config_ = config;
this.driver_ = null;
DriverProvider.call(this, config);
};
util.inherits(DirectDriverProvider, DriverProvider);

/**
* Configure and launch (if applicable) the object's environment.
Expand All @@ -38,30 +40,14 @@ DirectDriverProvider.prototype.setupEnv = function() {
};

/**
* Teardown and destroy the environment and do any associated cleanup.
* Shuts down the driver.
* Create a new driver.
*
* @public
* @return {q.promise} A promise which will resolve when the environment
* is down.
*/
DirectDriverProvider.prototype.teardownEnv = function() {
var deferred = q.defer();
this.driver_.quit().then(function() {
deferred.resolve();
});
return deferred.promise;
};

/**
* Retrieve the webdriver for the runner.
* @public
* @override
* @return webdriver instance
*/
DirectDriverProvider.prototype.getDriver = function() {
if (this.driver_) {
return this.driver_;
}
DirectDriverProvider.prototype.getNewDriver = function() {
var driver;
switch (this.config_.capabilities.browserName) {
case 'chrome':
var chromeDriverFile = this.config_.chromeDriver ||
Expand All @@ -78,20 +64,21 @@ DirectDriverProvider.prototype.getDriver = function() {
}

var service = new chrome.ServiceBuilder(chromeDriverFile).build();
this.driver_ = chrome.createDriver(
driver = chrome.createDriver(
new webdriver.Capabilities(this.config_.capabilities), service);
break;
case 'firefox':
if (this.config_.firefoxPath) {
this.config_.capabilities.firefox_binary = this.config_.firefoxPath;
}
this.driver_ = new firefox.Driver(this.config_.capabilities);
driver = new firefox.Driver(this.config_.capabilities);
break;
default:
throw new Error('browserName ' + this.config_.capabilities.browserName +
'is not supported with directConnect.');
}
return this.driver_;
this.drivers_.push(driver);
return driver;
};

// new instance w/ each include
Expand Down
55 changes: 55 additions & 0 deletions lib/driverProviders/driverProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* This is a base driver provider class.
* It is responsible for setting up the account object, tearing
* it down, and setting up the driver correctly.
*/

var webdriver = require('selenium-webdriver'),
q = require('q');

var DriverProvider = function(config) {
this.config_ = config;
this.drivers_ = [];
};

/**
* Teardown and destroy the environment and do any associated cleanup.
* Shuts down the drivers.
*
* @public
* @return {q.promise} A promise which will resolve when the environment
* is down.
*/
DriverProvider.prototype.teardownEnv = function() {
var deferredArray = this.drivers_.map(function(driver) {
var deferred = q.defer();
driver.getSession().then(function(session_) {
if (session_) {
driver.quit().then(function() {
deferred.resolve();
});
} else {
deferred.resolve();
}
});
return deferred.promise;
});
return q.all(deferredArray);
};

/**
* Create a new driver.
*
* @public
* @return webdriver instance
*/
DriverProvider.prototype.getNewDriver = function() {
var newDriver = new webdriver.Builder().
usingServer(this.config_.seleniumAddress).
withCapabilities(this.config_.capabilities).
build();
this.drivers_.push(newDriver);
return newDriver;
};

module.exports = DriverProvider;
39 changes: 4 additions & 35 deletions lib/driverProviders/hosted.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
*/

var util = require('util'),
webdriver = require('selenium-webdriver'),
q = require('q');
q = require('q'),
DriverProvider = require('./driverProvider');

var HostedDriverProvider = function(config) {
this.config_ = config;
this.driver_ = null;
DriverProvider.call(this, config);
};
util.inherits(HostedDriverProvider, DriverProvider);

/**
* Configure and launch (if applicable) the object's environment.
Expand All @@ -34,37 +34,6 @@ HostedDriverProvider.prototype.setupEnv = function() {
}
};

/**
* Teardown and destroy the environment and do any associated cleanup.
* Shuts down the driver.
*
* @public
* @return {q.promise} A promise which will resolve when the environment
* is down.
*/
HostedDriverProvider.prototype.teardownEnv = function() {
var deferred = q.defer();
this.driver_.quit().then(function() {
deferred.resolve();
});
return deferred.promise;
};

/**
* Return the webdriver for the runner.
* @public
* @return webdriver instance
*/
HostedDriverProvider.prototype.getDriver = function() {
if (!this.driver_) {
this.driver_ = new webdriver.Builder().
usingServer(this.config_.seleniumAddress).
withCapabilities(this.config_.capabilities).
build();
}
return this.driver_;
};

// new instance w/ each include
module.exports = function(config) {
return new HostedDriverProvider(config);
Expand Down
36 changes: 10 additions & 26 deletions lib/driverProviders/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
* so that we only start the local selenium once per entire launch.
*/
var util = require('util'),
webdriver = require('selenium-webdriver'),
path = require('path'),
remote = require('selenium-webdriver/remote'),
fs = require('fs'),
q = require('q');
q = require('q'),
DriverProvider = require('./driverProvider');

var LocalDriverProvider = function(config) {
this.config_ = config;
this.driver_ = null;
DriverProvider.call(this, config);
this.server_ = null;
};
util.inherits(LocalDriverProvider, DriverProvider);


/**
Expand Down Expand Up @@ -88,41 +88,25 @@ LocalDriverProvider.prototype.setupEnv = function() {

/**
* Teardown and destroy the environment and do any associated cleanup.
* Shuts down the driver.
* Shuts down the drivers and server.
*
* @public
* @override
* @return {q.promise} A promise which will resolve when the environment
* is down.
*/
LocalDriverProvider.prototype.teardownEnv = function() {
var deferred = q.defer();
var self = this;

util.puts('Shutting down selenium standalone server.');
self.driver_.quit().then(function() {
return self.server_.stop().then(function() {
var deferred = q.defer();
DriverProvider.prototype.teardownEnv.call(this).then(function() {
util.puts('Shutting down selenium standalone server.');
self.server_.stop().then(function() {
deferred.resolve();
});
});

return deferred.promise;
};

/**
* Retrieve the webdriver for the runner.
* @public
* @return webdriver instance
*/
LocalDriverProvider.prototype.getDriver = function() {
if (!this.driver_) {
this.driver_ = new webdriver.Builder().
usingServer(this.config_.seleniumAddress).
withCapabilities(this.config_.capabilities).
build();
}
return this.driver_;
};

// new instance w/ each include
module.exports = function(config) {
return new LocalDriverProvider(config);
Expand Down
35 changes: 14 additions & 21 deletions lib/driverProviders/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
* server.
*/
var webdriver = require('selenium-webdriver'),
q = require('q');
util = require('util'),
q = require('q'),
DriverProvider = require('./driverProvider');
/**
* @constructor
*/
var MockExecutor = function() {
this.driver_ = null;
this.drivers_ = [];
};

/**
Expand All @@ -25,8 +27,10 @@ MockExecutor.prototype.execute = function(command, callback) {
};

var MockDriverProvider = function(config) {
this.config_ = config;
DriverProvider.call(this, config);
};
util.inherits(MockDriverProvider, DriverProvider);


/**
* Configure and launch (if applicable) the object's environment.
Expand All @@ -37,30 +41,19 @@ MockDriverProvider.prototype.setupEnv = function() {
return q.fcall(function() {});
};

/**
* Teardown and destroy the environment and do any associated cleanup.
*
* @public
* @return {q.promise} A promise which will resolve immediately.
*/
MockDriverProvider.prototype.teardownEnv = function() {
var deferred = q.defer();
this.driver_.quit().then(function() {
deferred.resolve();
});
return deferred.promise;
};

/**
* Retrieve the webdriver for the runner.
* Create a new driver.
*
* @public
* @override
* @return webdriver instance
*/
MockDriverProvider.prototype.getDriver = function() {
MockDriverProvider.prototype.getNewDriver = function() {
var mockSession = new webdriver.Session('test_session_id', {});

this.driver_ = new webdriver.WebDriver(mockSession, new MockExecutor());
return this.driver_;
var newDriver = new webdriver.WebDriver(mockSession, new MockExecutor());
this.drivers_.push(newDriver);
return newDriver;
};

// new instance w/ each include
Expand Down
Loading

0 comments on commit 0bbfd2b

Please sign in to comment.