Skip to content

Commit 7dd4646

Browse files
committed
Update the js builder to select a server jar from the current environment.
Also update the examples to support this new behavior.
1 parent 36ae4e0 commit 7dd4646

File tree

6 files changed

+129
-37
lines changed

6 files changed

+129
-37
lines changed

javascript/node/selenium-webdriver/builder.js

+74-14
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,67 @@ var Browser = base.require('webdriver.Browser'),
2424

2525

2626

27+
var seleniumServer;
28+
29+
/**
30+
* Starts an instance of the Selenium server if not yet running.
31+
* @param {string} jar Path to the server jar to use.
32+
* @return {!webdriver.promise.Promise.<string>} A promise for the server's
33+
* addrss once started.
34+
*/
35+
function startSeleniumServer(jar) {
36+
if (!seleniumServer) {
37+
// Requiring 'chrome' above would create a cycle:
38+
// index -> builder -> chrome -> index
39+
var remote = require('./remote');
40+
seleniumServer = new remote.SeleniumServer(jar);
41+
}
42+
return seleniumServer.start();
43+
}
44+
45+
2746
/**
2847
* Creates new {@link webdriver.WebDriver WebDriver} instances. The environment
2948
* variables listed below may be used to override a builder's configuration,
3049
* allowing quick runtime changes.
3150
* <ul>
51+
* <li>{@code SELENIUM_BROWSER}: defines the target browser in the form
52+
* {@code browser[:version][:platform]}.
53+
*
3254
* <li>{@code SELENIUM_REMOTE_URL}: defines the remote URL for all builder
3355
* instances. This environment variable should be set to a fully qualified
34-
* URL for a WebDriver server (e.g. http://localhost:4444/wd/hub).
56+
* URL for a WebDriver server (e.g. http://localhost:4444/wd/hub). This
57+
* option always takes precedence over {@code SELENIUM_SERVER_JAR}.
3558
*
36-
* <li>{@code SELENIUM_BROWSER}: defines the target browser in the form
37-
* {@code browser[:version][:platform]}.
59+
* <li>{@code SELENIUM_SERVER_JAR}: defines the path to the
60+
* <a href="http://selenium-release.storage.googleapis.com/index.html">
61+
* standalone Selenium server</a> jar to use. The server will be started the
62+
* first time a WebDriver instance and be killed when the process exits.
3863
* </ul>
3964
*
4065
* <p>Suppose you had mytest.js that created WebDriver with
41-
* {@code var driver = new webdriver.Builder().build();}.
66+
* <pre><code>
67+
* var driver = new webdriver.Builder()
68+
* .forBrowser('chrome')
69+
* .build();
70+
* </code></pre>
4271
*
4372
* This test could be made to use Firefox on the local machine by running with
44-
* {@code SELENIUM_BROWSER=firefox node mytest.js}.
73+
* {@code SELENIUM_BROWSER=firefox node mytest.js}. Rather than change the
74+
* code to target Google Chrome on a remote machine, you can simply set the
75+
* SELENIUM_BROWSER and SELENIUM_REMOTE_URL environment variables:
76+
* <pre><code>
77+
* SELENIUM_BROWSER=chrome:36:LINUX \
78+
* SELENIUM_REMOTE_URL=http://www.example.com:4444/wd/hub \
79+
* node mytest.js
80+
* </code></pre>
4581
*
46-
* <p>Alternatively, you could request Chrome 36 on Linux from a remote
47-
* server with {@code
48-
* SELENIUM_BROWSER=chrome:36:LINUX
49-
* SELENIUM_REMOTE_URL=http://www.example.com:4444/wd/hub
50-
* node mytest.js}.
82+
* <p>You could also use a local copy of the standalone Selenium server:
83+
* <pre><code>
84+
* SELENIUM_BROWSER=chrome:36:LINUX \
85+
* SELENIUM_SERVER_JAR=/path/to/selenium-server-standalone.jar \
86+
* node mytest.js
87+
* </code></pre>
5188
*
5289
* @constructor
5390
*/
@@ -67,6 +104,21 @@ var Builder = function() {
67104

68105
/** @private {firefox.Options} */
69106
this.firefoxOptions_ = null;
107+
108+
/** @private {boolean} */
109+
this.ignoreEnv_ = false;
110+
};
111+
112+
113+
/**
114+
* Configures this builder to ignore any environment variable overrides and to
115+
* only use the configuration specified through this instance's API.
116+
*
117+
* @return {!Builder} A self reference.
118+
*/
119+
Builder.prototype.disableEnvironmentOverrides = function() {
120+
this.ignoreEnv_ = true;
121+
return this;
70122
};
71123

