Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
chore(local): if no local drivers provided in config, use update-conf…
Browse files Browse the repository at this point in the history
…ig.json (#3922)

- turn on all unit tests and update them to work
- direct driver provider unit test for browser error
- local driver provider unit test for browser error

closes #3782
  • Loading branch information
cnishina authored Jan 6, 2017
1 parent ec93c4a commit 574d5c2
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 23 deletions.
19 changes: 15 additions & 4 deletions lib/driverProviders/direct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,24 @@ export class Direct extends DriverProvider {
*/
getNewDriver(): WebDriver {
let driver: WebDriver;
let updateJson = path.resolve(SeleniumConfig.getSeleniumDir(), 'update-config.json');
let updateConfig = JSON.parse(fs.readFileSync(updateJson).toString());

switch (this.config_.capabilities.browserName) {
case 'chrome':
let defaultChromeDriverPath = updateConfig.chrome.last;
let chromeDriverFile = this.config_.chromeDriver || defaultChromeDriverPath;
let chromeDriverFile: string;
if (this.config_.chromeDriver) {
chromeDriverFile = this.config_.chromeDriver;
} else {
try {
let updateJson = path.resolve(SeleniumConfig.getSeleniumDir(), 'update-config.json');
let updateConfig = JSON.parse(fs.readFileSync(updateJson).toString());
chromeDriverFile = updateConfig.chrome.last;
} catch (e) {
throw new BrowserError(
logger,
'Could not find update-config.json. ' +
'Run \'webdriver-manager update\' to download binaries.');
}
}

if (!fs.existsSync(chromeDriverFile)) {
throw new BrowserError(
Expand Down
28 changes: 22 additions & 6 deletions lib/driverProviders/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,16 @@ export class Local extends DriverProvider {
logger.debug(
'Attempting to find the SeleniumServerJar in the default ' +
'location used by webdriver-manager');
this.config_.seleniumServerJar = path.resolve(
SeleniumConfig.getSeleniumDir(), new SeleniumStandAlone().executableFilename());
try {
let updateJson = path.resolve(SeleniumConfig.getSeleniumDir(), 'update-config.json');
let updateConfig = JSON.parse(fs.readFileSync(updateJson).toString());
this.config_.seleniumServerJar = updateConfig.standalone.last;
} catch (err) {
throw new BrowserError(
logger,
'No update-config.json found.' +
' Run \'webdriver-manager update\' to download binaries.');
}
}
if (!fs.existsSync(this.config_.seleniumServerJar)) {
throw new BrowserError(
Expand All @@ -53,8 +61,17 @@ export class Local extends DriverProvider {
logger.debug(
'Attempting to find the chromedriver binary in the default ' +
'location used by webdriver-manager');
this.config_.chromeDriver = path.resolve(
SeleniumConfig.getSeleniumDir(), new SeleniumChrome().executableFilename());

try {
let updateJson = path.resolve(SeleniumConfig.getSeleniumDir(), 'update-config.json');
let updateConfig = JSON.parse(fs.readFileSync(updateJson).toString());
this.config_.chromeDriver = updateConfig.chrome.last;
} catch (err) {
throw new BrowserError(
logger,
'No update-config.json found. ' +
'Run \'webdriver-manager update\' to download binaries.');
}
}

// Check if file exists, if not try .exe or fail accordingly
Expand All @@ -77,11 +94,10 @@ export class Local extends DriverProvider {
* @return {q.promise} A promise which will resolve when the environment is
* ready to test.
*/
protected setupDriverEnv(): q.Promise<any> {
setupDriverEnv(): q.Promise<any> {
let deferred = q.defer();

this.addDefaultBinaryLocs_();

logger.info('Starting selenium standalone server...');

let serverConf = this.config_.localSeleniumStandaloneOpts || {};
Expand Down
2 changes: 1 addition & 1 deletion scripts/unit_test.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"spec_dir": "",
"spec_files": [
"spec/unit/*.js",
"spec/unit/**/*.js",
"website/docgen/spec/*.js"
]
}
34 changes: 29 additions & 5 deletions spec/unit/driverProviders/direct_test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
var fs = require('fs'),
os = require('os'),
path = require('path');
var BrowserError = require('../../../built/exitCodes').BrowserError,
Logger = require('../../../built/logger2').Logger,
WriteTo = require('../../../built/logger2').WriteTo;
var BrowserError = require('../../../built/exitCodes').BrowserError;
var ProtractorError = require('../../../built/exitCodes').ProtractorError;
var Logger = require('../../../built/logger').Logger;
var WriteTo = require('../../../built/logger').WriteTo;
var Direct = require('../../../built/driverProviders').Direct;
var webdriver, file;

describe('direct connect', function() {
beforeEach(function() {
ProtractorError.SUPRESS_EXIT_CODE = true;
Logger.setWrite(WriteTo.NONE);
});

afterEach(function() {
ProtractorError.SUPRESS_EXIT_CODE = false;
Logger.setWrite(WriteTo.CONSOLE);
});

Expand All @@ -24,7 +28,7 @@ describe('direct connect', function() {
};
var errorFound = false;
try {
webdriver = require('../../../built/driverProviders/direct')(config);
webdriver = new Direct(config);
webdriver.getNewDriver();
} catch(e) {
errorFound = true;
Expand All @@ -49,6 +53,7 @@ describe('direct connect', function() {
} catch(e) {
}
});

it('should throw an error if the driver cannot be used', function() {
var config = {
directConnect: true,
Expand All @@ -57,7 +62,26 @@ describe('direct connect', function() {
};
var errorFound = false;
try {
webdriver = require('../../../built/driverProviders/direct')(config);
webdriver = new Direct(config);
webdriver.getNewDriver();
} catch(e) {
errorFound = true;
expect(e.code).toBe(BrowserError.CODE);
}
expect(errorFound).toBe(true);
});
});

describe('binary does not exist', () => {
it('should throw an error if the update-config.json does not exist', () => {
spyOn(fs, 'readFileSync').and.callFake(() => { return null; });
var config = {
directConnect: true,
capabilities: { browserName: 'chrome' },
};
var errorFound = false;
try {
webdriver = new Direct(config);
webdriver.getNewDriver();
} catch(e) {
errorFound = true;
Expand Down
35 changes: 28 additions & 7 deletions spec/unit/driverProviders/local_test.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
var fs = require('fs'),
os = require('os'),
path = require('path');
var BrowserError = require('../../../built/exitCodes').BrowserError,
Logger = require('../../../built/logger2').Logger,
WriteTo = require('../../../built/logger2').WriteTo;
var BrowserError = require('../../../built/exitCodes').BrowserError;
var ProtractorError = require('../../../built/exitCodes').ProtractorError;
var Logger = require('../../../built/logger').Logger;
var WriteTo = require('../../../built/logger').WriteTo;
var Local = require('../../../built/driverProviders').Local;
var webdriver, file;

describe('local connect', function() {
beforeEach(function() {
ProtractorError.SUPRESS_EXIT_CODE = true;
Logger.setWrite(WriteTo.NONE);
});

afterEach(function() {
ProtractorError.SUPRESS_EXIT_CODE = false;
Logger.setWrite(WriteTo.CONSOLE);
});

describe('without the selenium standalone jar', function() {
it('should throw an error jar file is not present', function() {
var config = {
directConnect: true,
capabilities: { browserName: 'chrome' },
seleniumServerJar: '/foo/bar/selenium.jar'
};
var errorFound = false;
try {
webdriver = require('../../../built/driverProviders/local')(config);
webdriver = new Local(config);
webdriver.setupEnv();
} catch(e) {
errorFound = true;
Expand Down Expand Up @@ -53,13 +56,12 @@ describe('local connect', function() {

it('should throw an error if the selenium sever jar cannot be used', function() {
var config = {
directConnect: true,
capabilities: { browserName: 'foobar explorer' },
seleniumServerJar: jarFile
};
var errorFound = false;
try {
webdriver = require('../../../built/driverProviders/local')(config);
webdriver = new Local(config);
webdriver.getNewDriver();
} catch(e) {
errorFound = true;
Expand All @@ -69,4 +71,23 @@ describe('local connect', function() {
});
});
});

describe('binary does not exist', () => {
it('should throw an error if the update-config.json does not exist', () => {
spyOn(fs, 'readFileSync').and.callFake(() => { return null; });
var config = {
capabilities: { browserName: 'chrome' },
openSync: fs.openSync
};
var errorFound = false;
try {
webdriver = new Local(config);
webdriver.setupDriverEnv();
} catch(e) {
errorFound = true;
expect(e.code).toBe(BrowserError.CODE);
}
expect(errorFound).toBe(true);
});
});
});

0 comments on commit 574d5c2

Please sign in to comment.