@@ -24,30 +24,67 @@ var Browser = base.require('webdriver.Browser'),
24
24
25
25
26
26
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
+
27
46
/**
28
47
* Creates new {@link webdriver.WebDriver WebDriver} instances. The environment
29
48
* variables listed below may be used to override a builder's configuration,
30
49
* allowing quick runtime changes.
31
50
* <ul>
51
+ * <li>{@code SELENIUM_BROWSER}: defines the target browser in the form
52
+ * {@code browser[:version][:platform]}.
53
+ *
32
54
* <li>{@code SELENIUM_REMOTE_URL}: defines the remote URL for all builder
33
55
* 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}.
35
58
*
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.
38
63
* </ul>
39
64
*
40
65
* <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>
42
71
*
43
72
* 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>
45
81
*
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>
51
88
*
52
89
* @constructor
53
90
*/
@@ -67,6 +104,21 @@ var Builder = function() {
67
104
68
105
/** @private {firefox.Options} */
69
106
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 ;
70
122
} ;
71
123
72
124
@@ -262,9 +314,9 @@ Builder.prototype.build = function() {
262
314
// environment.
263
315
var capabilities = new Capabilities ( this . capabilities_ ) ;
264
316
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 ) ;
268
320
capabilities . set ( Capability . BROWSER_NAME , browser [ 0 ] ) ;
269
321
capabilities . set ( Capability . VERSION , browser [ 1 ] || null ) ;
270
322
capabilities . set ( Capability . PLATFORM , browser [ 2 ] || null ) ;
@@ -288,7 +340,15 @@ Builder.prototype.build = function() {
288
340
}
289
341
290
342
// 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
+
292
352
if ( url ) {
293
353
var executor = executors . createExecutor ( url ) ;
294
354
return WebDriver . createSession ( executor , capabilities , this . flow_ ) ;
0 commit comments