html2canvas/tests/selenium.js

115 lines
4.3 KiB
JavaScript
Raw Normal View History

2012-12-28 21:41:14 +04:00
(function(){
2013-05-29 18:53:47 +04:00
"use strict;";
2014-09-17 01:11:47 +04:00
var wd = require('wd'),
2013-05-29 18:53:47 +04:00
http = require("http"),
2013-05-29 22:16:28 +04:00
https = require("https"),
2013-05-29 18:53:47 +04:00
url = require("url"),
path = require("path"),
base64_arraybuffer = require('base64-arraybuffer'),
2015-12-06 21:32:35 +03:00
PNG = require('pngjs').PNG,
2014-09-17 01:11:47 +04:00
Promise = require('bluebird'),
_ = require('lodash'),
humanizeDuration = require("humanize-duration"),
2014-05-18 18:39:24 +04:00
fs = require("fs");
2013-05-29 18:53:47 +04:00
var utils = require('./utils');
var colors = utils.colors;
2014-09-17 01:11:47 +04:00
Promise.promisifyAll(fs);
2012-12-28 01:06:47 +04:00
var port = 8080;
2012-12-28 01:06:47 +04:00
2013-05-29 18:53:47 +04:00
function getPixelArray(base64) {
2015-12-06 21:32:35 +03:00
return new Promise(function(resolve, reject) {
const arraybuffer = base64_arraybuffer.decode(base64);
new PNG().parse(arraybuffer, (error, data) => {
if (error) {
reject(error);
} else {
resolve(data.data);
}
});
2013-05-29 18:53:47 +04:00
});
}
function calculateDifference(h2cPixels, screenPixels) {
var len = h2cPixels.length, index = 0, diff = 0;
for (; index < len; index++) {
if (screenPixels[index] - h2cPixels[index] !== 0) {
diff++;
}
}
return (100 - (Math.round((diff/h2cPixels.length) * 10000) / 100));
}
2012-12-28 21:41:14 +04:00
2014-09-17 01:11:47 +04:00
function captureScreenshots(browser) {
return function() {
return Promise.props({
dataUrl: browser.waitForElementByCss(".html2canvas", 15000).then(function(node) {
2014-09-17 01:11:47 +04:00
return browser.execute("return arguments[0].toDataURL('image/png').substring(22)", [node]);
}),
screenshot: browser.takeScreenshot()
});
};
}
2014-09-17 01:11:47 +04:00
function analyzeResults(test) {
return function(result) {
return Promise.all([getPixelArray(result.dataUrl), getPixelArray(result.screenshot)]).spread(calculateDifference).then(function(accuracy) {
return {
testCase: test,
accuracy: accuracy,
dataUrl: result.dataUrl,
screenshot: result.screenshot
};
});
};
}
2014-09-17 01:11:47 +04:00
function runTestWithRetries(browser, test, retries) {
retries = retries || 0;
return runTest(browser, test)
.timeout(60000)
.catch(Promise.TimeoutError, function() {
if (retries < 3) {
2014-09-20 22:09:18 +04:00
console.log(colors.violet, "Retry", (retries + 1), test);
2014-09-17 01:11:47 +04:00
return runTestWithRetries(browser, test, retries + 1);
} else {
throw new Error("Couldn't run test after 3 retries");
}
});
}
2014-05-19 00:31:49 +04:00
2014-09-17 01:11:47 +04:00
function runTest(browser, test) {
2014-09-20 22:09:18 +04:00
return Promise.resolve(browser
.then(utils.loadTestPage(browser, test, port))
2014-09-17 01:11:47 +04:00
.then(captureScreenshots(browser))
2015-12-06 21:32:35 +03:00
.then(analyzeResults(test)));
2013-05-29 18:53:47 +04:00
}
2014-08-26 22:03:54 +04:00
exports.tests = function(browsers, singleTest) {
2014-09-17 01:11:47 +04:00
var path = "tests/cases";
return (singleTest ? Promise.resolve([singleTest]) : utils.getTests(path)).then(function(tests) {
2014-09-17 01:11:47 +04:00
return Promise.map(browsers, function(settings) {
var browser = utils.initBrowser(settings);
2014-09-17 01:11:47 +04:00
var name = [settings.browserName, settings.version, settings.platform].join("-");
var count = 0;
return Promise.using(browser, function() {
return Promise.map(tests, function(test, index, total) {
console.log(colors.green, "STARTING", "(" + (++count) + "/" + total + ")", name, test, colors.clear);
var start = Date.now();
return runTestWithRetries(browser, test).then(function(result) {
console.log(colors.green, "COMPLETE", humanizeDuration(Date.now() - start), "(" + count + "/" + total + ")", name, result.testCase.substring(path.length), result.accuracy.toFixed(2) + "%", colors.clear);
});
}, {concurrency: 1})
.settle()
.catch(function(error) {
console.log(colors.red, "ERROR", name, error.message);
throw error;
});
2014-09-17 01:11:47 +04:00
});
}, {concurrency: 3});
});
2014-05-18 18:39:24 +04:00
};
})();