diff --git a/lib/driverProviders/direct.js b/lib/driverProviders/direct.js index 2c7ee341d..8ef8c0ee7 100644 --- a/lib/driverProviders/direct.js +++ b/lib/driverProviders/direct.js @@ -12,8 +12,10 @@ var webdriver = require('selenium-webdriver'), path = require('path'), util = require('util'), DriverProvider = require('./driverProvider'), - log = require('../logger'); + Logger = require('../logger2').Logger, + BrowserError = require('../exitCodes').BrowserError; +var logger = new Logger('direct'); var DirectDriverProvider = function(config) { DriverProvider.call(this, config); }; @@ -28,13 +30,13 @@ util.inherits(DirectDriverProvider, DriverProvider); DirectDriverProvider.prototype.setupEnv = function() { switch (this.config_.capabilities.browserName) { case 'chrome': - log.puts('Using ChromeDriver directly...'); + logger.info('Using ChromeDriver directly...'); break; case 'firefox': - log.puts('Using FirefoxDriver directly...'); + logger.info('Using FirefoxDriver directly...'); break; default: - throw new Error('browserName (' + this.config_.capabilities.browserName + + throw new BrowserError(logger, 'browserName (' + this.config_.capabilities.browserName + ') is not supported with directConnect.'); } return q.fcall(function() {}); @@ -62,7 +64,7 @@ DirectDriverProvider.prototype.getNewDriver = function() { var chromeDriverFile = this.config_.chromeDriver || defaultChromeDriverPath; if (!fs.existsSync(chromeDriverFile)) { - throw new Error('Could not find chromedriver at ' + chromeDriverFile); + throw new BrowserError(logger, 'Could not find chromedriver at ' + chromeDriverFile); } var service = new chrome.ServiceBuilder(chromeDriverFile).build(); @@ -76,8 +78,8 @@ DirectDriverProvider.prototype.getNewDriver = function() { driver = new firefox.Driver(this.config_.capabilities); break; default: - throw new Error('browserName ' + this.config_.capabilities.browserName + - 'is not supported with directConnect.'); + throw new BrowserError(logger, 'browserName ' + this.config_.capabilities.browserName + + ' is not supported with directConnect.'); } this.drivers_.push(driver); return driver; diff --git a/lib/exitCodes.ts b/lib/exitCodes.ts index 01701c9c0..8b9b14ebe 100644 --- a/lib/exitCodes.ts +++ b/lib/exitCodes.ts @@ -1,12 +1,15 @@ import {Logger} from './logger2'; + const CONFIG_ERROR_CODE = 105; +const BROWSER_ERROR_CODE = 135; export class ProtractorError { error: Error; description: string; code: number; stack: string; + constructor(logger: Logger, description: string, code: number) { this.error = new Error(); this.description = description; @@ -26,3 +29,13 @@ export class ConfigError extends ProtractorError { super(logger, description, ConfigError.CODE); } } + +/** + * Browser errors including getting a driver session, direct connect, etc. + */ +export class BrowserError extends ProtractorError { + static CODE = BROWSER_ERROR_CODE; + constructor(logger: Logger, msg: string) { + super(logger, msg, BrowserError.CODE); + } +} diff --git a/spec/unit/driverProviders/direct_test.js b/spec/unit/driverProviders/direct_test.js new file mode 100644 index 000000000..ffe001b33 --- /dev/null +++ b/spec/unit/driverProviders/direct_test.js @@ -0,0 +1,69 @@ + +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; + +describe('direct connect', function() { + beforeEach(function() { + Logger.setWrite(WriteTo.NONE); + }); + + afterEach(function() { + Logger.setWrite(WriteTo.CONSOLE); + }); + + describe('without the selenium driver', function() { + it('should throw an error if no drivers are present', function() { + var config = { + directConnect: true, + capabilities: { browserName: 'chrome' }, + chromeDriver: '/foo/bar/chromedriver' + }; + var errorFound = false; + try { + webdriver = require('../../../built/driverProviders/direct')(config); + webdriver.getNewDriver(); + } catch(e) { + errorFound = true; + expect(e.code).toBe(BrowserError.CODE); + } + expect(errorFound).toBe(true); + }); + }); + + describe('with selenium drivers', function() { + var chromedriver = ''; + beforeEach(function() { + // add files to selenium folder + file = 'chromedriver_' + require('../../../config.json').webdriverVersions.chromedriver; + chromedriver = path.resolve(os.tmpdir(), file); + fs.openSync(chromedriver, 'w'); + }); + + afterEach(function() { + try { + fs.unlinkSync(chromedriver); + } catch(e) { + } + }); + it('should throw an error if the driver cannot be used', function() { + var config = { + directConnect: true, + capabilities: { browserName: 'foobar explorer' }, + chromeDriver: chromedriver + }; + var errorFound = false; + try { + webdriver = require('../../../built/driverProviders/direct')(config); + webdriver.getNewDriver(); + } catch(e) { + errorFound = true; + expect(e.code).toBe(BrowserError.CODE); + } + expect(errorFound).toBe(true); + }); + }); +});