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

Commit 73a26ed

Browse files
committed
feat: Add extended WebDriver error data in output
1 parent 18d197a commit 73a26ed

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

lib/browser/new-browser.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const debug = require('debug');
77
const chalk = require('chalk');
88
const _ = require('lodash');
99
const Promise = require('bluebird');
10+
const striptags = require('striptags');
1011

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

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

120+
if (e.data) {
121+
error.message += '\nReason: ' + this._parseHtmlBody(e.data);
122+
}
123+
119124
error.browserId = this.id;
120125
error.sessionId = this.sessionId;
121126

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

132+
_parseHtmlBody(html) {
133+
const body = html.match(/<body[\s\S]*<\/body>/i);
134+
135+
if (!body) {
136+
return html;
137+
}
138+
139+
const rawText = striptags(body[0]);
140+
141+
return rawText
142+
? (rawText).replace(/\s+/g, ' ').trim()
143+
: html;
144+
}
145+
127146
_setSessionTimeout(timeout) {
128147
if (_.isNull(timeout)) {
129148
timeout = this.config.httpTimeout;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"resolve": "^1.1.0",
4141
"sizzle": "^2.2.0",
4242
"source-map": "^0.5.3",
43+
"striptags": "^2.1.1",
4344
"temp": "~0.8.0",
4445
"uglify-js": "^2.7.3",
4546
"uglifyify": "^3.0.1",

test/unit/browser/new-browser.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const Camera = require('lib/browser/camera');
88
const ClientBridge = require('lib/browser/client-bridge');
99
const Calibrator = require('lib/calibrator');
1010
const WdErrors = require('lib/constants/wd-errors');
11+
const GeminiError = require('lib/errors/gemini-error');
1112

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

@@ -203,6 +204,80 @@ describe('browser/new-browser', () => {
203204
return assert.isRejected(launchBrowser());
204205
});
205206
});
207+
208+
describe('catch error on wd init', () => {
209+
it('should fail if wd init fails', () => {
210+
wd.init.returns(Promise.reject(new Error('o.O')));
211+
212+
return assert.isRejected(launchBrowser());
213+
});
214+
215+
it('should fail with GeminiError instance', () => {
216+
wd.init.returns(Promise.reject({message: 'defaultError'}));
217+
218+
return assert.isRejected(launchBrowser(), GeminiError);
219+
});
220+
221+
it('should fail with the error message by default', () => {
222+
wd.init.returns(Promise.reject({message: 'error text'}));
223+
224+
return launchBrowser()
225+
.catch((e) => assert.include(e.message, 'error text'));
226+
});
227+
228+
it('should extend error message with error data if it exists', () => {
229+
wd.init.returns(Promise.reject({data: 'error text'}));
230+
231+
return launchBrowser()
232+
.catch((e) => assert.include(e.message, 'error text'));
233+
});
234+
235+
it('should not add to the message fail reason if error data does not exists', () => {
236+
wd.init.returns(Promise.reject({message: 'defaultError'}));
237+
238+
return launchBrowser()
239+
.catch((e) => assert.notInclude(e.message, 'Reason'));
240+
});
241+
242+
it('should cut all tags from error', () => {
243+
wd.init.returns(Promise.reject({data: '<title></title><body><h1>Error</h1> text</body>'}));
244+
245+
return launchBrowser()
246+
.catch((e) => {
247+
assert.notInclude(e.message, '<title>');
248+
assert.notInclude(e.message, '<body>');
249+
assert.notInclude(e.message, '<h1>');
250+
});
251+
});
252+
253+
it('should skip text from all tags except body', () => {
254+
wd.init.returns(Promise.reject({data: '<title>4xx</title><body>Error</body>'}));
255+
256+
return launchBrowser()
257+
.catch((e) => assert.notInclude(e.message, '4xx'));
258+
});
259+
260+
it('should not skip text from internal tags in body tag', () => {
261+
wd.init.returns(Promise.reject({data: '<body><h1>Error</h1> text</body>'}));
262+
263+
return launchBrowser()
264+
.catch((e) => assert.include(e.message, 'Error text'));
265+
});
266+
267+
it('should replace newlines to spaces', () => {
268+
wd.init.returns(Promise.reject({data: '<body>Error\ntext</body>'}));
269+
270+
return launchBrowser()
271+
.catch((e) => assert.include(e.message, 'Error text'));
272+
});
273+
274+
it('should fail with full html if <body> tag is empty', () => {
275+
wd.init.returns(Promise.reject({data: '<html><body></body></html>'}));
276+
277+
return launchBrowser()
278+
.catch((e) => assert.include(e.message, '<html><body></body></html>'));
279+
});
280+
});
206281
});
207282

208283
describe('URL opening', () => {

0 commit comments

Comments
 (0)