salix/e2e/helpers/extensions.js

303 lines
10 KiB
JavaScript
Raw Normal View History

2018-10-25 14:44:03 +00:00
/* eslint no-invalid-this: "off" */
import selectors from './selectors.js';
2017-09-15 10:24:37 +00:00
import config from './config.js';
import Nightmare from 'nightmare';
import {URL} from 'url';
2017-09-15 10:24:37 +00:00
2018-11-21 15:59:09 +00:00
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) {
2018-10-25 14:44:03 +00:00
const backSpaces = [];
for (let i = 0; i < 50; i += 1)
2017-09-15 10:24:37 +00:00
backSpaces.push('\u0008');
2018-11-21 15:59:09 +00:00
await this.wait(selector)
.type(selector, backSpaces.join(''));
2017-09-15 10:24:37 +00:00
});
let actions = {
login: function(userName, done) {
this.goto(`${config.url}auth/?apiKey=salix`)
.wait(`vn-login input[name=user]`)
.write(`vn-login input[name=user]`, userName)
.write(`vn-login input[name=password]`, 'nightmare')
.click(`vn-login input[type=submit]`)
// FIXME: Wait for dom to be ready: https://github.com/segmentio/nightmare/issues/481
.wait(1000)
.then(done)
.catch(done);
},
changeLanguageToEnglish: function(done) {
this.wait('#lang')
.evaluate(selector => {
return document.querySelector(selector).title;
}, '#lang')
.then(title => {
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)
.catch(done);
}
});
},
waitForLogin: function(userName, done) {
this.login(userName)
.waitForURL('#!/')
.url()
.changeLanguageToEnglish()
.then(done)
.catch(done);
},
selectModule: function(moduleName, done) {
this.waitToClick(`body > vn-app > vn-vertical > vn-vertical > vn-home > div > div > a[ui-sref="${moduleName}.index"]`)
.waitForURL(moduleName)
.then(done)
.catch(done);
},
loginAndModule: function(userName, moduleName, done) {
this.waitForLogin(userName)
.selectModule(moduleName)
.then(done)
.catch(done);
},
parsedUrl: function(done) {
this.url()
.then(url => {
done(null, new URL(url));
}).catch(done);
},
getProperty: function(selector, property, done) {
this.evaluate_now((selector, property) => {
return document.querySelector(selector)[property].replace(/\s+/g, ' ').trim();
}, done, selector, property);
},
waitPropertyLength: function(selector, property, minLength, done) {
this.wait((selector, property, minLength) => {
const element = document.querySelector(selector);
return element && element[property] != null && element[property] !== '' && element[property].length >= minLength;
}, selector, property, minLength)
.getProperty(selector, property)
.then(result => done(null, result), done);
},
waitToGetProperty: function(selector, property, done) {
this.wait((selector, property) => {
const element = document.querySelector(selector);
return element && element[property] != null && element[property] !== '';
}, selector, property)
.getProperty(selector, property)
.then(result => done(null, result), done);
},
write: function(selector, text, done) {
this.wait(selector)
.type(selector, text)
.then(done)
.catch(done);
},
2017-09-15 10:24:37 +00:00
waitToClick: function(selector, done) {
this.wait(selector)
.click(selector)
.then(done)
.catch(done);
},
isVisible: function(selector, done) {
this.wait(selector)
.evaluate_now(elementSelector => {
const selectorMatches = document.querySelectorAll(elementSelector);
const element = selectorMatches[0];
if (selectorMatches.length > 1)
throw new Error(`multiple matches of ${elementSelector} found`);
let isVisible = false;
if (element) {
const eventHandler = event => {
event.preventDefault();
isVisible = true;
};
element.addEventListener('mouseover', eventHandler);
const elementBoundaries = element.getBoundingClientRect();
const x = elementBoundaries.left + element.offsetWidth / 2;
const y = elementBoundaries.top + element.offsetHeight / 2;
const elementInCenter = document.elementFromPoint(x, y);
const elementInTopLeft = document.elementFromPoint(elementBoundaries.left, elementBoundaries.top);
const elementInBottomRight = document.elementFromPoint(elementBoundaries.right, elementBoundaries.bottom);
const e = new MouseEvent('mouseover', {
view: window,
bubbles: true,
cancelable: true,
});
if (elementInCenter)
elementInCenter.dispatchEvent(e);
if (elementInTopLeft)
elementInTopLeft.dispatchEvent(e);
if (elementInBottomRight)
elementInBottomRight.dispatchEvent(e);
element.removeEventListener('mouseover', eventHandler);
}
return isVisible;
}, done, selector);
},
countElement: function(selector, done) {
this.evaluate_now(selector => {
return document.querySelectorAll(selector).length;
2018-10-25 14:44:03 +00:00
}, done, selector);
},
waitForNumberOfElements: function(selector, count, done) {
this.wait((selector, count) => {
return document.querySelectorAll(selector).length === count;
}, selector, count)
.then(done)
.catch(err => {
console.error(err.name, err.message);
done(new Error(`.waitForNumberOfElements() for ${selector}, count ${count} timed out`));
});
},
waitForClassNotPresent: function(selector, className, done) {
this.wait(selector)
.wait((selector, className) => {
if (!document.querySelector(selector).classList.contains(className))
return true;
}, selector, className)
.then(done)
.catch(() => {
done(new Error(`.waitForClassNotPresent() for ${selector}, class ${className} timed out`));
});
},
waitForClassPresent: function(selector, className, done) {
this.wait(selector)
.wait((selector, className) => {
if (document.querySelector(selector).classList.contains(className))
return true;
}, selector, className)
.then(done)
.catch(done);
},
waitForTextInElement: function(selector, name, done) {
this.wait(selector)
.wait((selector, name) => {
return document.querySelector(selector).innerText.toLowerCase().includes(name.toLowerCase());
}, selector, name)
.then(done)
.catch(done);
},
waitForTextInInput: function(selector, name, done) {
this.wait(selector)
.wait((selector, name) => {
return document.querySelector(selector).value.toLowerCase().includes(name.toLowerCase());
}, selector, name)
.then(done)
.catch(done);
},
waitForInnerText: function(selector, done) {
this.wait(selector)
.wait(selector => {
const innerText = document.querySelector(selector).innerText;
return innerText != null && innerText != '';
}, selector)
.evaluate_now(selector => {
return document.querySelector(selector).innerText;
}, done, selector);
},
waitForEmptyInnerText: function(selector, done) {
this.wait(selector => {
return document.querySelector(selector).innerText == '';
2018-10-25 14:44:03 +00:00
}, selector)
.then(done)
.catch(done);
},
waitForURL: function(hashURL, done) {
this.wait(hash => {
return document.location.hash.includes(hash);
}, hashURL)
.then(done)
.catch(done);
},
waitForShapes: function(selector, done) {
this.wait(selector)
.evaluate_now(selector => {
const shapes = document.querySelectorAll(selector);
const shapesList = [];
for (const shape of shapes)
shapesList.push(shape.innerText);
return shapesList;
}, done, selector);
},
waitForSnackbar: function(done) {
this.wait(500).waitForShapes('vn-snackbar .shape .text')
.then(shapes => {
done(null, shapes);
}).catch(done);
},
waitForLastShape: function(selector, done) {
this.wait(selector)
.evaluate_now(selector => {
const shape = document.querySelector(selector);
return shape.innerText;
}, done, selector);
},
waitForLastSnackbar: function(done) {
this.wait(500).waitForLastShape('vn-snackbar .shape .text')
.then(shapes => {
done(null, shapes);
}).catch(done);
},
// indexSearchToSection: function(searchValue, sectionName, done) {
// this.wait(`vn-${sectionName}-index vn-searchbar input`)
// .type(`vn-${sectionName}-index vn-searchbar input`, searchValue)
// .click(`vn-${sectionName}-index vn-searchbar vn-icon[icon="search"]`)
// .waitForNumberOfElements(`vn-${sectionName}-index > div > div > vn-card:nth-child(2) > div > vn-item-client > a`, 1)
// .countElement(selectors.clientsIndex.searchResult);
// expect(resultCount).toEqual(1);
// }
};
Object.keys(actions).forEach(function(name) {
let fn = actions[name];
Nightmare.action(name, fn);
});