setup.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. interface ShopPage extends Page {
  9. fillInTinymce: (iframeSelector: string, content: string) => Promise<void>;
  10. }
  11. /**
  12. * Fixtures Types
  13. */
  14. type Fixtures = {
  15. adminPage: AdminPage;
  16. shopPage: ShopPage;
  17. };
  18. /**
  19. * Test with Fixtures
  20. */
  21. export const test = base.extend<Fixtures>({
  22. /**
  23. * AdminPage
  24. */
  25. adminPage: async ({ browser }, use) => {
  26. const authExists = fs.existsSync(ADMIN_AUTH_STATE_PATH);
  27. const context = await browser.newContext(
  28. authExists ? { storageState: ADMIN_AUTH_STATE_PATH } : {}
  29. );
  30. const page = await context.newPage();
  31. // Login if needed
  32. if (!authExists) {
  33. await loginAsAdmin(page);
  34. await context.storageState({ path: ADMIN_AUTH_STATE_PATH });
  35. } else {
  36. await page.goto("admin/dashboard");
  37. }
  38. // Safety check (session expired)
  39. if (page.url().includes("admin/login")) {
  40. await loginAsAdmin(page);
  41. await context.storageState({ path: ADMIN_AUTH_STATE_PATH });
  42. }
  43. // Extend admin page
  44. (page as AdminPage).fillInTinymce = async (
  45. iframeSelector: string,
  46. content: string
  47. ) => {
  48. await page.waitForSelector(iframeSelector);
  49. const iframe = page.frameLocator(iframeSelector);
  50. const editorBody = iframe.locator("body");
  51. await expect(editorBody).toBeVisible();
  52. await editorBody.focus();
  53. await editorBody.press("Control+a");
  54. await editorBody.press("Backspace");
  55. await editorBody.pressSequentially(content);
  56. await expect(editorBody).toHaveText(content);
  57. };
  58. await use(page as AdminPage);
  59. await context.close();
  60. },
  61. /**
  62. * Shop Page
  63. */
  64. shopPage: async ({ browser }, use) => {
  65. const context = await browser.newContext();
  66. const page = await context.newPage();
  67. /**
  68. * Extend shop page with Tinymce helper
  69. * (exact logic you provided)
  70. */
  71. (page as ShopPage).fillInTinymce = async (
  72. iframeSelector: string,
  73. content: string
  74. ) => {
  75. await page.waitForSelector(iframeSelector);
  76. const iframe = page.frameLocator(iframeSelector);
  77. const editorBody = iframe.locator("body");
  78. await editorBody.click();
  79. await editorBody.pressSequentially(content);
  80. await expect(editorBody).toHaveText(content);
  81. };
  82. await use(page as ShopPage);
  83. await context.close();
  84. },
  85. });
  86. export { expect };