redoc/tests/e2e/helpers.js

108 lines
2.8 KiB
JavaScript
Raw Normal View History

2016-01-15 23:34:28 +03:00
'use strict';
var https = require('https');
function loadJson(url) {
let promise = new Promise((resolve, reject) => {
https.get(url, function(res){
var body = '';
res.on('data', function(chunk){
body += chunk;
});
res.on('end', function(){
let resp = JSON.parse(body);
resolve(resp);
});
}).on('error', function(e){
reject(e);
});
});
return promise;
}
2016-01-16 00:22:11 +03:00
const LogLevel = {
INFO: 800,
WARNING: 900
};
2016-01-15 23:34:28 +03:00
2016-01-16 00:22:11 +03:00
const MAX_ERROR_MESSAGE_SYMBOLS = 128;
2016-01-15 23:34:28 +03:00
//copied from angular/modules/angular2/src/testing/e2e_util.ts
function verifyNoBrowserErrors() {
2016-01-17 14:51:21 +03:00
// IE doesn't support logs method
if (browser.isIE) {
let err = browser.executeScript('return window.redocError');
expect(err).toBeNull();
return;
}
2016-01-15 23:34:28 +03:00
// Bug in ChromeDriver: Need to execute at least one command
// so that the browser logs can be read out!
browser.executeScript('1+1');
browser.manage().logs().get('browser').then(function(browserLog) {
2016-01-16 00:22:11 +03:00
let filteredLog = browserLog.filter((logEntry) => {
2016-01-17 01:36:23 +03:00
let message = logEntry.message;
2016-01-17 14:51:21 +03:00
// skip browser-sync errors
if (message.indexOf('browser-sync') > -1) return false;
// skip firefox-specific warning
if (message.indexOf('mutating the [[Prototype]]') > -1) return false;
2016-08-29 07:34:44 +03:00
if (message.match(/^Unknown property.*Declaration dropped/)) return false;
if (message.match(/^Error in parsing value for.*Declaration dropped/)) return false;
if (message.indexOf('The character encoding of the HTML document was not declared') > -1) return false;
if (message.match(/addons.manager\s+DEBUG/)) return false;
2016-01-17 14:51:21 +03:00
2016-01-16 00:22:11 +03:00
if (logEntry.level.value >= LogLevel.INFO) {
if (message.length > MAX_ERROR_MESSAGE_SYMBOLS) {
message = message.substr(0, MAX_ERROR_MESSAGE_SYMBOLS) + '...';
}
console.log('>> ' + message);
2016-01-15 23:34:28 +03:00
}
2016-01-17 01:36:23 +03:00
2016-01-16 00:22:11 +03:00
return logEntry.level.value > LogLevel.WARNING;
2016-01-15 23:34:28 +03:00
});
2016-01-16 00:22:11 +03:00
expect(filteredLog.length).toEqual(0, `Found ${filteredLog.length} browser errors`);
2016-01-15 23:34:28 +03:00
});
}
function scrollToEl(selector) {
let script = `
document.querySelector('${selector}').scrollIntoView(true);
window.scrollBy(0, 200);
2016-01-15 23:34:28 +03:00
`;
return browser.driver.executeScript(script);
}
2016-01-17 14:51:21 +03:00
function fixFFTest(done) {
// firefox issue. the first try to access something from webpage fails
// but the second works fine. So skipping first error
$('body').isPresent().then(()=> {
done();
}, () => {
//error skipped
done();
})
}
/* picks each n-th property from object */
function eachNth(obj, n) {
let res = {};
Object.keys(obj).forEach((k, idx) => {
if (idx % n === 0) {
res[k] = obj[k];
}
});
return res;
}
2016-01-15 23:34:28 +03:00
module.exports = {
loadJson: loadJson,
verifyNoBrowserErrors: verifyNoBrowserErrors,
2016-01-17 14:51:21 +03:00
scrollToEl: scrollToEl,
fixFFTest: fixFFTest,
eachNth: eachNth
2016-01-15 23:34:28 +03:00
}