Skip to content

Commit

Permalink
Add browser specific constructors for consistency with other language
Browse files Browse the repository at this point in the history
bindings.
  • Loading branch information
jleyba committed Jul 29, 2014
1 parent 2d75905 commit c977fb4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 18 deletions.
9 changes: 6 additions & 3 deletions javascript/node/selenium-webdriver/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
`webdriver.promise.consume`. Requires harmony support (run with
`node --harmony-generators` in `v0.11.x`).
* Promise A+ compliance: a promise may no longer resolve to itself.
* For consistency with other language bindings, deprecated
`UnhandledAlertError#getAlert` and added `#getAlertText`.
`getAlert` will be removed in `2.45.0`.
* Changes for consistency with other language bindings:
* Added the `chrome.ChromeDriver` class, which may be used to create a new
ChromeDriver session (vs. using `webdriver.Builder`).
* Added the `phantomjs.PhantomDriverJs` class.
* Deprecated `UnhandledAlertError#getAlert` and added `#getAlertText`.
`getAlert` will be removed in `2.45.0`.
* FIXED: 7563: Mocha integration no longer disables timeouts. Default Mocha
timeouts apply (2000 ms) and may be changed using `this.timeout(ms)`.

Expand Down
4 changes: 2 additions & 2 deletions javascript/node/selenium-webdriver/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ function createNativeDriver(capabilities) {
// Requiring 'chrome' above would create a cycle:
// index -> builder -> chrome -> index
var chrome = require('./chrome');
return chrome.createDriver(capabilities);
return new chrome.ChromeDriver(capabilities);

case Browser.PHANTOM_JS:
// Requiring 'phantomjs' would create a cycle:
// index -> builder -> phantomjs -> index
var phantomjs = require('./phantomjs');
return phantomjs.createDriver(capabilities);
return new phantomjs.PhantomJsDriver(capabilities);

default:
return null;
Expand Down
27 changes: 23 additions & 4 deletions javascript/node/selenium-webdriver/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,10 @@ Options.prototype.toJSON = function() {
* @param {(webdriver.Capabilities|Options)=} opt_options The session options.
* @param {remote.DriverService=} opt_service The session to use; will use
* the {@link getDefaultService default service} by default.
* @return {!webdriver.WebDriver} A new WebDriver instance.
* @constructor
* @extends {webdriver.WebDriver}
*/
function createDriver(opt_options, opt_service) {
var ChromeDriver = function(opt_options, opt_service) {
var service = opt_service || getDefaultService();
var executor = executors.createExecutor(service.start());

Expand All @@ -457,17 +458,35 @@ function createDriver(opt_options, opt_service) {
options = Options.fromCapabilities(options);
}

return webdriver.WebDriver.createSession(
var driver = webdriver.WebDriver.createSession(
executor, options.toCapabilities());
webdriver.WebDriver.call(
this, driver.getSession(), executor, driver.controlFlow());
};
util.inherits(ChromeDriver, webdriver.WebDriver);


/**
* Creates a new ChromeDriver session.
* @param {(webdriver.Capabilities|Options)=} opt_options The session options.
* @param {remote.DriverService=} opt_service The session to use; will use
* the {@link getDefaultService default service} by default.
* @return {!webdriver.WebDriver} A new WebDriver instance.
* @deprecated Use {@link ChromeDriver} directly. This function will be
* removed in 2.45.0.
*/
function createDriver(opt_options, opt_service) {
return new ChromeDriver(opt_options, opt_service);
}



// PUBLIC API


exports.ServiceBuilder = ServiceBuilder;
exports.ChromeDriver = ChromeDriver;
exports.Options = Options;
exports.ServiceBuilder = ServiceBuilder;
exports.createDriver = createDriver;
exports.getDefaultService = getDefaultService;
exports.setDefaultService = setDefaultService;
42 changes: 33 additions & 9 deletions javascript/node/selenium-webdriver/phantomjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ var WEBDRIVER_TO_PHANTOMJS_LEVEL = (function() {
/**
* Creates a new PhantomJS WebDriver client.
* @param {webdriver.Capabilities=} opt_capabilities The desired capabilities.
* @return {!webdriver.WebDriver} A new WebDriver instance.
* @constructor
* @extends {webdriver.WebDriver}
*/
function createDriver(opt_capabilities) {
var PhantomJsDriver = function(opt_capabilities) {
var capabilities = opt_capabilities || webdriver.Capabilities.phantomjs();
var exe = findExecutable(capabilities.get(BINARY_PATH_CAPABILITY));
var args = ['--webdriver-logfile=' + DEFAULT_LOG_FILE];
Expand Down Expand Up @@ -140,25 +141,48 @@ function createDriver(opt_capabilities) {
args = args.concat(capabilities.get(CLI_ARGS_CAPABILITY) || []);

var port = portprober.findFreePort();
var service = new remote.DriverService(exe, {

/** @private {!remote.DriverService} */
this.service_ = new remote.DriverService(exe, {
port: port,
args: webdriver.promise.when(port, function(port) {
args.push('--webdriver=' + port);
return args;
})
});

var executor = executors.createExecutor(service.start());
var executor = executors.createExecutor(this.service_.start());
var driver = webdriver.WebDriver.createSession(executor, capabilities);
var boundQuit = driver.quit.bind(driver);
driver.quit = function() {
return boundQuit().thenFinally(service.kill.bind(service));
};
return driver;

webdriver.WebDriver.call(
this, driver.getSession(), executor, driver.controlFlow());
}
util.inherits(PhantomJsDriver, webdriver.WebDriver);


/** @override */
PhantomJsDriver.prototype.quit = function() {
var self = this;
return webdriver.WebDriver.prototype.quit.call(this).thenFinally(function() {
return self.service_.kill();
});
};


/**
* Creates a new PhantomJS WebDriver client.
* @param {webdriver.Capabilities=} opt_capabilities The desired capabilities.
* @return {!webdriver.WebDriver} A new WebDriver instance.
* @deprecated Use {@link PhantomJsDriver}. This function will be removed in
* the 2.45.0 release.
*/
function createDriver(opt_capabilities) {
return new PhantomJsDriver(opt_capabilities);
}


// PUBLIC API


exports.PhantomJsDriver = PhantomJsDriver;
exports.createDriver = createDriver;

0 comments on commit c977fb4

Please sign in to comment.