Skip to content
This repository was archived by the owner on Sep 21, 2022. It is now read-only.

Commit

Permalink
feat: Add extended WebDriver error data in output
Browse files Browse the repository at this point in the history
  • Loading branch information
up73k committed Dec 22, 2016
1 parent 18d197a commit 73a26ed
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/browser/new-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const debug = require('debug');
const chalk = require('chalk');
const _ = require('lodash');
const Promise = require('bluebird');
const striptags = require('striptags');

const Browser = require('./browser');
const ClientBridge = require('./client-bridge');
Expand Down Expand Up @@ -116,6 +117,10 @@ module.exports = class NewBrowser extends Browser {

const error = new GeminiError(`Cannot launch browser ${this.id}:\n${e.message}.`);

if (e.data) {
error.message += '\nReason: ' + this._parseHtmlBody(e.data);
}

error.browserId = this.id;
error.sessionId = this.sessionId;

Expand All @@ -124,6 +129,20 @@ module.exports = class NewBrowser extends Browser {
});
}

_parseHtmlBody(html) {
const body = html.match(/<body[\s\S]*<\/body>/i);

if (!body) {
return html;
}

const rawText = striptags(body[0]);

return rawText
? (rawText).replace(/\s+/g, ' ').trim()
: html;
}

_setSessionTimeout(timeout) {
if (_.isNull(timeout)) {
timeout = this.config.httpTimeout;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"resolve": "^1.1.0",
"sizzle": "^2.2.0",
"source-map": "^0.5.3",
"striptags": "^2.1.1",
"temp": "~0.8.0",
"uglify-js": "^2.7.3",
"uglifyify": "^3.0.1",
Expand Down
75 changes: 75 additions & 0 deletions test/unit/browser/new-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Camera = require('lib/browser/camera');
const ClientBridge = require('lib/browser/client-bridge');
const Calibrator = require('lib/calibrator');
const WdErrors = require('lib/constants/wd-errors');
const GeminiError = require('lib/errors/gemini-error');

const makeBrowser = require('../../util').makeBrowser;

Expand Down Expand Up @@ -203,6 +204,80 @@ describe('browser/new-browser', () => {
return assert.isRejected(launchBrowser());
});
});

describe('catch error on wd init', () => {
it('should fail if wd init fails', () => {
wd.init.returns(Promise.reject(new Error('o.O')));

return assert.isRejected(launchBrowser());
});

it('should fail with GeminiError instance', () => {
wd.init.returns(Promise.reject({message: 'defaultError'}));

return assert.isRejected(launchBrowser(), GeminiError);
});

it('should fail with the error message by default', () => {
wd.init.returns(Promise.reject({message: 'error text'}));

return launchBrowser()
.catch((e) => assert.include(e.message, 'error text'));
});

it('should extend error message with error data if it exists', () => {
wd.init.returns(Promise.reject({data: 'error text'}));

return launchBrowser()
.catch((e) => assert.include(e.message, 'error text'));
});

it('should not add to the message fail reason if error data does not exists', () => {
wd.init.returns(Promise.reject({message: 'defaultError'}));

return launchBrowser()
.catch((e) => assert.notInclude(e.message, 'Reason'));
});

it('should cut all tags from error', () => {
wd.init.returns(Promise.reject({data: '<title></title><body><h1>Error</h1> text</body>'}));

return launchBrowser()
.catch((e) => {
assert.notInclude(e.message, '<title>');
assert.notInclude(e.message, '<body>');
assert.notInclude(e.message, '<h1>');
});
});

it('should skip text from all tags except body', () => {
wd.init.returns(Promise.reject({data: '<title>4xx</title><body>Error</body>'}));

return launchBrowser()
.catch((e) => assert.notInclude(e.message, '4xx'));
});

it('should not skip text from internal tags in body tag', () => {
wd.init.returns(Promise.reject({data: '<body><h1>Error</h1> text</body>'}));

return launchBrowser()
.catch((e) => assert.include(e.message, 'Error text'));
});

it('should replace newlines to spaces', () => {
wd.init.returns(Promise.reject({data: '<body>Error\ntext</body>'}));

return launchBrowser()
.catch((e) => assert.include(e.message, 'Error text'));
});

it('should fail with full html if <body> tag is empty', () => {
wd.init.returns(Promise.reject({data: '<html><body></body></html>'}));

return launchBrowser()
.catch((e) => assert.include(e.message, '<html><body></body></html>'));
});
});
});

describe('URL opening', () => {
Expand Down

0 comments on commit 73a26ed

Please sign in to comment.