globals.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { spawn } from 'child_process';
  2. import path from 'path';
  3. import fs from 'fs';
  4. import mkdirp from 'mkdirp';
  5. import chromedriver from 'chromedriver';
  6. import nightwatchSettings from './nightwatch.conf';
  7. const commandAsWebserver = command => {
  8. if (process.env.DRUPAL_TEST_WEBSERVER_USER) {
  9. return `sudo -u ${process.env.DRUPAL_TEST_WEBSERVER_USER} ${command}`;
  10. }
  11. return command;
  12. };
  13. module.exports = {
  14. before: done => {
  15. if (JSON.parse(process.env.DRUPAL_TEST_CHROMEDRIVER_AUTOSTART)) {
  16. chromedriver.start();
  17. }
  18. done();
  19. },
  20. after: done => {
  21. if (JSON.parse(process.env.DRUPAL_TEST_CHROMEDRIVER_AUTOSTART)) {
  22. chromedriver.stop();
  23. }
  24. done();
  25. },
  26. afterEach: (browser, done) => {
  27. // Writes the console log - used by the "logAndEnd" command.
  28. if (
  29. browser.drupalLogConsole &&
  30. (!browser.drupalLogConsoleOnlyOnError ||
  31. browser.currentTest.results.errors > 0 ||
  32. browser.currentTest.results.failed > 0)
  33. ) {
  34. const resultPath = path.join(
  35. __dirname,
  36. `../../../${nightwatchSettings.output_folder}/consoleLogs/${
  37. browser.currentTest.module
  38. }`,
  39. );
  40. const status =
  41. browser.currentTest.results.errors > 0 ||
  42. browser.currentTest.results.failed > 0
  43. ? 'FAILED'
  44. : 'PASSED';
  45. mkdirp.sync(resultPath);
  46. const now = new Date().toString().replace(/[\s]+/g, '-');
  47. const testName = (
  48. browser.currentTest.name || browser.currentTest.module
  49. ).replace(/[\s/]+/g, '-');
  50. browser
  51. .getLog('browser', logEntries => {
  52. const browserLog = JSON.stringify(logEntries, null, ' ');
  53. fs.writeFileSync(
  54. `${resultPath}/${testName}_${status}_${now}_console.json`,
  55. browserLog,
  56. );
  57. })
  58. .end(done);
  59. } else {
  60. browser.end(done);
  61. }
  62. },
  63. commandAsWebserver,
  64. };