refactor: migrate e2e tests and components to use data-testid attributes

This commit is contained in:
me
2026-05-06 22:48:06 +05:30
parent 8d0ab979f5
commit 548b34fb51
16 changed files with 395 additions and 387 deletions
+23 -53
View File
@@ -1,6 +1,7 @@
import { expect, test } from "@playwright/test";
import pino from "pino";
import { AuthHelper } from "./utils/auth";
import { revealEnvelope } from "./utils/envelope";
const logger = pino({
transport: {
@@ -22,12 +23,12 @@ test.describe("Letter Drafting (Real Backend)", () => {
await AuthHelper.registerAndLogin(page, email, name, password);
logger.info(">> [Draft] Navigating to Editor via UI...");
await page.getByRole("button", { name: /write something/i }).click();
await page.getByTestId("write-letter-btn").click();
logger.info(`>> [Draft] Current URL after click: ${page.url()}`);
// Wait for the recipient input to be present in the DOM
const recipientInput = page.locator("#recipient");
const recipientInput = page.getByTestId("recipient-input");
await recipientInput.waitFor({ state: "visible", timeout: 20000 });
const recipientName = "Dear Friend";
@@ -46,7 +47,7 @@ test.describe("Letter Drafting (Real Backend)", () => {
await page.keyboard.press("Enter");
await page.keyboard.type("It should persist.");
logger.info(">> [Draft] Clicking Draft...");
await page.getByRole("button", { name: /draft/i }).click();
await page.getByTestId("draft-btn").click();
// Verify Success Modal/Alert
await expect(page.getByText(/your letter is saved/i)).toBeVisible();
@@ -70,7 +71,7 @@ test.describe("Letter Drafting (Real Backend)", () => {
});
// Check recipient
await expect(page.locator("#recipient")).toHaveValue(recipientName);
await expect(page.getByTestId("recipient-input")).toHaveValue(recipientName);
// Check canvas content
// We wait for the content to appear in the textarea.
@@ -92,9 +93,9 @@ test.describe("Letter Drafting (Real Backend)", () => {
await AuthHelper.registerAndLogin(page, email, name, password);
logger.info(">> [Seal] Navigating to Editor via UI...");
await page.locator("#write-letter-btn").click();
await page.getByTestId("write-letter-btn").click();
const recipientInput = page.locator("#recipient");
const recipientInput = page.getByTestId("recipient-input");
await recipientInput.waitFor({ state: "visible", timeout: 10000 });
await recipientInput.fill("A Secret Guest");
@@ -104,14 +105,8 @@ test.describe("Letter Drafting (Real Backend)", () => {
// Click Seal (open menu, then confirm)
logger.info(">> [Seal] Clicking Seal...");
await page
.getByRole("button", { name: /seal/i })
.filter({ visible: true })
.click();
await page
.getByRole("button", { name: /seal/i })
.filter({ visible: true })
.click();
await page.getByTestId("seal-trigger-btn").click();
await page.getByTestId("seal-confirm-btn").click();
// Should show sealed confirmation modal
logger.info(">> [Seal] Verifying sealed modal...");
@@ -120,7 +115,7 @@ test.describe("Letter Drafting (Real Backend)", () => {
});
// Navigate to Reader via "View letter"
await page.getByRole("button", { name: /view letter/i }).click();
await page.getByTestId("view-letter-btn").click();
// Should be on Reader URL
await expect(page).toHaveURL(/\/read\/[a-f0-9-]{36}$/, { timeout: 15000 });
@@ -129,18 +124,13 @@ test.describe("Letter Drafting (Real Backend)", () => {
await expect(page.getByText(/breaking the seal/i)).toBeHidden({
timeout: 10000,
});
// Flip the envelope to show the seal
await page.locator("#env-front").click();
await page.waitForTimeout(2500); // Wait for flip transition
await page.getByAltText("Seal").click();
await page.waitForTimeout(1500);
await page.locator("#letter").click({ position: { x: 30, y: 15 } });
await expect(page.locator("#letter")).toBeHidden({ timeout: 20000 });
// Flip the envelope to show the seal and reveal the letter
await revealEnvelope(page);
await expect(page.getByTestId("envelope-letter")).toBeHidden({ timeout: 20000 });
// Share on demand
logger.info(">> [Seal] Clicking Share button in Reader...");
await page.locator("#share-letter-btn").click();
await page.getByTestId("share-letter-btn").click();
// Verify share modal with a valid link
await expect(page.getByText(/send this letter/i)).toBeVisible();
@@ -151,6 +141,7 @@ test.describe("Letter Drafting (Real Backend)", () => {
logger.info(`>> [Seal] Sharing link: ${linkValue}`);
await expect(page.getByRole("button", { name: /copy/i })).toBeVisible();
// Assuming Close button in ShareModal might need a testid too, but for now let's use text if unique or add testid
await page.getByRole("button", { name: /close/i }).click();
await expect(page.getByText(/send this letter/i)).toBeHidden();
});
@@ -167,9 +158,9 @@ test.describe("Letter Drafting (Real Backend)", () => {
await AuthHelper.registerAndLogin(page, email, name, password);
logger.info(">> [Drawer] Creating and sealing a letter...");
await page.getByRole("button", { name: /write something/i }).click();
await page.getByTestId("write-letter-btn").click();
const recipientInput = page.locator("#recipient");
const recipientInput = page.getByTestId("recipient-input");
await recipientInput.waitFor({ state: "visible" });
await recipientInput.fill(recipientName);
@@ -178,20 +169,14 @@ test.describe("Letter Drafting (Real Backend)", () => {
await canvasInput.fill(letterContent);
// Click Seal (open menu, then confirm)
await page
.getByRole("button", { name: /seal/i })
.filter({ visible: true })
.click();
await page
.getByRole("button", { name: /seal/i })
.filter({ visible: true })
.click();
await page.getByTestId("seal-trigger-btn").click();
await page.getByTestId("seal-confirm-btn").click();
// Sealed modal should appear — click "Keep it" to go to Drawer
await expect(page.getByText(/your letter is sealed/i)).toBeVisible({
timeout: 10000,
});
await page.getByRole("button", { name: /keep it to myself/i }).click();
await page.getByTestId("keep-it-btn").click();
// Open "Kept" section - search for the section with id='kept' and click its toggle button
logger.info(">> [Drawer] Opening Kept section...");
@@ -213,24 +198,9 @@ test.describe("Letter Drafting (Real Backend)", () => {
await expect(page.getByText(/breaking the seal/i)).toBeHidden({
timeout: 10000,
});
// Check recipient on the front of the envelope
await expect(page.getByText(new RegExp(recipientName, "i"))).toBeVisible();
// Flip the envelope to the back
await page.getByText(new RegExp(recipientName, "i")).click();
// Wait for flip transition (2s)
await page.waitForTimeout(2500);
// Reveal the letter: click seal then click letter
await page.getByAltText("Seal").click();
// Wait for flap transition
await page.waitForTimeout(1500);
// Click the letter to pull it out
await page.locator("#letter").click({ position: { x: 30, y: 15 } });
// Wait for reveal transition
await expect(page.locator("#letter")).toBeHidden({ timeout: 20000 });
// Flip the envelope and reveal the letter
await revealEnvelope(page);
await expect(page.getByTestId("envelope-letter")).toBeHidden({ timeout: 20000 });
// Also check if we are redirected to the Reader if we manually go to the Editor URL
const readerUrl = page.url();