2017-09-15 10:24:37 +00:00
|
|
|
import config from './config.js';
|
|
|
|
import Nightmare from 'nightmare';
|
2018-02-20 09:00:19 +00:00
|
|
|
import {URL} from 'url';
|
2017-09-15 10:24:37 +00:00
|
|
|
|
2018-03-02 11:15:17 +00:00
|
|
|
Nightmare.action('login', function(userName, done) {
|
2017-10-30 14:37:35 +00:00
|
|
|
this.goto(`${config.url}auth/?apiKey=salix`)
|
2018-02-20 12:48:25 +00:00
|
|
|
.wait(`vn-login input[name=user]`)
|
2018-03-02 11:15:17 +00:00
|
|
|
.write(`vn-login input[name=user]`, userName)
|
2018-02-20 12:48:25 +00:00
|
|
|
.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)
|
2017-10-30 14:37:35 +00:00
|
|
|
.then(done);
|
2017-09-15 10:24:37 +00:00
|
|
|
});
|
|
|
|
|
2018-02-20 09:00:19 +00:00
|
|
|
Nightmare.action('changeLanguageToEnglish', function(done) {
|
2018-02-20 12:48:25 +00:00
|
|
|
this.wait('#lang')
|
2018-02-20 09:00:19 +00:00
|
|
|
.evaluate(selector => {
|
|
|
|
return document.querySelector(selector).title;
|
2018-02-20 12:48:25 +00:00
|
|
|
}, '#lang')
|
2018-02-20 09:00:19 +00:00
|
|
|
.then(title => {
|
|
|
|
if (title === 'Change language') {
|
|
|
|
this.then(done);
|
|
|
|
} else {
|
2018-02-20 12:48:25 +00:00
|
|
|
this.click('#lang')
|
2018-03-13 11:07:28 +00:00
|
|
|
.click('vn-main-menu [vn-id="langs-menu"] ul > li[name="en"]')
|
2018-02-20 09:00:19 +00:00
|
|
|
.then(done);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-03-02 11:15:17 +00:00
|
|
|
Nightmare.action('waitForLogin', function(userName, done) {
|
|
|
|
this.login(userName)
|
2018-02-20 09:00:19 +00:00
|
|
|
.waitForURL('#!/')
|
|
|
|
.url()
|
|
|
|
.changeLanguageToEnglish()
|
|
|
|
.then(done);
|
|
|
|
});
|
|
|
|
|
2018-02-20 21:35:54 +00:00
|
|
|
Nightmare.action('parsedUrl', function(done) {
|
2018-02-20 09:00:19 +00:00
|
|
|
this.url()
|
|
|
|
.then(url => {
|
|
|
|
done(null, new URL(url));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-19 17:54:46 +00:00
|
|
|
Nightmare.action('getProperty', function(selector, property, done) {
|
|
|
|
this.evaluate_now((selector, property) => {
|
|
|
|
return document.querySelector(selector)[property];
|
|
|
|
}, done, selector, property);
|
|
|
|
});
|
|
|
|
|
|
|
|
Nightmare.action('waitProperty', function(selector, property, done) {
|
|
|
|
this.wait((selector, property) => {
|
|
|
|
let element = document.querySelector(selector);
|
|
|
|
return element && element[property] != null && element[property] !== '';
|
|
|
|
}, selector, property)
|
|
|
|
.getProperty(selector, property)
|
|
|
|
.then(result => done(null, result), done);
|
|
|
|
});
|
|
|
|
|
2017-09-15 10:24:37 +00:00
|
|
|
Nightmare.action('getInnerText', function(selector, done) {
|
|
|
|
this.wait(selector)
|
|
|
|
.evaluate_now(function(elementToSelect) {
|
|
|
|
return document.querySelector(elementToSelect).innerText;
|
|
|
|
}, done, selector);
|
|
|
|
});
|
|
|
|
|
2017-10-31 07:14:33 +00:00
|
|
|
Nightmare.action('getInputValue', function(selector, done) {
|
|
|
|
this.wait(selector)
|
|
|
|
.evaluate_now(function(elementToSelect) {
|
2017-11-08 14:46:19 +00:00
|
|
|
return document.querySelector(elementToSelect).value;
|
2017-10-31 07:14:33 +00:00
|
|
|
}, done, selector);
|
|
|
|
});
|
|
|
|
|
2017-09-15 10:24:37 +00:00
|
|
|
Nightmare.action('clearInput', function(selector, done) {
|
|
|
|
let backSpaces = [];
|
|
|
|
for (let i = 0; i < 50; i += 1) {
|
|
|
|
backSpaces.push('\u0008');
|
|
|
|
}
|
|
|
|
this.wait(selector)
|
|
|
|
.type(selector, backSpaces.join(''))
|
|
|
|
.then(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
Nightmare.action('write', function(selector, text, done) {
|
|
|
|
this.wait(selector)
|
2017-09-17 16:09:59 +00:00
|
|
|
.type(selector, text)
|
2017-09-15 10:24:37 +00:00
|
|
|
.then(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
Nightmare.action('waitToClick', function(selector, done) {
|
|
|
|
this.wait(selector)
|
|
|
|
.click(selector)
|
|
|
|
.then(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
Nightmare.action('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);
|
|
|
|
});
|
|
|
|
|
|
|
|
Nightmare.action('selectText', function(selector, done) {
|
|
|
|
this.wait(selector)
|
|
|
|
.evaluate(elementToSelect => {
|
|
|
|
const range = document.createRange();
|
|
|
|
range.selectNodeContents(document.querySelector(elementToSelect));
|
|
|
|
const sel = window.getSelection();
|
|
|
|
sel.removeAllRanges();
|
|
|
|
sel.addRange(range);
|
|
|
|
}, selector)
|
|
|
|
.mouseup(selector)
|
|
|
|
.then(done);
|
|
|
|
});
|
2017-09-18 11:03:15 +00:00
|
|
|
|
2018-08-02 12:07:57 +00:00
|
|
|
Nightmare.action('countElement', function(selector, done) {
|
2017-09-18 11:03:15 +00:00
|
|
|
this.evaluate_now(selector => {
|
|
|
|
return document.querySelectorAll(selector).length;
|
|
|
|
}, done, selector);
|
|
|
|
});
|
|
|
|
|
|
|
|
Nightmare.action('waitForNumberOfElements', function(selector, count, done) {
|
2017-10-29 14:49:44 +00:00
|
|
|
this.wait((resultSelector, expectedCount) => {
|
|
|
|
return document.querySelectorAll(resultSelector).length === expectedCount;
|
|
|
|
}, selector, count)
|
2017-09-18 11:03:15 +00:00
|
|
|
.then(done);
|
|
|
|
});
|
2017-09-18 11:25:48 +00:00
|
|
|
|
2018-09-10 08:23:26 +00:00
|
|
|
Nightmare.action('waitForClassNotPresent', function(selector, className, done) {
|
|
|
|
this.wait(selector)
|
|
|
|
.wait((resultSelector, targetClass) => {
|
|
|
|
if (!document.querySelector(resultSelector).classList.contains(targetClass)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}, selector, className)
|
|
|
|
.then(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
Nightmare.action('waitForClassPresent', function(selector, className, done) {
|
|
|
|
this.wait(selector)
|
|
|
|
.wait((resultSelector, targetClass) => {
|
|
|
|
if (document.querySelector(resultSelector).classList.contains(targetClass)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}, selector, className)
|
|
|
|
.then(done);
|
|
|
|
});
|
|
|
|
|
2017-10-30 14:37:35 +00:00
|
|
|
Nightmare.action('waitForTextInElement', function(selector, name, done) {
|
2018-03-05 14:21:14 +00:00
|
|
|
this.wait(selector)
|
|
|
|
.wait((resultSelector, expectedText) => {
|
2018-02-15 11:28:05 +00:00
|
|
|
return document.querySelector(resultSelector).innerText.toLowerCase().includes(expectedText.toLowerCase());
|
|
|
|
}, selector, name)
|
|
|
|
.then(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
Nightmare.action('waitForTextInInput', function(selector, name, done) {
|
2018-03-05 14:21:14 +00:00
|
|
|
this.wait(selector)
|
|
|
|
.wait((resultSelector, expectedText) => {
|
2018-02-15 11:28:05 +00:00
|
|
|
return document.querySelector(resultSelector).value.toLowerCase().includes(expectedText.toLowerCase());
|
2017-10-30 14:37:35 +00:00
|
|
|
}, selector, name)
|
|
|
|
.then(done);
|
|
|
|
});
|
|
|
|
|
2018-02-20 09:00:19 +00:00
|
|
|
Nightmare.action('waitForInnerText', function(selector, done) {
|
|
|
|
this.wait(selector)
|
|
|
|
.wait(selector => {
|
|
|
|
let innerText = document.querySelector(selector).innerText;
|
|
|
|
return innerText != null && innerText != '';
|
|
|
|
}, selector)
|
|
|
|
.evaluate_now(selector => {
|
|
|
|
return document.querySelector(selector).innerText;
|
|
|
|
}, done, selector);
|
|
|
|
});
|
|
|
|
|
|
|
|
Nightmare.action('waitForEmptyInnerText', function(selector, done) {
|
|
|
|
this.wait(selector => {
|
|
|
|
return document.querySelector(selector).innerText == '';
|
|
|
|
}, selector)
|
|
|
|
.then(done);
|
2017-12-07 14:40:16 +00:00
|
|
|
});
|
|
|
|
|
2017-09-18 15:28:47 +00:00
|
|
|
Nightmare.action('waitForURL', function(hashURL, done) {
|
|
|
|
this.wait(hash => {
|
2017-10-31 07:14:33 +00:00
|
|
|
return document.location.hash.includes(hash);
|
2017-09-18 15:28:47 +00:00
|
|
|
}, hashURL)
|
|
|
|
.then(done);
|
|
|
|
});
|
2018-06-19 06:12:36 +00:00
|
|
|
|
|
|
|
Nightmare.action('waitForShapes', function(selector, done) {
|
|
|
|
this.wait(selector)
|
|
|
|
.evaluate_now(selector => {
|
|
|
|
let shapes = document.querySelectorAll(selector);
|
|
|
|
let shapesList = [];
|
|
|
|
|
|
|
|
for (let shape of shapes) {
|
|
|
|
shapesList.push(shape.innerText);
|
|
|
|
}
|
|
|
|
|
|
|
|
return shapesList;
|
|
|
|
}, done, selector);
|
|
|
|
});
|
2018-09-10 08:23:26 +00:00
|
|
|
|
|
|
|
Nightmare.action('waitForSnackbar', function(done) {
|
|
|
|
this.wait(500).waitForShapes('vn-snackbar .shape .text')
|
|
|
|
.then(shapes => {
|
|
|
|
done(null, shapes);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
Nightmare.action('waitForLastShape', function(selector, done) {
|
|
|
|
this.wait(selector)
|
|
|
|
.evaluate_now(selector => {
|
|
|
|
let shape = document.querySelector(selector);
|
|
|
|
|
|
|
|
return shape.innerText;
|
|
|
|
}, done, selector);
|
|
|
|
});
|
|
|
|
|
|
|
|
Nightmare.action('waitForLastSnackbar', function(done) {
|
|
|
|
this.wait(500).waitForLastShape('vn-snackbar .shape .text')
|
|
|
|
.then(shapes => {
|
|
|
|
done(null, shapes);
|
|
|
|
});
|
|
|
|
});
|