#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]`)
// 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);
});

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})
.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;