Files
claude-code-viewer/e2e/utils/withPlaywright.ts
d-kimsuon 1e62eeb856 feat(e2e): enhance end-to-end testing setup with new scripts and capture cases
- Updated package.json to include new E2E scripts for execution and snapshot capturing.
- Added new capture cases for the "new-project-modal" and "start-new-chat" functionalities in the E2E tests.
- Increased wait times in session detail captures to ensure elements are fully loaded before interactions.
- Introduced new shell scripts for starting the server and capturing snapshots, improving the E2E testing workflow.
- Updated NewChatModal and SessionsTab components to include data-testid attributes for better test targeting.
2025-10-18 17:48:15 +09:00

74 lines
1.5 KiB
TypeScript

import {
type Browser,
type BrowserContext,
type BrowserContextOptions,
type BrowserType,
chromium,
type LaunchOptions,
} from "playwright";
import prexit from "prexit";
type PlaywrightContext = {
context: BrowserContext;
cleanUp: () => Promise<void>;
};
type BrowserOptions = {
browserType: BrowserType;
contextOptions: BrowserContextOptions;
launchOptions: LaunchOptions;
};
const useBrowser = (options: BrowserOptions) => {
const { contextOptions, launchOptions, browserType } = options ?? {};
let browser: Browser | null = null;
let context: BrowserContext | null = null;
return async () => {
browser ??= await browserType.launch({
headless: true,
...launchOptions,
});
context ??= await browser.newContext({
...contextOptions,
});
return {
browser,
context,
};
};
};
export const withPlaywright = async <T>(
cb: (ctx: PlaywrightContext) => Promise<T>,
options?: Partial<BrowserOptions>,
) => {
const {
browserType = chromium,
contextOptions = {},
launchOptions = {},
} = options ?? {};
const { browser, context } = await useBrowser({
browserType,
contextOptions,
launchOptions,
})();
let isClosed = false;
const cleanUp = async () => {
await Promise.all(context.pages().map((page) => page.close()));
await context.close();
await browser.close();
isClosed = true;
};
prexit(async () => {
if (isClosed) return;
await cleanUp();
});
return cb({ context, cleanUp });
};