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