72124

@@ -262,9 +314,9 @@ Builder.prototype.build = function() {
262314
// environment.
263315
var capabilities = new Capabilities(this.capabilities_);
264316

265-
var browser = process.env.SELENIUM_BROWSER;
266-
if (browser) {
267-
browser = browser.split(/:/, 3);
317+
var browser;
318+
if (!this.ignoreEnv_ && process.env.SELENIUM_BROWSER) {
319+
browser = process.env.SELENIUM_BROWSER.split(/:/, 3);
268320
capabilities.set(Capability.BROWSER_NAME, browser[0]);
269321
capabilities.set(Capability.VERSION, browser[1] || null);
270322
capabilities.set(Capability.PLATFORM, browser[2] || null);
@@ -288,7 +340,15 @@ Builder.prototype.build = function() {
288340
}
289341

290342
// Check for a remote browser.
291-
var url = process.env.SELENIUM_REMOTE_URL || this.url_;
343+
var url = this.url_;
344+
if (!this.ignoreEnv_) {
345+
if (process.env.SELENIUM_REMOTE_URL) {
346+
url = process.env.SELENIUM_REMOTE_URL;
347+
} else if (process.env.SELENIUM_SERVER_JAR) {
348+
url = startSeleniumServer(process.env.SELENIUM_SERVER_JAR);
349+
}
350+
}
351+
292352
if (url) {
293353
var executor = executors.createExecutor(url);
294354
return WebDriver.createSession(executor, capabilities, this.flow_);

javascript/node/selenium-webdriver/example/google_search.js

+21-5
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,30 @@
1616
/**
1717
* @fileoverview An example WebDriver script. This requires the chromedriver
1818
* to be present on the system PATH.
19-
* Usage: node selenium-webdriver/example/google_search.js
19+
*
20+
* Usage:
21+
* // Default behavior
22+
* node selenium-webdriver/example/google_search.js
23+
*
24+
* // Target Chrome locally; the chromedriver must be on your PATH
25+
* SELENIUM_BROWSER=chrome node selenium-webdriver/example/google_search.js
26+
*
27+
* // Use a local copy of the standalone Selenium server
28+
* SELENIUM_SERVER_JAR=/path/to/selenium-server-standalone.jar \
29+
* node selenium-webdriver/example/google_search.js
30+
*
31+
* // Target a remove Selenium server
32+
* SELENIUM_REMOTE_URL=http://www.example.com:4444/wd/hub \
33+
* node selenium-webdriver/example/google_search.js
2034
*/
2135

22-
var By = require('..').By,
23-
until = require('..').until,
24-
firefox = require('../firefox');
36+
var webdriver = require('..'),
37+
By = webdriver.By,
38+
until = webdriver.until;
2539

26-
var driver = new firefox.Driver();
40+
var driver = new webdriver.Builder()
41+
.forBrowser('firefox')
42+
.build();
2743

2844
driver.get('http://www.google.com/ncr');
2945
driver.findElement(By.name('q')).sendKeys('webdriver');

javascript/node/selenium-webdriver/example/google_search_generator.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
* selenium-webdriver/example/google_search_generator.js
2222
*/
2323

24-
var By = require('..').By,
25-
firefox = require('../firefox');
24+
var webdriver = require('..'),
25+
By = webdriver.By;
2626

27-
var driver = new firefox.Driver();
27+
var driver = new webdriver.Builder()
28+
.forBrowser('firefox')
29+
.build();
2830

2931
driver.get('http://www.google.com/ncr');
3032
driver.call(function* () {

javascript/node/selenium-webdriver/example/google_search_test.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@
1818
* Usage: mocha -t 10000 selenium-webdriver/example/google_search_test.js
1919
*/
2020

21-
var By = require('..').By,
22-
until = require('..').until,
23-
firefox = require('../firefox'),
21+
var webdriver = require('..'),
22+
By = webdriver.By,
23+
until = webdriver.until,
2424
test = require('../testing');
2525

26-
2726
test.describe('Google Search', function() {
2827
var driver;
2928

3029
test.before(function() {
31-
driver = new firefox.Driver();
30+
driver = new webdriver.Builder()
31+
.forBrowser('firefox')
32+
.build();
3233
});
3334

3435
test.it('should append query to title', function() {
@@ -38,5 +39,7 @@ test.describe('Google Search', function() {
3839
driver.wait(until.titleIs('webdriver - Google Search'), 1000);
3940
});
4041

41-
test.after(function() { driver.quit(); });
42+
test.after(function() {
43+
driver.quit();
44+
});
4245
});

javascript/node/selenium-webdriver/example/parallel_flows.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020

2121
var webdriver = require('..'),
22+
By = webdriver.By,
2223
until = webdriver.until;
2324

2425
for (var i = 0; i < 3; i++) {
@@ -29,7 +30,7 @@ for (var i = 0; i < 3; i++) {
2930
});
3031

3132
var driver = new webdriver.Builder().
32-
withCapabilities(webdriver.Capabilities.firefox()).
33+
forBrowser('firefox').
3334
setControlFlow(flow). // Comment out this line to see the difference.
3435
build();
3536

@@ -38,8 +39,8 @@ for (var i = 0; i < 3; i++) {
3839
driver.manage().window().setPosition(300 * i, 400 * i);
3940

4041
driver.get('http://www.google.com');
41-
driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');
42-
driver.findElement(webdriver.By.name('btnG')).click();
42+
driver.findElement(By.name('q')).sendKeys('webdriver');
43+
driver.findElement(By.name('btnG')).click();
4344
driver.wait(until.titleIs('webdriver - Google Search'), 1000);
4445

4546
driver.quit();

javascript/node/selenium-webdriver/remote/index.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -231,18 +231,28 @@ DriverService.prototype.stop = function() {
231231

232232

233233
/**
234-
* Manages the life and death of the Selenium standalone server. The server
235-
* may be obtained from http://selenium-release.storage.googleapis.com/index.html.
234+
* Manages the life and death of the
235+
* <a href="http://selenium-release.storage.googleapis.com/index.html">
236+
* standalone Selenium server</a>.
237+
*
236238
* @param {string} jar Path to the Selenium server jar.
237-
* @param {!SeleniumServer.Options} options Configuration options for the
239+
* @param {SeleniumServer.Options=} opt_options Configuration options for the
238240
* server.
239-
* @throws {Error} If an invalid port is specified.
241+
* @throws {Error} If the path to the Selenium jar is not specified or if an
242+
* invalid port is specified.
240243
* @constructor
241244
* @extends {DriverService}
242245
*/
243-
function SeleniumServer(jar, options) {
244-
if (options.port < 0)
246+
function SeleniumServer(jar, opt_options) {
247+
if (!jar) {
248+
throw Error('Path to the Selenium jar not specified');
249+
}
250+
251+
var options = opt_options || {};
252+
253+
if (options.port < 0) {
245254
throw Error('Port must be >= 0: ' + options.port);
255+
}
246256

247257
var port = options.port || portprober.findFreePort();
248258
var args = promise.when(options.jvmArgs || [], function(jvmArgs) {

0 commit comments

Comments
 (0)