globals.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. export 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. export const drupalDbPrefix = null;
  14. export const drupalSitePath = null;
  15. module.exports = {
  16. before: done => {
  17. if (JSON.parse(process.env.DRUPAL_TEST_CHROMEDRIVER_AUTOSTART)) {
  18. chromedriver.start();
  19. }
  20. done();
  21. },
  22. after: done => {
  23. if (JSON.parse(process.env.DRUPAL_TEST_CHROMEDRIVER_AUTOSTART)) {
  24. chromedriver.stop();
  25. }
  26. done();
  27. },
  28. afterEach: (browser, done) => {
  29. // Writes the console log - used by the "logAndEnd" command.
  30. if (
  31. browser.drupalLogConsole &&
  32. (!browser.drupalLogConsoleOnlyOnError ||
  33. browser.currentTest.results.errors > 0 ||
  34. browser.currentTest.results.failed > 0)
  35. ) {
  36. const resultPath = path.join(
  37. __dirname,
  38. `../../../${nightwatchSettings.output_folder}/consoleLogs/${browser.currentTest.module}`,
  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. };