81151fcdb39531607f2d7b8ee18a84098bc913c1.svn-base 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. var wd = require('wd');
  2. var http = require("http");
  3. var https = require("https");
  4. var url = require("url");
  5. var path = require("path");
  6. var Promise = require('bluebird');
  7. var _ = require('lodash');
  8. var humanizeDuration = require("humanize-duration");
  9. var utils = require('../utils');
  10. var colors = utils.colors;
  11. var port = 8080;
  12. function runTestWithRetries(browser, test, retries) {
  13. retries = retries || 0;
  14. return runTest(browser, test)
  15. .timeout(30000)
  16. .catch(Promise.TimeoutError, function() {
  17. if (retries < 3) {
  18. console.log(colors.violet, "Retry", (retries + 1), test);
  19. return runTestWithRetries(browser, test, retries + 1);
  20. } else {
  21. throw new Error("Couldn't run test after 3 retries");
  22. }
  23. });
  24. }
  25. function getResults(browser) {
  26. return function() {
  27. return Promise.props({
  28. dataUrl: browser.waitForElementByCss("body[data-complete='true']", 90000).then(function() {
  29. return browser.elementsByCssSelector('.test.fail');
  30. }).then(function(nodes) {
  31. return Array.isArray(nodes) ? Promise.map(nodes, function(node) {
  32. return browser.text(node).then(function(error) {
  33. return Promise.reject(error);
  34. });
  35. }) : Promise.resolve([]);
  36. })
  37. });
  38. };
  39. }
  40. function runTest(browser, test) {
  41. return Promise.resolve(browser
  42. .then(utils.loadTestPage(browser, test, port))
  43. .then(getResults(browser))
  44. ).cancellable();
  45. }
  46. exports.tests = function(browsers, singleTest) {
  47. var path = "tests/mocha";
  48. return (singleTest ? Promise.resolve([singleTest]) : utils.getTests(path)).then(function(tests) {
  49. return Promise.map(browsers, function(settings) {
  50. var name = [settings.browserName, settings.version, settings.platform].join("-");
  51. var count = 0;
  52. var browser = utils.initBrowser(settings);
  53. return Promise.using(browser, function() {
  54. return Promise.map(tests, function(test, index, total) {
  55. console.log(colors.green, "STARTING", "(" + (++count) + "/" + total + ")", name, test, colors.clear);
  56. var start = Date.now();
  57. return runTestWithRetries(browser, test).then(function() {
  58. console.log(colors.green, "COMPLETE", humanizeDuration(Date.now() - start), "(" + count + "/" + total + ")", name, colors.clear);
  59. });
  60. }, {concurrency: 1})
  61. .settle()
  62. .catch(function(error) {
  63. console.error(colors.red, "ERROR", name, error);
  64. throw error;
  65. });
  66. });
  67. }, {concurrency: 3});
  68. });
  69. };