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

Commit

Permalink
chore(exitCodes): adding exit code for direct connect errors
Browse files Browse the repository at this point in the history
  • Loading branch information
cnishina committed Apr 19, 2016
1 parent afdd9d7 commit 9def91e
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/driverProviders/direct.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand All @@ -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() {});
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions lib/exitCodes.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}
69 changes: 69 additions & 0 deletions spec/unit/driverProviders/direct_test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit 9def91e

Please sign in to comment.