setup.js 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. const { mkdir, writeFile } = require('fs').promises;
  2. const os = require('os');
  3. const path = require('path');
  4. const puppeteer = require('puppeteer');
  5. const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');
  6. /**
  7. * Custom Puppeteer setup as documented on https://jestjs.io/docs/puppeteer.
  8. */
  9. module.exports = async () => {
  10. const browser = await puppeteer.launch();
  11. // store the browser instance so we can teardown it later
  12. // this global is only available in the teardown but not in TestEnvironments
  13. global.__BROWSER_GLOBAL__ = browser;
  14. // Make sure this matches the origin defined in PuppeteerEnvironment.js.
  15. const testOrigin = process.env.TEST_ORIGIN ?? 'http://localhost:8000';
  16. // use the file system to expose the wsEndpoint for TestEnvironments
  17. await mkdir(DIR, { recursive: true });
  18. await writeFile(path.join(DIR, 'wsEndpoint'), browser.wsEndpoint());
  19. // Automatically log into the Wagtail admin.
  20. const page = await browser.newPage();
  21. await page.goto(`${testOrigin}/admin/login/`);
  22. await page.type('#id_username', 'admin');
  23. await page.type('#id_password', 'changeme');
  24. await Promise.all([
  25. page.waitForNavigation({ waitUntil: 'load' }),
  26. page.keyboard.press('Enter'),
  27. ]);
  28. };