setup.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { test as base, expect, type Page } from "@playwright/test";
  2. import fs from "fs";
  3. import { ADMIN_AUTH_STATE_PATH } from "./playwright.config";
  4. import { loginAsAdmin } from "./utils/admin";
  5. interface AdminPage extends Page {
  6. fillInTinymce: (iframeSelector: string, content: string) => Promise<void>;
  7. }
  8. type AdminFixtures = {
  9. adminPage: AdminPage;
  10. };
  11. export const test = base.extend<AdminFixtures>({
  12. adminPage: async ({ browser }, use) => {
  13. const authExists = fs.existsSync(ADMIN_AUTH_STATE_PATH);
  14. const context = await browser.newContext(
  15. authExists ? { storageState: ADMIN_AUTH_STATE_PATH } : {}
  16. );
  17. const page = await context.newPage();
  18. if (!authExists) {
  19. /**
  20. * Authenticate the admin user.
  21. */
  22. await loginAsAdmin(page);
  23. /**
  24. * Save authentication state to a file.
  25. */
  26. await context.storageState({ path: ADMIN_AUTH_STATE_PATH });
  27. } else {
  28. /**
  29. * Navigate to the dashboard.
  30. */
  31. await page.goto("admin/dashboard");
  32. }
  33. if (page.url().includes("admin/login")) {
  34. /**
  35. * Authenticate the admin user.
  36. */
  37. await loginAsAdmin(page);
  38. /**
  39. * Save authentication state to a file.
  40. */
  41. await context.storageState({ path: ADMIN_AUTH_STATE_PATH });
  42. }
  43. /**
  44. * Extend the page object with custom methods.
  45. */
  46. (page as AdminPage).fillInTinymce = async function (
  47. iframeSelector: string,
  48. content: string
  49. ) {
  50. await page.waitForSelector(iframeSelector);
  51. const iframe = page.frameLocator(iframeSelector);
  52. const editorBody = iframe.locator("body");
  53. await editorBody.click();
  54. await editorBody.press("Control+a");
  55. await editorBody.press("Backspace");
  56. await editorBody.pressSequentially(content);
  57. await expect(editorBody).toHaveText(content);
  58. };
  59. await use(page as AdminPage);
  60. await context.close();
  61. },
  62. });
  63. export { expect };