working
This commit is contained in:
parent
c0999370c6
commit
4e1a339432
17 changed files with 393 additions and 91 deletions
|
|
@ -4,9 +4,12 @@ import { Page } from "@playwright/test";
|
|||
* Auth helpers for e2e tests.
|
||||
*/
|
||||
|
||||
const API_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000";
|
||||
import { getBackendUrl } from "./backend-url";
|
||||
|
||||
export { API_URL };
|
||||
// Use dynamic backend URL based on worker index
|
||||
// Note: API_URL is evaluated at module load time, so it may use default value
|
||||
// For dynamic per-test URLs, use getBackendUrl() directly
|
||||
export const getApiUrl = () => getBackendUrl();
|
||||
|
||||
export function getRequiredEnv(name: string): string {
|
||||
const value = process.env[name];
|
||||
|
|
@ -30,23 +33,10 @@ export async function clearAuth(page: Page) {
|
|||
await page.context().clearCookies();
|
||||
}
|
||||
|
||||
export async function setEnglishLanguage(page: Page) {
|
||||
// Set English language in localStorage
|
||||
await page.evaluate(() => {
|
||||
window.localStorage.setItem("arbret-locale", "en");
|
||||
});
|
||||
}
|
||||
|
||||
export async function loginUser(page: Page, email: string, password: string) {
|
||||
await page.goto("/login");
|
||||
// Set language after navigation to ensure localStorage is available
|
||||
await setEnglishLanguage(page);
|
||||
// Reload to apply language setting
|
||||
await page.reload();
|
||||
await page.fill('input[type="email"]', email);
|
||||
await page.fill('input[type="password"]', password);
|
||||
await page.click('button[type="submit"]');
|
||||
await page.waitForURL((url) => !url.pathname.includes("/login"), { timeout: 10000 });
|
||||
// Set language again after navigation to new page
|
||||
await setEnglishLanguage(page);
|
||||
}
|
||||
|
|
|
|||
22
frontend/e2e/helpers/backend-url.ts
Normal file
22
frontend/e2e/helpers/backend-url.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/**
|
||||
* Helper to determine which backend URL to use based on Playwright worker index.
|
||||
* Each worker gets its own backend instance and database.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get the backend URL for the current Playwright worker.
|
||||
* Uses NEXT_PUBLIC_API_URL which is set in setup.ts based on worker index.
|
||||
* Falls back to environment variable or default port 8001.
|
||||
*/
|
||||
export function getBackendUrl(): string {
|
||||
// NEXT_PUBLIC_API_URL is set in setup.ts based on worker index
|
||||
return process.env.NEXT_PUBLIC_API_URL || "http://localhost:8001";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the API URL for the current worker.
|
||||
* This is the same as getBackendUrl but with a clearer name.
|
||||
*/
|
||||
export function getApiUrl(): string {
|
||||
return getBackendUrl();
|
||||
}
|
||||
21
frontend/e2e/helpers/reset-db.ts
Normal file
21
frontend/e2e/helpers/reset-db.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
/**
|
||||
* Helper to reset the database before each test.
|
||||
* Calls the /api/test/reset endpoint on the worker's backend.
|
||||
*/
|
||||
|
||||
import { APIRequestContext } from "@playwright/test";
|
||||
import { getBackendUrl } from "./backend-url";
|
||||
|
||||
/**
|
||||
* Reset the database for the current worker.
|
||||
* Truncates all tables and re-seeds base data.
|
||||
*/
|
||||
export async function resetDatabase(request: APIRequestContext): Promise<void> {
|
||||
const backendUrl = getBackendUrl();
|
||||
const response = await request.post(`${backendUrl}/api/test/reset`);
|
||||
|
||||
if (!response.ok()) {
|
||||
const text = await response.text();
|
||||
throw new Error(`Failed to reset database: ${response.status()} - ${text}`);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +1,35 @@
|
|||
import { test } from "@playwright/test";
|
||||
import { resetDatabase } from "./reset-db";
|
||||
|
||||
/**
|
||||
* Set language to English for e2e tests.
|
||||
* E2E tests should only test in English according to requirements.
|
||||
* This is applied globally via test.beforeEach in the setup file.
|
||||
*
|
||||
* Also sets NEXT_PUBLIC_API_URL based on worker index so each worker
|
||||
* connects to its own backend instance, and resets the database before
|
||||
* each test for isolation.
|
||||
*/
|
||||
test.beforeEach(async ({ context }) => {
|
||||
test.beforeEach(async ({ context, request }, testInfo) => {
|
||||
// Set API URL based on worker index
|
||||
// Each worker gets its own backend (port 8001 + workerIndex)
|
||||
const workerIndex = testInfo.workerIndex;
|
||||
const basePort = parseInt(process.env.E2E_PORT_START || "8001", 10);
|
||||
const backendPort = basePort + workerIndex;
|
||||
const backendUrl = `http://localhost:${backendPort}`;
|
||||
|
||||
// Set environment variable for this test run
|
||||
process.env.NEXT_PUBLIC_API_URL = backendUrl;
|
||||
|
||||
// Reset database before each test for isolation
|
||||
try {
|
||||
await resetDatabase(request);
|
||||
} catch (error) {
|
||||
// If reset fails, log but don't fail the test
|
||||
// This allows tests to run even if reset endpoint is unavailable
|
||||
console.warn(`Failed to reset database: ${error}`);
|
||||
}
|
||||
|
||||
// Add init script to set English language before any page loads
|
||||
// This must be called before any page.goto() calls
|
||||
await context.addInitScript(() => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue