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 20, 2016
1 parent afdd9d7 commit fe344a9
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 13 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
16 changes: 10 additions & 6 deletions lib/driverProviders/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ var util = require('util'),
remote = require('selenium-webdriver/remote'),
fs = require('fs'),
q = require('q'),
DriverProvider = require('./driverProvider');
BrowserError = require('../exitCodes').BrowserError,
DriverProvider = require('./driverProvider'),
Logger = require('../logger2').Logger;

var logger = new Logger('localDriverProvider');
var LocalDriverProvider = function(config) {
DriverProvider.call(this, config);
this.server_ = null;
Expand All @@ -27,23 +30,24 @@ util.inherits(LocalDriverProvider, DriverProvider);
*/
LocalDriverProvider.prototype.addDefaultBinaryLocs_ = function() {
if (!this.config_.seleniumServerJar) {
log.debug('Attempting to find the SeleniumServerJar in the default ' +
logger.debug('Attempting to find the SeleniumServerJar in the default ' +
'location used by webdriver-manager');
this.config_.seleniumServerJar = path.resolve(__dirname,
'../../selenium/selenium-server-standalone-' +
require('../../config.json').webdriverVersions.selenium + '.jar');
}

if (!fs.existsSync(this.config_.seleniumServerJar)) {
throw new Error('No selenium server jar found at the specified ' +
throw new BrowserError(logger, 'No selenium server jar found at the specified ' +
'location (' + this.config_.seleniumServerJar +
'). Check that the version number is up to date.');
}
if (this.config_.capabilities.browserName === 'chrome') {
if (!this.config_.chromeDriver) {
log.debug('Attempting to find the chromedriver binary in the default ' +
logger.debug('Attempting to find the chromedriver binary in the default ' +
'location used by webdriver-manager');
this.config_.chromeDriver =
path.resolve(__dirname, '../../selenium/chromedriver_' +
path.resolve(__dirname, '../../selenium/chromedriver_' +
require('../../config.json').webdriverVersions.chromedriver);
}

Expand All @@ -52,7 +56,7 @@ LocalDriverProvider.prototype.addDefaultBinaryLocs_ = function() {
if (fs.existsSync(this.config_.chromeDriver + '.exe')) {
this.config_.chromeDriver += '.exe';
} else {
throw new Error('Could not find chromedriver at ' +
throw new BrowserError(logger, 'Could not find chromedriver at ' +
this.config_.chromeDriver);
}
}
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);
}
}
23 changes: 23 additions & 0 deletions spec/errorTest/sauceLabNotAvailable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var env = require('../environment.js');

exports.config = {
sauceUser: 'foo',
sauceKey: process.env.SAUCE_ACCESS_KEY,

framework: 'jasmine',

specs: [
'../../example/example_spec.js'
],

capabilities: {
'browserName': 'chrome'
},

baseUrl: env.baseUrl + '/ng1/',

jasmineNodeOpts: {
defaultTimeoutInterval: 90000
}

};
68 changes: 68 additions & 0 deletions spec/unit/driverProviders/direct_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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 chrome 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 chromedriver drivers', function() {
var chromedriver = '';
beforeEach(function() {
// add files to selenium folder
file = '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);
});
});
});
71 changes: 71 additions & 0 deletions spec/unit/driverProviders/local_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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('local connect', function() {
beforeEach(function() {
Logger.setWrite(WriteTo.NONE);
});

afterEach(function() {
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.setupEnv();
} catch(e) {
errorFound = true;
expect(e.code).toBe(BrowserError.CODE);
}
expect(errorFound).toBe(true);
});
});

describe('with the selenium standalone jar', function() {
it('should throw an error if the jar file does not work', function() {
var jarFile = '';
beforeEach(function() {
// add files to selenium folder
file = 'selenium.jar';
jarFile = path.resolve(os.tmpdir(), file);
fs.openSync(jarFile, 'w');
});

afterEach(function() {
try {
fs.unlinkSync(jarFile);
} catch(e) {
}
});

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.getNewDriver();
} catch(e) {
errorFound = true;
expect(e.code).toBe(BrowserError.CODE);
}
expect(errorFound).toBe(true);
});
});
});
});

0 comments on commit fe344a9

Please sign in to comment.