#781 Extensiones nightmare 1st steps

This commit is contained in:
Carlos Jimenez 2018-11-21 16:59:09 +01:00
parent a3be9a1b6d
commit 53333df3a7
2 changed files with 62 additions and 46 deletions

View File

@ -12,7 +12,8 @@ Nightmare.action('login', function(userName, done) {
.click(`vn-login input[type=submit]`) .click(`vn-login input[type=submit]`)
// FIXME: Wait for dom to be ready: https://github.com/segmentio/nightmare/issues/481 // FIXME: Wait for dom to be ready: https://github.com/segmentio/nightmare/issues/481
.wait(1000) .wait(1000)
.then(done); .then(done)
.catch(done);
}); });
Nightmare.action('changeLanguageToEnglish', function(done) { Nightmare.action('changeLanguageToEnglish', function(done) {
@ -21,12 +22,14 @@ Nightmare.action('changeLanguageToEnglish', function(done) {
return document.querySelector(selector).title; return document.querySelector(selector).title;
}, '#lang') }, '#lang')
.then(title => { .then(title => {
if (title === 'Change language') if (title === 'Change language') {
this.then(done); this.then(done)
else { .catch(done);
} else {
this.click('#lang') this.click('#lang')
.click('vn-main-menu [vn-id="langs-menu"] ul > li[name="en"]') .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('#!/') .waitForURL('#!/')
.url() .url()
.changeLanguageToEnglish() .changeLanguageToEnglish()
.then(done); .then(done)
.catch(done);
}); });
Nightmare.action('parsedUrl', function(done) { Nightmare.action('parsedUrl', function(done) {
this.url() this.url()
.then(url => { .then(url => {
done(null, new URL(url)); done(null, new URL(url));
}); }).catch(done);
}); });
Nightmare.action('getProperty', function(selector, property, done) { Nightmare.action('getProperty', function(selector, property, done) {
@ -84,26 +88,36 @@ Nightmare.action('getInputValue', function(selector, done) {
}, done, selector); }, 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 = []; const backSpaces = [];
for (let i = 0; i < 50; i += 1) for (let i = 0; i < 50; i += 1)
backSpaces.push('\u0008'); backSpaces.push('\u0008');
this.wait(selector) await this.wait(selector)
.type(selector, backSpaces.join('')) .type(selector, backSpaces.join(''));
.then(done);
}); });
Nightmare.action('write', function(selector, text, done) { Nightmare.action('write', function(selector, text, done) {
this.wait(selector) this.wait(selector)
.type(selector, text) .type(selector, text)
.then(done); .then(done)
.catch(done);
}); });
Nightmare.action('waitToClick', function(selector, done) { Nightmare.action('waitToClick', function(selector, done) {
this.wait(selector) this.wait(selector)
.click(selector) .click(selector)
.then(done); .then(done)
.catch(done);
}); });
Nightmare.action('isVisible', function(selector, done) { Nightmare.action('isVisible', function(selector, done) {
@ -157,7 +171,8 @@ Nightmare.action('selectText', function(selector, done) {
sel.addRange(range); sel.addRange(range);
}, selector) }, selector)
.mouseup(selector) .mouseup(selector)
.then(done); .then(done)
.catch(done);
}); });
Nightmare.action('countElement', function(selector, done) { Nightmare.action('countElement', function(selector, done) {
@ -167,44 +182,54 @@ Nightmare.action('countElement', function(selector, done) {
}); });
Nightmare.action('waitForNumberOfElements', function(selector, count, done) { Nightmare.action('waitForNumberOfElements', function(selector, count, done) {
this.wait((resultSelector, expectedCount) => { this.wait((selector, count) => {
return document.querySelectorAll(resultSelector).length === expectedCount; return document.querySelectorAll(selector).length === count;
}, selector, 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) { Nightmare.action('waitForClassNotPresent', function(selector, className, done) {
this.wait(selector) this.wait(selector)
.wait((resultSelector, targetClass) => { .wait((selector, className) => {
if (!document.querySelector(resultSelector).classList.contains(targetClass)) if (!document.querySelector(selector).classList.contains(className))
return true; return true;
}, selector, className) }, selector, className)
.then(done); .then(done)
.catch(() => {
done(new Error(`.waitForClassNotPresent() for ${selector}, class ${className} timed out`));
});
}); });
Nightmare.action('waitForClassPresent', function(selector, className, done) { Nightmare.action('waitForClassPresent', function(selector, className, done) {
this.wait(selector) this.wait(selector)
.wait((resultSelector, targetClass) => { .wait((selector, className) => {
if (document.querySelector(resultSelector).classList.contains(targetClass)) if (document.querySelector(selector).classList.contains(className))
return true; return true;
}, selector, className) }, selector, className)
.then(done); .then(done)
.catch(done);
}); });
Nightmare.action('waitForTextInElement', function(selector, name, done) { Nightmare.action('waitForTextInElement', function(selector, name, done) {
this.wait(selector) this.wait(selector)
.wait((resultSelector, expectedText) => { .wait((selector, name) => {
return document.querySelector(resultSelector).innerText.toLowerCase().includes(expectedText.toLowerCase()); return document.querySelector(selector).innerText.toLowerCase().includes(name.toLowerCase());
}, selector, name) }, selector, name)
.then(done); .then(done)
.catch(done);
}); });
Nightmare.action('waitForTextInInput', function(selector, name, done) { Nightmare.action('waitForTextInInput', function(selector, name, done) {
this.wait(selector) this.wait(selector)
.wait((resultSelector, expectedText) => { .wait((selector, name) => {
return document.querySelector(resultSelector).value.toLowerCase().includes(expectedText.toLowerCase()); return document.querySelector(selector).value.toLowerCase().includes(name.toLowerCase());
}, selector, name) }, selector, name)
.then(done); .then(done)
.catch(done);
}); });
Nightmare.action('waitForInnerText', function(selector, done) { Nightmare.action('waitForInnerText', function(selector, done) {
@ -222,14 +247,16 @@ Nightmare.action('waitForEmptyInnerText', function(selector, done) {
this.wait(selector => { this.wait(selector => {
return document.querySelector(selector).innerText == ''; return document.querySelector(selector).innerText == '';
}, selector) }, selector)
.then(done); .then(done)
.catch(done);
}); });
Nightmare.action('waitForURL', function(hashURL, done) { Nightmare.action('waitForURL', function(hashURL, done) {
this.wait(hash => { this.wait(hash => {
return document.location.hash.includes(hash); return document.location.hash.includes(hash);
}, hashURL) }, hashURL)
.then(done); .then(done)
.catch(done);
}); });
Nightmare.action('waitForShapes', function(selector, done) { Nightmare.action('waitForShapes', function(selector, done) {
@ -250,7 +277,7 @@ Nightmare.action('waitForSnackbar', function(done) {
this.wait(500).waitForShapes('vn-snackbar .shape .text') this.wait(500).waitForShapes('vn-snackbar .shape .text')
.then(shapes => { .then(shapes => {
done(null, shapes); done(null, shapes);
}); }).catch(done);
}); });
Nightmare.action('waitForLastShape', function(selector, done) { Nightmare.action('waitForLastShape', function(selector, done) {
@ -266,5 +293,5 @@ Nightmare.action('waitForLastSnackbar', function(done) {
this.wait(500).waitForLastShape('vn-snackbar .shape .text') this.wait(500).waitForLastShape('vn-snackbar .shape .text')
.then(shapes => { .then(shapes => {
done(null, shapes); done(null, shapes);
}); }).catch(done);
}); });

View File

@ -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}) const nightmare = new Nightmare({show: process.env.E2E_SHOW, typeInterval: 10, x: 0, y: 0, waitTimeout: 2000})
.viewport(width, height); .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) => { nightmare.on('console', (type, message) => {
if (type === 'error') { if (type === 'error')
fail(message); fail(message);
}
if (type === 'log') {
// console.log(message);
}
}); });
nightmare.header('Accept-Language', 'en'); nightmare.header('Accept-Language', 'en');
afterAll(() => { afterAll(() => {
return nightmare return nightmare
.end(); .end();
}); });
return nightmare; return nightmare;