From 53333df3a7812fbe1bff55623205d06baa212164 Mon Sep 17 00:00:00 2001 From: Carlos Jimenez <=> Date: Wed, 21 Nov 2018 16:59:09 +0100 Subject: [PATCH] #781 Extensiones nightmare 1st steps --- e2e/helpers/extensions.js | 93 +++++++++++++++++++++++++-------------- e2e/helpers/nightmare.js | 15 +------ 2 files changed, 62 insertions(+), 46 deletions(-) diff --git a/e2e/helpers/extensions.js b/e2e/helpers/extensions.js index 152730000..9aa1c039b 100644 --- a/e2e/helpers/extensions.js +++ b/e2e/helpers/extensions.js @@ -12,7 +12,8 @@ Nightmare.action('login', function(userName, done) { .click(`vn-login input[type=submit]`) // FIXME: Wait for dom to be ready: https://github.com/segmentio/nightmare/issues/481 .wait(1000) - .then(done); + .then(done) + .catch(done); }); Nightmare.action('changeLanguageToEnglish', function(done) { @@ -21,12 +22,14 @@ Nightmare.action('changeLanguageToEnglish', function(done) { return document.querySelector(selector).title; }, '#lang') .then(title => { - if (title === 'Change language') - this.then(done); - else { + if (title === 'Change language') { + this.then(done) + .catch(done); + } else { this.click('#lang') .click('vn-main-menu [vn-id="langs-menu"] ul > li[name="en"]') - .then(done); + .then(done) + .catch(done); } }); }); @@ -36,14 +39,15 @@ Nightmare.action('waitForLogin', function(userName, done) { .waitForURL('#!/') .url() .changeLanguageToEnglish() - .then(done); + .then(done) + .catch(done); }); Nightmare.action('parsedUrl', function(done) { this.url() .then(url => { done(null, new URL(url)); - }); + }).catch(done); }); Nightmare.action('getProperty', function(selector, property, done) { @@ -84,26 +88,36 @@ Nightmare.action('getInputValue', function(selector, done) { }, done, selector); }); -Nightmare.action('clearInput', function(selector, done) { +Nightmare.asyncAction = function(name, func) { + Nightmare.action(name, function(...args) { + let done = args[arguments.length - 1]; + func.apply(this, args) + .then(result => done(null, result)) + .catch(done); + }); +}; + +Nightmare.asyncAction('clearInput', async function(selector) { const backSpaces = []; for (let i = 0; i < 50; i += 1) backSpaces.push('\u0008'); - this.wait(selector) - .type(selector, backSpaces.join('')) - .then(done); + await this.wait(selector) + .type(selector, backSpaces.join('')); }); Nightmare.action('write', function(selector, text, done) { this.wait(selector) .type(selector, text) - .then(done); + .then(done) + .catch(done); }); Nightmare.action('waitToClick', function(selector, done) { this.wait(selector) .click(selector) - .then(done); + .then(done) + .catch(done); }); Nightmare.action('isVisible', function(selector, done) { @@ -157,7 +171,8 @@ Nightmare.action('selectText', function(selector, done) { sel.addRange(range); }, selector) .mouseup(selector) - .then(done); + .then(done) + .catch(done); }); Nightmare.action('countElement', function(selector, done) { @@ -167,44 +182,54 @@ Nightmare.action('countElement', function(selector, done) { }); Nightmare.action('waitForNumberOfElements', function(selector, count, done) { - this.wait((resultSelector, expectedCount) => { - return document.querySelectorAll(resultSelector).length === expectedCount; + this.wait((selector, count) => { + return document.querySelectorAll(selector).length === count; }, selector, count) - .then(done); + .then(done) + .catch(err => { + console.error(err.name, err.message); + done(new Error(`.waitForNumberOfElements() for ${selector}, count ${count} timed out`)); + }); }); Nightmare.action('waitForClassNotPresent', function(selector, className, done) { this.wait(selector) - .wait((resultSelector, targetClass) => { - if (!document.querySelector(resultSelector).classList.contains(targetClass)) + .wait((selector, className) => { + if (!document.querySelector(selector).classList.contains(className)) return true; }, selector, className) - .then(done); + .then(done) + .catch(() => { + done(new Error(`.waitForClassNotPresent() for ${selector}, class ${className} timed out`)); + }); }); Nightmare.action('waitForClassPresent', function(selector, className, done) { this.wait(selector) - .wait((resultSelector, targetClass) => { - if (document.querySelector(resultSelector).classList.contains(targetClass)) + .wait((selector, className) => { + if (document.querySelector(selector).classList.contains(className)) return true; }, selector, className) - .then(done); + .then(done) + .catch(done); }); Nightmare.action('waitForTextInElement', function(selector, name, done) { this.wait(selector) - .wait((resultSelector, expectedText) => { - return document.querySelector(resultSelector).innerText.toLowerCase().includes(expectedText.toLowerCase()); + .wait((selector, name) => { + return document.querySelector(selector).innerText.toLowerCase().includes(name.toLowerCase()); }, selector, name) - .then(done); + .then(done) + .catch(done); }); Nightmare.action('waitForTextInInput', function(selector, name, done) { this.wait(selector) - .wait((resultSelector, expectedText) => { - return document.querySelector(resultSelector).value.toLowerCase().includes(expectedText.toLowerCase()); + .wait((selector, name) => { + return document.querySelector(selector).value.toLowerCase().includes(name.toLowerCase()); }, selector, name) - .then(done); + .then(done) + .catch(done); }); Nightmare.action('waitForInnerText', function(selector, done) { @@ -222,14 +247,16 @@ Nightmare.action('waitForEmptyInnerText', function(selector, done) { this.wait(selector => { return document.querySelector(selector).innerText == ''; }, selector) - .then(done); + .then(done) + .catch(done); }); Nightmare.action('waitForURL', function(hashURL, done) { this.wait(hash => { return document.location.hash.includes(hash); }, hashURL) - .then(done); + .then(done) + .catch(done); }); Nightmare.action('waitForShapes', function(selector, done) { @@ -250,7 +277,7 @@ Nightmare.action('waitForSnackbar', function(done) { this.wait(500).waitForShapes('vn-snackbar .shape .text') .then(shapes => { done(null, shapes); - }); + }).catch(done); }); Nightmare.action('waitForLastShape', function(selector, done) { @@ -266,5 +293,5 @@ Nightmare.action('waitForLastSnackbar', function(done) { this.wait(500).waitForLastShape('vn-snackbar .shape .text') .then(shapes => { done(null, shapes); - }); + }).catch(done); }); diff --git a/e2e/helpers/nightmare.js b/e2e/helpers/nightmare.js index 0550eae86..62a408d21 100644 --- a/e2e/helpers/nightmare.js +++ b/e2e/helpers/nightmare.js @@ -5,27 +5,16 @@ export default function createNightmare(width = 1280, height = 720) { const nightmare = new Nightmare({show: process.env.E2E_SHOW, typeInterval: 10, x: 0, y: 0, waitTimeout: 2000}) .viewport(width, height); - // nightmare.on('page', (type, message, error) => { - // console.log(type); - // console.log(message.name); - // console.log(error); - // fail(error); - // }); - nightmare.on('console', (type, message) => { - if (type === 'error') { + if (type === 'error') fail(message); - } - if (type === 'log') { - // console.log(message); - } }); nightmare.header('Accept-Language', 'en'); afterAll(() => { return nightmare - .end(); + .end(); }); return nightmare;