You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.4 KiB

4 years ago
  1. var srcFolder = '../../../src/node_modules/';
  2. var wd = require(`${srcFolder}wd`);
  3. var async = require(`${srcFolder}async`);
  4. var config = {
  5. host: 'ondemand.saucelabs.com',
  6. port: 80,
  7. username: process.env.SAUCE_USER,
  8. accessKey: process.env.SAUCE_ACCESS_KEY,
  9. };
  10. var allTestsPassed = true;
  11. // overwrite the default exit code
  12. // in case not all worker can be run (due to saucelabs limits), `queue.drain` below will not be called
  13. // and the script would silently exit with error code 0
  14. process.exitCode = 2;
  15. process.on('exit', (code) => {
  16. if (code === 2) {
  17. console.log('\x1B[31mFAILED\x1B[39m Not all saucelabs runner have been started.');
  18. }
  19. });
  20. var sauceTestWorker = async.queue((testSettings, callback) => {
  21. const browser = wd.promiseChainRemote(config.host, config.port, config.username, config.accessKey);
  22. const name = `${process.env.GIT_HASH} - ${testSettings.browserName} ${testSettings.version}, ${testSettings.platform}`;
  23. testSettings.name = name;
  24. testSettings.public = true;
  25. testSettings.build = process.env.GIT_HASH;
  26. testSettings.extendedDebugging = true; // console.json can be downloaded via saucelabs, don't know how to print them into output of the tests
  27. testSettings.tunnelIdentifier = process.env.TRAVIS_JOB_NUMBER;
  28. browser.init(testSettings).get('http://localhost:9001/tests/frontend/', () => {
  29. const url = `https://saucelabs.com/jobs/${browser.sessionID}`;
  30. console.log(`Remote sauce test '${name}' started! ${url}`);
  31. // tear down the test excecution
  32. const stopSauce = function (success, timesup) {
  33. clearInterval(getStatusInterval);
  34. clearTimeout(timeout);
  35. browser.quit(() => {
  36. if (!success) {
  37. allTestsPassed = false;
  38. }
  39. // if stopSauce is called via timeout (in contrast to via getStatusInterval) than the log of up to the last
  40. // five seconds may not be available here. It's an error anyway, so don't care about it.
  41. printLog(logIndex);
  42. if (timesup) {
  43. console.log(`[${testSettings.browserName} ${testSettings.platform}${testSettings.version === '' ? '' : (` ${testSettings.version}`)}] \x1B[31mFAILED\x1B[39m allowed test duration exceeded`);
  44. }
  45. console.log(`Remote sauce test '${name}' finished! ${url}`);
  46. callback();
  47. });
  48. };
  49. /**
  50. * timeout if a test hangs or the job exceeds 14.5 minutes
  51. * It's necessary because if travis kills the saucelabs session due to inactivity, we don't get any output
  52. * @todo this should be configured in testSettings, see https://wiki.saucelabs.com/display/DOCS/Test+Configuration+Options#TestConfigurationOptions-Timeouts
  53. */
  54. var timeout = setTimeout(() => {
  55. stopSauce(false, true);
  56. }, 870000); // travis timeout is 15 minutes, set this to a slightly lower value
  57. let knownConsoleText = '';
  58. // how many characters of the log have been sent to travis
  59. let logIndex = 0;
  60. var getStatusInterval = setInterval(() => {
  61. browser.eval("$('#console').text()", (err, consoleText) => {
  62. if (!consoleText || err) {
  63. return;
  64. }
  65. knownConsoleText = consoleText;
  66. if (knownConsoleText.indexOf('FINISHED') > 0) {
  67. const match = knownConsoleText.match(/FINISHED.*([0-9]+) tests passed, ([0-9]+) tests failed/);
  68. // finished without failures
  69. if (match[2] && match[2] == '0') {
  70. stopSauce(true);
  71. // finished but some tests did not return or some tests failed
  72. } else {
  73. stopSauce(false);
  74. }
  75. } else {
  76. // not finished yet
  77. printLog(logIndex);
  78. logIndex = knownConsoleText.length;
  79. }
  80. });
  81. }, 5000);
  82. /**
  83. * Replaces color codes in the test runners log, appends
  84. * browser name, platform etc. to every line and prints them.
  85. *
  86. * @param {number} index offset from where to start
  87. */
  88. function printLog(index) {
  89. let testResult = knownConsoleText.substring(index).replace(/\[red\]/g, '\x1B[31m').replace(/\[yellow\]/g, '\x1B[33m')
  90. .replace(/\[green\]/g, '\x1B[32m').replace(/\[clear\]/g, '\x1B[39m');
  91. testResult = testResult.split('\\n').map((line) => `[${testSettings.browserName} ${testSettings.platform}${testSettings.version === '' ? '' : (` ${testSettings.version}`)}] ${line}`).join('\n');
  92. console.log(testResult);
  93. }
  94. });
  95. }, 6); // run 6 tests in parrallel
  96. // 1) Firefox on Linux
  97. sauceTestWorker.push({
  98. platform: 'Windows 7',
  99. browserName: 'firefox',
  100. version: '52.0',
  101. });
  102. // 2) Chrome on Linux
  103. sauceTestWorker.push({
  104. platform: 'Windows 7',
  105. browserName: 'chrome',
  106. version: '55.0',
  107. args: ['--use-fake-device-for-media-stream'],
  108. });
  109. /*
  110. // 3) Safari on OSX 10.15
  111. sauceTestWorker.push({
  112. 'platform' : 'OS X 10.15'
  113. , 'browserName' : 'safari'
  114. , 'version' : '13.1'
  115. });
  116. */
  117. // 4) Safari on OSX 10.14
  118. sauceTestWorker.push({
  119. platform: 'OS X 10.15',
  120. browserName: 'safari',
  121. version: '13.1',
  122. });
  123. // IE 10 doesn't appear to be working anyway
  124. /*
  125. // 4) IE 10 on Win 8
  126. sauceTestWorker.push({
  127. 'platform' : 'Windows 8'
  128. , 'browserName' : 'iexplore'
  129. , 'version' : '10.0'
  130. });
  131. */
  132. // 5) Edge on Win 10
  133. sauceTestWorker.push({
  134. platform: 'Windows 10',
  135. browserName: 'microsoftedge',
  136. version: '83.0',
  137. });
  138. // 6) Firefox on Win 7
  139. sauceTestWorker.push({
  140. platform: 'Windows 7',
  141. browserName: 'firefox',
  142. version: '78.0',
  143. });
  144. sauceTestWorker.drain(() => {
  145. process.exit(allTestsPassed ? 0 : 1);
  146. });