Skip to content

Commit

Permalink
Merge pull request #88 from saadtazi/make-encoded-first-error-cb
Browse files Browse the repository at this point in the history
`fp.encoded(db)` method now accepts a error-first callback function
  • Loading branch information
saadtazi authored Apr 11, 2017
2 parents 34d705e + 8924d94 commit b5821e9
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.0.0

* BREAKING CHANGE: handle errors in `encoded(cb)` callback function

# 0.5.0

* Make sure to load user settings if existing profile (thanks @lutostag)
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ Make sure you have selenium server running... or use 'selenium-webdriver/remote'

// you can install multiple extensions at the same time
fp.addExtensions(['./test/extensions/firebug-1.12.4-fx.xpi'], function() {
fp.encoded(function(zippedProfile) {
fp.encoded(function(err, zippedProfile) {
if (err) {
console.error('oops, an error occured:', err);
return;
}
browser = wd.promiseChainRemote();
browser.init({
browserName:'firefox',
Expand Down
6 changes: 5 additions & 1 deletion examples/example-wd.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ fp.setPreference('saadt.coucou', 'console');
fp.updatePreferences();
// you can install multiple extensions at the same time
fp.addExtensions(['../test/extensions/firebug-2.0.1-fx.xpi'], function() {
fp.encoded(function(zippedProfile) {
fp.encoded(function(err, zippedProfile) {
if (err) {
console.log('oops, error!', err);
return;
}
browser = wd.promiseChainRemote();
browser.init({
browserName:'firefox',
Expand Down
6 changes: 5 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ createProfile(function(err, fp) {
}
log('encoding profile...');

fp.encoded(function(zippedProfileString) {
fp.encoded(function(err, zippedProfileString) {
if (err) {
console.error('oops, an error occured:', err);
process.exit(4);
}
if (encoded === true) {
process.stdout.write(zippedProfileString);
process.exit(0);
Expand Down
44 changes: 26 additions & 18 deletions lib/firefox_profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,28 +413,36 @@ FirefoxProfile.prototype.setNativeEventsEnabled = function (val) {
* return zipped, base64 encoded string of the profile directory
* for use with remote WebDriver JSON wire protocol
*
* @param {Function} function a callback function with first params as a zipped, base64 encoded string of the profile directory
* @param {Function} function a callback function with first param: an error, and 2nd param: a zipped, base64 encoded string of the profile directory
*/
FirefoxProfile.prototype.encoded = function(cb) {
var self = this,
tmpFolder = this._createTempFolder(),
zipStream = fs.createWriteStream(path.join(tmpFolder,'profile.zip')),
archive = archiver('zip', { forceUTC: true });
try {
var self = this,
tmpFolder = this._createTempFolder(),
zipStream = fs.createWriteStream(path.join(tmpFolder,'profile.zip')),
archive = archiver('zip', { forceUTC: true });

if (this._preferencesModified) {
this.updatePreferences();
}
zipStream.on('close', function() {
fs.readFile(path.join(tmpFolder,'profile.zip'), function(err, content) {
cb(content.toString('base64'));
deleteParallel([path.join(tmpFolder,'profile.zip'), tmpFolder]);
if (this._preferencesModified) {
this.updatePreferences();
}
zipStream.on('error', function (err) {
cb(err);
});
});
archive.pipe(zipStream);
archive.glob('**', {
cwd: self.profileDir, expand: true
}, {});
archive.finalize();

zipStream.on('close', function() {
fs.readFile(path.join(tmpFolder,'profile.zip'), function(err, content) {
cb(null, content.toString('base64'));
deleteParallel([path.join(tmpFolder,'profile.zip'), tmpFolder]);
});
});
archive.pipe(zipStream);
archive.glob('**', {
cwd: self.profileDir, expand: true
}, {});
archive.finalize();
} catch (e) {
cb(e);
}
};

// only '1' found in proxy.js
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "firefox-profile",
"version": "0.5.0",
"version": "1.0.0",
"description": "firefox profile for selenium WebDriverJs, admc/wd or any other node selenium driver that supports capabilities",
"main": "lib/firefox_profile",
"types": "lib/firefox_profile.d.ts",
Expand Down Expand Up @@ -87,7 +87,7 @@
"dependencies": {
"adm-zip": "~0.4.x",
"archiver": "~1.3.0",
"async": "~2.1.2",
"async": "~2.3.0",
"fs-extra": "~2.1.2",
"ini": "~1.3.3",
"jetpack-id": "1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion test/firefox_profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ describe('firefox_profile', function() {
spy.restore();
});
it('should work with a brand new profile', function(done) {
fp.encoded(function(zippedProfileString) {
fp.encoded(function(error, zippedProfileString) {
expect(zippedProfileString).to.be.equal(testProfiles.brandNewProfile.expectedZip);
done();
});
Expand Down
6 changes: 2 additions & 4 deletions test/spec/extension.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,8 @@ describe('install extension', function() {
})
.get('http://saadtazi.com')
.sleep(1000)
// see http://getfirebug.com/wiki/index.php/Command_Line_API
// dirxml, $$ ... and console.table are defined by firebug
// but only console.table is available from js (not in console)
// because table method is probably added to the regular console
// note: console.table used to be exclusive to firebug,
// but it is now only implemented by most modern browsers
/*jshint evil:true */
.eval('console.table').then(function(res) {
expect(res).to.contain('function');
Expand Down

0 comments on commit b5821e9

Please sign in to comment.