Skip to content

Commit

Permalink
Merge 848e68e into 5d0f299
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Dec 1, 2016
2 parents 5d0f299 + 848e68e commit ebac8ae
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ jspm_packages

# Mac OS
.DS_Store

# Temporary files created by tests
test/temp
38 changes: 33 additions & 5 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,47 @@
/**
* Module dependencies.
*/
var path = require('path');
var utilities = require('./utilities');
var webdriver = require('selenium-webdriver');

/**
* Builds and launches a driver.
*
* @param {String} browserName - The browser name.
* @return {WebDriver}
* @param {String} browserName - The browser name.
* @param {Object} [browserOptions] - The build options.
* @return {WebDriver} - The driver instance.
*/
module.exports = function build(browserName) {
if (typeof browserName !== 'string') {
throw new TypeError('First argument `browserName` is missing');
module.exports = function build(browserName, browserOptions) {
if (typeof browserName === 'string') {
browserName = browserName.toLowerCase();
} else {
throw new TypeError('First argument `browserName` must be a string');
}

var builder = new webdriver.Builder();
builder.forBrowser(browserName);

// browser options
if (typeof browserOptions === 'object') {
var client = require(path.join('selenium-webdriver', browserName));
var options = new client.Options();
var profile = browserOptions.profile;

// profile
if (profile && typeof profile === 'string') {
switch (browserName) {
case 'firefox':
options.setProfile(profile);
break;
case 'chrome':
options.addArguments('user-data-dir=' + profile);
break;
}
}

builder['set' + utilities.capitalize(browserName) + 'Options'](options);
}

return builder.build();
};
21 changes: 21 additions & 0 deletions lib/utilities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

/**
* Capitalize string.
*
* @param {String} string - The string.
* @return {String}
*/
function capitalize(string) {
if (typeof string !== 'string') {
throw new TypeError('First argument must be a string');
}
return string.charAt(0).toUpperCase() + string.slice(1);
}

/**
* Export utilities.
*/
module.exports = {
capitalize: capitalize
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"main": "index.js",
"scripts": {
"test": "mocha",
"lint": "eslint .",
"cover": "istanbul cover _mocha -- -R spec \"test/**/*\"",
"lint": "eslint --ignore-path .gitignore .",
"cover": "istanbul cover _mocha -- \"test/*.js\"",
"coveralls": "cat coverage/lcov.info | coveralls"
},
"repository": {
Expand Down
19 changes: 17 additions & 2 deletions test/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* Module dependencies.
*/
var assert = require('assert');
var test = require('selenium-webdriver/testing');
var build = require('../lib/build');
var test = require('selenium-webdriver/testing');

/**
* Build.
Expand All @@ -21,9 +21,24 @@ test.describe('#build', function() {
}, Error);
});

test.it('builds and launches a driver', function() {
test.it('launches a driver', function(done) {
var driver = build('phantomjs');
assert.equal(driver.constructor.name, 'thenableWebDriverProxy')
driver.quit();
done();
});

if (!process.env.TRAVIS) {
test.it('launches chrome with profile', function(done) {
var profilePath = 'test/temp/chrome-profile';
var driver = build('chrome', {
profile: profilePath
});
driver.getCapabilities().then(function(capabilities) {
assert.equal(capabilities.get('chrome').userDataDir, profilePath);
});
driver.quit();
done();
});
}
});
3 changes: 1 addition & 2 deletions test/saveScreenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test.describe('#saveScreenshot', function() {
});

test.it('takes and writes the screenshot', function(done) {
var filename = 'test/temp.png';
var filename = 'test/temp/save-screenshot.png';
saveScreenshot(driver, filename, function(error) {
if (error) throw error;

Expand All @@ -51,7 +51,6 @@ test.describe('#saveScreenshot', function() {
fs.readFileSync(filename, 'base64'),
data.replace('data:image/png;base64')
);
fs.unlinkSync(filename);
done();
});
});
Expand Down
Empty file added test/temp/.gitkeep
Empty file.
32 changes: 32 additions & 0 deletions test/utilities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

/**
* Module dependencies.
*/
var assert = require('assert');
var utilities = require('../lib/utilities');

/**
* Utilities.
*/
describe('utilities', function() {

describe('#capitalize', function() {
var capitalize = utilities.capitalize;

it('throws an error if first argument is not a string', function() {
[undefined, null, 1, {}, [], Function].forEach(function(argument) {
assert.throws(function() {
capitalize(argument);
}, TypeError);
});
});

it('capitalizes a string', function() {
assert.equal(capitalize(''), '');
assert.equal(capitalize('foo'), 'Foo');
assert.equal(capitalize('FOO'), 'FOO');
});
});

});

0 comments on commit ebac8ae

Please sign in to comment.