Skip to content

Commit

Permalink
[js] Replace uses of managed-promises with native promises where the …
Browse files Browse the repository at this point in the history
…promise

manager isn't required.
  • Loading branch information
jleyba committed Jun 9, 2016
1 parent 4227381 commit 0342309
Show file tree
Hide file tree
Showing 23 changed files with 452 additions and 371 deletions.
4 changes: 3 additions & 1 deletion javascript/node/selenium-webdriver/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
`builder.Builder#usingHttpAgent()`
* Added new wait conditions: `until.urlIs()`, `until.urlContains()`,
`until.urlMatches()`
* Added work around for [GeckoDriver bug](https://bugzilla.mozilla.org/show_bug.cgi?id=1274924) raising a type conversion error
* Added work around for [GeckoDriver bug](https://bugzilla.mozilla.org/show_bug.cgi?id=1274924)
raising a type conversion error
* Internal cleanup replacing uses of managed promises with native promises
* Removed the mandatory use of Firefox Dev Edition, when using Marionette driver
* Fixed timeouts' URL

Expand Down
10 changes: 5 additions & 5 deletions javascript/node/selenium-webdriver/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ const Command = {

/**
* Creates a command executor with support for ChromeDriver's custom commands.
* @param {!promise.Promise<string>} url The server's URL.
* @param {!Promise<string>} url The server's URL.
* @return {!command.Executor} The new command executor.
*/
function createExecutor(url) {
return new executors.DeferredExecutor(url.then(function(url) {
return new executors.DeferredExecutor(url.then(url => {
let client = new http.HttpClient(url);
let executor = new http.Executor(client);
executor.defineCommand(
Expand Down Expand Up @@ -321,7 +321,7 @@ class ServiceBuilder {
loopback: true,
path: this.path_,
port: port,
args: promise.when(port, function(port) {
args: Promise.resolve(port).then(function(port) {
return args.concat('--port=' + port);
}),
env: this.env_,
Expand Down Expand Up @@ -746,8 +746,8 @@ class Options {
if (Buffer.isBuffer(extension)) {
return extension.toString('base64');
}
return promise.checkedNodeCall(
fs.readFile, extension, 'base64');
return io.read(/** @type {string} */(extension))
.then(buffer => buffer.toString('base64'));
});
}
return json;
Expand Down
2 changes: 1 addition & 1 deletion javascript/node/selenium-webdriver/edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class ServiceBuilder {
// (or can we?), force the DriverService to use "localhost".
hostname: 'localhost',
port: port,
args: promise.fulfilled(port).then(function(port) {
args: Promise.resolve(port).then(function(port) {
return args.concat('--port=' + port);
}),
env: this.env_,
Expand Down
13 changes: 6 additions & 7 deletions javascript/node/selenium-webdriver/executors.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@

const HttpClient = require('./http').HttpClient,
HttpExecutor = require('./http').Executor,
DeferredExecutor = require('./lib/command').DeferredExecutor,
promise = require('./lib/promise');
DeferredExecutor = require('./lib/command').DeferredExecutor;


// PUBLIC API
Expand All @@ -36,16 +35,16 @@ exports.DeferredExecutor = DeferredExecutor;

/**
* Creates a command executor that uses WebDriver's JSON wire protocol.
* @param {(string|!promise.Promise<string>)} url The server's URL,
* or a promise that will resolve to that URL.
* @param {http.Agent=} opt_agent (optional) The Http.Agent for the client to
* use.
* @param {(string|!Promise<string>)} url The server's URL, or a promise that
* will resolve to that URL.
* @param {http.Agent=} opt_agent (optional) The Http.Agent for the client to
* use.
* @param {(string|null)=} opt_proxy (optional) The URL of the HTTP proxy for
* the client to use.
* @returns {!./lib/command.Executor} The new command executor.
*/
exports.createExecutor = function(url, opt_agent, opt_proxy) {
return new DeferredExecutor(promise.when(url, function(url) {
return new DeferredExecutor(url.then(url => {
var client = new HttpClient(url, opt_agent, opt_proxy);
return new HttpExecutor(client);
}));
Expand Down
32 changes: 11 additions & 21 deletions javascript/node/selenium-webdriver/firefox/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const child = require('child_process'),
util = require('util');

const isDevMode = require('../lib/devmode'),
promise = require('../lib/promise'),
Symbols = require('../lib/symbols'),
io = require('../io'),
exec = require('../io/exec');
Expand Down Expand Up @@ -126,29 +125,21 @@ function findFirefox(opt_dev) {
/**
* Copies the no focus libs into the given profile directory.
* @param {string} profileDir Path to the profile directory to install into.
* @return {!promise.Promise.<string>} The LD_LIBRARY_PATH prefix string to use
* @return {!Promise<string>} The LD_LIBRARY_PATH prefix string to use
* for the installed libs.
*/
function installNoFocusLibs(profileDir) {
var x86 = path.join(profileDir, 'x86');
var amd64 = path.join(profileDir, 'amd64');

return mkdir(x86)
.then(copyLib.bind(null, NO_FOCUS_LIB_X86, x86))
.then(mkdir.bind(null, amd64))
.then(copyLib.bind(null, NO_FOCUS_LIB_AMD64, amd64))
return io.mkdir(x86)
.then(() => copyLib(NO_FOCUS_LIB_X86, x86))
.then(() => io.mkdir(amd64))
.then(() => copyLib(NO_FOCUS_LIB_AMD64, amd64))
.then(function() {
return x86 + ':' + amd64;
});

function mkdir(dir) {
return io.exists(dir).then(function(exists) {
if (!exists) {
return promise.checkedNodeCall(fs.mkdir, dir);
}
});
}

function copyLib(src, dir) {
return io.copy(src, path.join(dir, X_IGNORE_NO_FOCUS_LIB));
}
Expand Down Expand Up @@ -226,19 +217,19 @@ class Binary {
* executable path when this instance was created. Otherwise, an attempt will
* be made to find Firefox on the current system.
*
* @return {!promise.Promise<string>} a promise for the path to the Firefox
* executable used by this instance.
* @return {!Promise<string>} a promise for the path to the Firefox executable
* used by this instance.
*/
locate() {
return promise.fulfilled(this.exe_ || findFirefox(this.devEdition_));
return Promise.resolve(this.exe_ || findFirefox(this.devEdition_));
}

/**
* Launches Firefox and returns a promise that will be fulfilled when the
* process terminates.
* @param {string} profile Path to the profile directory to use.
* @return {!promise.Promise<!exec.Command>} A promise for the handle to the
* started subprocess.
* @return {!Promise<!exec.Command>} A promise for the handle to the started
* subprocess.
*/
launch(profile) {
let env = {};
Expand All @@ -264,8 +255,7 @@ class Binary {
* the wire; all command line arguments and environment variables will be
* discarded.
*
* @return {!promise.Promise<string>} A promise for this binary's wire
* representation.
* @return {!Promise<string>} A promise for this binary's wire representation.
*/
[Symbols.serialize]() {
return this.locate();
Expand Down
52 changes: 29 additions & 23 deletions javascript/node/selenium-webdriver/firefox/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ const AdmZip = require('adm-zip'),
path = require('path'),
xml = require('xml2js');

const promise = require('../lib/promise'),
checkedCall = promise.checkedNodeCall,
io = require('../io');
const io = require('../io');


/**
Expand All @@ -48,25 +46,24 @@ class AddonFormatError extends Error {
* @param {string} extension Path to the extension to install, as either a xpi
* file or a directory.
* @param {string} dir Path to the directory to install the extension in.
* @return {!promise.Promise.<string>} A promise for the add-on ID once
* @return {!Promise<string>} A promise for the add-on ID once
* installed.
*/
function install(extension, dir) {
return getDetails(extension).then(function(details) {
function returnId() { return details.id; }

var dst = path.join(dir, details.id);
if (extension.slice(-4) === '.xpi') {
if (!details.unpack) {
return io.copy(extension, dst + '.xpi').then(returnId);
return io.copy(extension, dst + '.xpi').then(() => details.id);
} else {
return checkedCall(fs.readFile, extension).then(function(buff) {
return Promise.resolve().then(function() {
// TODO: find an async library for inflating a zip archive.
new AdmZip(buff).extractAllTo(dst, true);
}).then(returnId);
new AdmZip(extension).extractAllTo(dst, true);
return details.id;
});
}
} else {
return io.copyDir(extension, dst).then(returnId);
return io.copyDir(extension, dst).then(() => details.id);
}
});
}
Expand All @@ -86,7 +83,7 @@ var RdfRoot;
/**
* Extracts the details needed to install an add-on.
* @param {string} addonPath Path to the extension directory.
* @return {!promise.Promise.<!AddonDetails>} A promise for the add-on details.
* @return {!Promise<!AddonDetails>} A promise for the add-on details.
*/
function getDetails(addonPath) {
return readManifest(addonPath).then(function(doc) {
Expand Down Expand Up @@ -143,34 +140,43 @@ function getDetails(addonPath) {
/**
* Reads the manifest for a Firefox add-on.
* @param {string} addonPath Path to a Firefox add-on as a xpi or an extension.
* @return {!promise.Promise<!Object>} A promise for the parsed manifest.
* @return {!Promise<!Object>} A promise for the parsed manifest.
*/
function readManifest(addonPath) {
var manifest;

if (addonPath.slice(-4) === '.xpi') {
manifest = checkedCall(fs.readFile, addonPath).then(function(buff) {
var zip = new AdmZip(buff);
manifest = new Promise((resolve, reject) => {
let zip = new AdmZip(addonPath);

if (!zip.getEntry('install.rdf')) {
throw new AddonFormatError(
'Could not find install.rdf in ' + addonPath);
reject(new AddonFormatError(
'Could not find install.rdf in ' + addonPath));
return;
}
var done = promise.defer();
zip.readAsTextAsync('install.rdf', done.fulfill);
return done.promise;

zip.readAsTextAsync('install.rdf', resolve);
});
} else {
manifest = checkedCall(fs.stat, addonPath).then(function(stats) {
manifest = io.stat(addonPath).then(function(stats) {
if (!stats.isDirectory()) {
throw Error(
'Add-on path is niether a xpi nor a directory: ' + addonPath);
}
return checkedCall(fs.readFile, path.join(addonPath, 'install.rdf'));
return io.read(path.join(addonPath, 'install.rdf'));
});
}

return manifest.then(function(content) {
return checkedCall(xml.parseString, content);
return new Promise((resolve, reject) => {
xml.parseString(content, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
});
}

Expand Down
11 changes: 6 additions & 5 deletions javascript/node/selenium-webdriver/firefox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ function createGeckoDriverService(binary) {
return new remote.DriverService(geckoDriver, {
loopback: true,
port: port,
args: promise.all([exe, port]).then(args => {
args: Promise.all([exe, port]).then(args => {
return ['-b', args[0], '--webdriver-port', args[1]];
})
// ,stdio: 'inherit'
Expand Down Expand Up @@ -370,11 +370,12 @@ class Driver extends webdriver.WebDriver {
});

onQuit = function() {
let finishCommand = command.then(command => {
return command.then(command => {
command.kill();
return command.result();
return preparedProfile.then(io.rmDir)
.then(() => command.result(),
() => command.result());
});
return finishCommand.finally(() => preparedProfile.then(io.rmDir));
};
}

Expand All @@ -386,7 +387,7 @@ class Driver extends webdriver.WebDriver {

/** @override */
this.quit = function() {
return boundQuit().thenFinally(onQuit);
return boundQuit().finally(onQuit);
};
}

Expand Down
Loading

0 comments on commit 0342309

Please sign in to comment.