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

Commit 0bbfd2b

Browse files
committed
feat(protractor/runner): allow multiple browser in test
1 parent 289dbb9 commit 0bbfd2b

21 files changed

+504
-239
lines changed

lib/driverProviders/README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ Each file exports a function which takes in the configuration as a parameter and
1414
DriverProvider.prototype.setupEnv
1515

1616
/**
17-
* @return {webdriver.WebDriver} The setup driver instance.
17+
* @return {webdriver.WebDriver} A new setup driver instance.
1818
*/
19-
DriverProvider.prototype.getDriver
19+
DriverProvider.prototype.getNewDriver
2020

2121
/**
2222
* @return {q.promise} A promise which will resolve when the environment
@@ -37,6 +37,10 @@ DriverProvider.prototype.updateJob
3737
Requirements
3838
------------
3939

40-
- `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.
40+
- `setupEnv` will be called before the test framework is loaded, so any
41+
pre-work which might cause timeouts on the first test should be done there.
42+
`getNewDriver` will be called once right after `setupEnv` to generate the
43+
initial driver, and possibly during the middle of the test if users request
44+
additional browsers.
4145

4246
- `teardownEnv` should call the driver's `quit` method.

lib/driverProviders/direct.js

+13-26
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ var webdriver = require('selenium-webdriver'),
99
firefox = require('selenium-webdriver/firefox'),
1010
q = require('q'),
1111
fs = require('fs'),
12-
path = require('path');
12+
path = require('path'),
13+
util = require('util'),
14+
DriverProvider = require('./driverProvider');
1315

1416
var DirectDriverProvider = function(config) {
15-
this.config_ = config;
16-
this.driver_ = null;
17+
DriverProvider.call(this, config);
1718
};
19+
util.inherits(DirectDriverProvider, DriverProvider);
1820

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

4042
/**
41-
* Teardown and destroy the environment and do any associated cleanup.
42-
* Shuts down the driver.
43+
* Create a new driver.
4344
*
4445
* @public
45-
* @return {q.promise} A promise which will resolve when the environment
46-
* is down.
47-
*/
48-
DirectDriverProvider.prototype.teardownEnv = function() {
49-
var deferred = q.defer();
50-
this.driver_.quit().then(function() {
51-
deferred.resolve();
52-
});
53-
return deferred.promise;
54-
};
55-
56-
/**
57-
* Retrieve the webdriver for the runner.
58-
* @public
46+
* @override
5947
* @return webdriver instance
6048
*/
61-
DirectDriverProvider.prototype.getDriver = function() {
62-
if (this.driver_) {
63-
return this.driver_;
64-
}
49+
DirectDriverProvider.prototype.getNewDriver = function() {
50+
var driver;
6551
switch (this.config_.capabilities.browserName) {
6652
case 'chrome':
6753
var chromeDriverFile = this.config_.chromeDriver ||
@@ -78,20 +64,21 @@ DirectDriverProvider.prototype.getDriver = function() {
7864
}
7965

8066
var service = new chrome.ServiceBuilder(chromeDriverFile).build();
81-
this.driver_ = chrome.createDriver(
67+
driver = chrome.createDriver(
8268
new webdriver.Capabilities(this.config_.capabilities), service);
8369
break;
8470
case 'firefox':
8571
if (this.config_.firefoxPath) {
8672
this.config_.capabilities.firefox_binary = this.config_.firefoxPath;
8773
}
88-
this.driver_ = new firefox.Driver(this.config_.capabilities);
74+
driver = new firefox.Driver(this.config_.capabilities);
8975
break;
9076
default:
9177
throw new Error('browserName ' + this.config_.capabilities.browserName +
9278
'is not supported with directConnect.');
9379
}
94-
return this.driver_;
80+
this.drivers_.push(driver);
81+
return driver;
9582
};
9683

9784
// new instance w/ each include

lib/driverProviders/driverProvider.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* This is a base driver provider class.
3+
* It is responsible for setting up the account object, tearing
4+
* it down, and setting up the driver correctly.
5+
*/
6+
7+
var webdriver = require('selenium-webdriver'),
8+
q = require('q');
9+
10+
var DriverProvider = function(config) {
11+
this.config_ = config;
12+
this.drivers_ = [];
13+
};
14+
15+
/**
16+
* Teardown and destroy the environment and do any associated cleanup.
17+
* Shuts down the drivers.
18+
*
19+
* @public
20+
* @return {q.promise} A promise which will resolve when the environment
21+
* is down.
22+
*/
23+
DriverProvider.prototype.teardownEnv = function() {
24+
var deferredArray = this.drivers_.map(function(driver) {
25+
var deferred = q.defer();
26+
driver.getSession().then(function(session_) {
27+
if (session_) {
28+
driver.quit().then(function() {
29+
deferred.resolve();
30+
});
31+
} else {
32+
deferred.resolve();
33+
}
34+
});
35+
return deferred.promise;
36+
});
37+
return q.all(deferredArray);
38+
};
39+
40+
/**
41+
* Create a new driver.
42+
*
43+
* @public
44+
* @return webdriver instance
45+
*/
46+
DriverProvider.prototype.getNewDriver = function() {
47+
var newDriver = new webdriver.Builder().
48+
usingServer(this.config_.seleniumAddress).
49+
withCapabilities(this.config_.capabilities).
50+
build();
51+
this.drivers_.push(newDriver);
52+
return newDriver;
53+
};
54+
55+
module.exports = DriverProvider;

lib/driverProviders/hosted.js

+4-35
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
*/
66

77
var util = require('util'),
8-
webdriver = require('selenium-webdriver'),
9-
q = require('q');
8+
q = require('q'),
9+
DriverProvider = require('./driverProvider');
1010

1111
var HostedDriverProvider = function(config) {
12-
this.config_ = config;
13-
this.driver_ = null;
12+
DriverProvider.call(this, config);
1413
};
14+
util.inherits(HostedDriverProvider, DriverProvider);
1515

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

37-
/**
38-
* Teardown and destroy the environment and do any associated cleanup.
39-
* Shuts down the driver.
40-
*
41-
* @public
42-
* @return {q.promise} A promise which will resolve when the environment
43-
* is down.
44-
*/
45-
HostedDriverProvider.prototype.teardownEnv = function() {
46-
var deferred = q.defer();
47-
this.driver_.quit().then(function() {
48-
deferred.resolve();
49-
});
50-
return deferred.promise;
51-
};
52-
53-
/**
54-
* Return the webdriver for the runner.
55-
* @public
56-
* @return webdriver instance
57-
*/
58-
HostedDriverProvider.prototype.getDriver = function() {
59-
if (!this.driver_) {
60-
this.driver_ = new webdriver.Builder().
61-
usingServer(this.config_.seleniumAddress).
62-
withCapabilities(this.config_.capabilities).
63-
build();
64-
}
65-
return this.driver_;
66-
};
67-
6837
// new instance w/ each include
6938
module.exports = function(config) {
7039
return new HostedDriverProvider(config);

lib/driverProviders/local.js

+10-26
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
* so that we only start the local selenium once per entire launch.
88
*/
99
var util = require('util'),
10-
webdriver = require('selenium-webdriver'),
1110
path = require('path'),
1211
remote = require('selenium-webdriver/remote'),
1312
fs = require('fs'),
14-
q = require('q');
13+
q = require('q'),
14+
DriverProvider = require('./driverProvider');
1515

1616
var LocalDriverProvider = function(config) {
17-
this.config_ = config;
18-
this.driver_ = null;
17+
DriverProvider.call(this, config);
1918
this.server_ = null;
2019
};
20+
util.inherits(LocalDriverProvider, DriverProvider);
2121

2222

2323
/**
@@ -88,41 +88,25 @@ LocalDriverProvider.prototype.setupEnv = function() {
8888

8989
/**
9090
* Teardown and destroy the environment and do any associated cleanup.
91-
* Shuts down the driver.
91+
* Shuts down the drivers and server.
9292
*
9393
* @public
94+
* @override
9495
* @return {q.promise} A promise which will resolve when the environment
9596
* is down.
9697
*/
9798
LocalDriverProvider.prototype.teardownEnv = function() {
98-
var deferred = q.defer();
9999
var self = this;
100-
101-
util.puts('Shutting down selenium standalone server.');
102-
self.driver_.quit().then(function() {
103-
return self.server_.stop().then(function() {
100+
var deferred = q.defer();
101+
DriverProvider.prototype.teardownEnv.call(this).then(function() {
102+
util.puts('Shutting down selenium standalone server.');
103+
self.server_.stop().then(function() {
104104
deferred.resolve();
105105
});
106106
});
107-
108107
return deferred.promise;
109108
};
110109

111-
/**
112-
* Retrieve the webdriver for the runner.
113-
* @public
114-
* @return webdriver instance
115-
*/
116-
LocalDriverProvider.prototype.getDriver = function() {
117-
if (!this.driver_) {
118-
this.driver_ = new webdriver.Builder().
119-
usingServer(this.config_.seleniumAddress).
120-
withCapabilities(this.config_.capabilities).
121-
build();
122-
}
123-
return this.driver_;
124-
};
125-
126110
// new instance w/ each include
127111
module.exports = function(config) {
128112
return new LocalDriverProvider(config);

lib/driverProviders/mock.js

+14-21
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
* server.
55
*/
66
var webdriver = require('selenium-webdriver'),
7-
q = require('q');
7+
util = require('util'),
8+
q = require('q'),
9+
DriverProvider = require('./driverProvider');
810
/**
911
* @constructor
1012
*/
1113
var MockExecutor = function() {
12-
this.driver_ = null;
14+
this.drivers_ = [];
1315
};
1416

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

2729
var MockDriverProvider = function(config) {
28-
this.config_ = config;
30+
DriverProvider.call(this, config);
2931
};
32+
util.inherits(MockDriverProvider, DriverProvider);
33+
3034

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

40-
/**
41-
* Teardown and destroy the environment and do any associated cleanup.
42-
*
43-
* @public
44-
* @return {q.promise} A promise which will resolve immediately.
45-
*/
46-
MockDriverProvider.prototype.teardownEnv = function() {
47-
var deferred = q.defer();
48-
this.driver_.quit().then(function() {
49-
deferred.resolve();
50-
});
51-
return deferred.promise;
52-
};
5344

5445
/**
55-
* Retrieve the webdriver for the runner.
46+
* Create a new driver.
47+
*
5648
* @public
49+
* @override
5750
* @return webdriver instance
5851
*/
59-
MockDriverProvider.prototype.getDriver = function() {
52+
MockDriverProvider.prototype.getNewDriver = function() {
6053
var mockSession = new webdriver.Session('test_session_id', {});
61-
62-
this.driver_ = new webdriver.WebDriver(mockSession, new MockExecutor());
63-
return this.driver_;
54+
var newDriver = new webdriver.WebDriver(mockSession, new MockExecutor());
55+
this.drivers_.push(newDriver);
56+
return newDriver;
6457
};
6558

6659
// new instance w/ each include

0 commit comments

Comments
 (0)