Nigthmare error logging improved

This commit is contained in:
Juan Ferrer 2019-10-28 19:52:54 +01:00
parent 1b2c383502
commit ae178e5192
2 changed files with 47 additions and 41 deletions

View File

@ -7,6 +7,12 @@ import config from './config.js';
let currentUser; let currentUser;
let actions = { let actions = {
test: function() {
return this.evaluate(() => {
document.perico();
});
},
// Generic extensions // Generic extensions
clickIfExists: async function(selector) { clickIfExists: async function(selector) {
@ -53,7 +59,7 @@ let actions = {
if (logoutClicked) { if (logoutClicked) {
let buttonSelector = '.vn-dialog.shown button[response=ACCEPT]'; let buttonSelector = '.vn-dialog.shown button[response=ACCEPT]';
this.wait(buttonSelector => { await this.wait(buttonSelector => {
return document.querySelector(buttonSelector) != null return document.querySelector(buttonSelector) != null
|| location.hash == '#!/login'; || location.hash == '#!/login';
}, buttonSelector); }, buttonSelector);
@ -63,7 +69,7 @@ let actions = {
try { try {
await this.waitForURL('#!/login'); await this.waitForURL('#!/login');
} catch (e) { } catch (e) {
this.goto(`${config.url}/#!/login`); await this.goto(`${config.url}/#!/login`);
} }
await this.doLogin(userName, null) await this.doLogin(userName, null)
@ -133,10 +139,10 @@ let actions = {
}, selector); }, selector);
}, },
getProperty: function(selector, property, done) { getProperty: function(selector, property) {
this.evaluate_now((selector, property) => { return this.evaluate((selector, property) => {
return document.querySelector(selector)[property].replace(/\s+/g, ' ').trim(); return document.querySelector(selector)[property].replace(/\s+/g, ' ').trim();
}, done, selector, property); }, selector, property);
}, },
waitPropertyLength: function(selector, property, minLength) { waitPropertyLength: function(selector, property, minLength) {
@ -174,17 +180,17 @@ let actions = {
.click(selector); .click(selector);
}, },
focusElement: function(selector, done) { focusElement: function(selector) {
this.wait(selector) return this.wait(selector)
.evaluate_now(selector => { .evaluate(selector => {
let element = document.querySelector(selector); let element = document.querySelector(selector);
element.focus(); element.focus();
}, done, selector); }, selector);
}, },
isVisible: function(selector, done) { isVisible: function(selector) {
this.wait(selector) return this.wait(selector)
.evaluate_now(elementSelector => { .evaluate(elementSelector => {
const selectorMatches = document.querySelectorAll(elementSelector); const selectorMatches = document.querySelectorAll(elementSelector);
const element = selectorMatches[0]; const element = selectorMatches[0];
@ -222,7 +228,7 @@ let actions = {
element.removeEventListener('mouseover', eventHandler); element.removeEventListener('mouseover', eventHandler);
} }
return isVisible; return isVisible;
}, done, selector); }, selector);
}, },
waitImgLoad: function(selector) { waitImgLoad: function(selector) {
@ -244,10 +250,10 @@ let actions = {
}); });
}, },
countElement: function(selector, done) { countElement: function(selector) {
this.evaluate_now(selector => { return this.evaluate(selector => {
return document.querySelectorAll(selector).length; return document.querySelectorAll(selector).length;
}, done, selector); }, selector);
}, },
waitForNumberOfElements: function(selector, count) { waitForNumberOfElements: function(selector, count) {
@ -286,15 +292,15 @@ let actions = {
}, selector, text); }, selector, text);
}, },
waitForInnerText: function(selector, done) { waitForInnerText: function(selector) {
this.wait(selector) return this.wait(selector)
.wait(selector => { .wait(selector => {
const innerText = document.querySelector(selector).innerText; const innerText = document.querySelector(selector).innerText;
return innerText != null && innerText != ''; return innerText != null && innerText != '';
}, selector) }, selector)
.evaluate_now(selector => { .evaluate(selector => {
return document.querySelector(selector).innerText; return document.querySelector(selector).innerText;
}, done, selector); }, selector);
}, },
waitForEmptyInnerText: function(selector) { waitForEmptyInnerText: function(selector) {
@ -309,9 +315,9 @@ let actions = {
}, hashURL); }, hashURL);
}, },
waitForShapes: function(selector, done) { waitForShapes: function(selector) {
this.wait(selector) return this.wait(selector)
.evaluate_now(selector => { .evaluate(selector => {
const shapes = document.querySelectorAll(selector); const shapes = document.querySelectorAll(selector);
const shapesList = []; const shapesList = [];
@ -320,20 +326,20 @@ let actions = {
return shapesList; return shapesList;
}, done, selector); }, selector);
}, },
waitForSnackbar: function() { waitForSnackbar: function() {
return this.wait(500) return this.wait(500)
.waitForShapes('vn-snackbar .shape .text'); .waitForShapes('vn-snackbar .shape .text');
}, },
waitForLastShape: function(selector, done) { waitForLastShape: function(selector) {
this.wait(selector) return this.wait(selector)
.evaluate_now(selector => { .evaluate(selector => {
const shape = document.querySelector(selector); const shape = document.querySelector(selector);
return shape.innerText; return shape.innerText;
}, done, selector); }, selector);
}, },
waitForLastSnackbar: function() { waitForLastSnackbar: function() {
@ -427,20 +433,21 @@ let actions = {
for (let name in actions) { for (let name in actions) {
Nightmare.action(name, function(...args) { Nightmare.action(name, function(...args) {
let fnArgs = args.slice(0, args.length - 1);
let done = args[args.length - 1]; let done = args[args.length - 1];
let promise = actions[name].apply(this, args);
if (promise) { actions[name].apply(this, fnArgs)
promise
.then(res => done(null, res)) .then(res => done(null, res))
.catch(err => { .catch(err => {
let errArgs = args.slice(0, args.length - 1); let stringArgs = fnArgs
errArgs = errArgs .map(i => typeof i == 'function' ? 'Function' : i)
.map(i => JSON.stringify(i))
.join(', '); .join(', ');
done(new Error(`.${name}(${errArgs}) failed: ${err.message}`)); let orgMessage = err.message.startsWith('.wait()')
? '.wait() timed out'
: err.message;
done(new Error(`.${name}(${stringArgs}) failed: ${orgMessage}`));
}); });
}
}); });
} }

View File

@ -6,7 +6,6 @@ describe('Login path', () => {
it('should receive an error when the username is incorrect', async() => { it('should receive an error when the username is incorrect', async() => {
const result = await nightmare const result = await nightmare
.doLogin('badUser', null) .doLogin('badUser', null)
.waitUntilNotPresent('body', 'jose')
.waitForLastSnackbar(); .waitForLastSnackbar();
expect(result.length).toBeGreaterThan(0); expect(result.length).toBeGreaterThan(0);