* refactor: use patchwright * refactor: migrate setRequestInterception and waitUntil * fix: overwrite ua * refactor: drop puppeteer cookie/requestfinished shims Migrate page.cookies/setCookie and browser.cookies/setCookie call sites to the native context APIs (page.context().cookies/addCookies), rewrite requestfinished handlers to await request.response() instead of consuming the synchronous puppeteer-shaped payload, and remove the now-redundant browser.close context-cleanup wrapper. Playwright already handles all of these natively; the shims existed only for the puppeteer -> playwright migration. * refactor: remove page.authenticate, page.setUserAgent, browser.newPage(), browser.userAgent() shims update cookie utility tests * refactor: replace browser instance with context in Playwright usage across multiple routes - Updated all instances of `browser` to `context` in Playwright-related functions for consistency and improved resource management. - Ensured proper closure of context after operations to prevent memory leaks. - Adjusted request interception methods to utilize the new context variable. * fix: direct type import * refactor: remove Puppeteer compatibility layers and unify page handling with Playwright * fix: test case * chore: bump patchright to v1.60.0 * chore: pin patchright to 1.59.1 nft can't resolve browser.json due to https://github.com/microsoft/playwright/pull/40093 * chore: pin to node 24.15 for playwright browser install on node 24.16 stucks due to https://github.com/microsoft/playwright/issues/40724 * fix: update patchright and patchright-core to version 1.60.2 and adjust minify-docker.js for compatibility * fix: import BrowserContext from patchright
69 lines
2.1 KiB
TypeScript
69 lines
2.1 KiB
TypeScript
import type { BrowserContext } from 'patchright';
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import wait from './wait';
|
|
|
|
let context: BrowserContext | null = null;
|
|
|
|
afterEach(async () => {
|
|
if (context) {
|
|
await context.close();
|
|
context = null;
|
|
}
|
|
|
|
delete process.env.PROXY_URI;
|
|
delete process.env.PROXY_PROTOCOL;
|
|
delete process.env.PROXY_HOST;
|
|
delete process.env.PROXY_PORT;
|
|
delete process.env.PROXY_AUTH;
|
|
delete process.env.PROXY_URL_REGEX;
|
|
|
|
vi.resetModules();
|
|
});
|
|
|
|
describe('playwright', () => {
|
|
it('playwright run', async () => {
|
|
const { default: playwright } = await import('./playwright');
|
|
context = await playwright();
|
|
const browser = context.browser();
|
|
const startTime = Date.now();
|
|
const page = await context.newPage();
|
|
await page.goto('https://www.google.com', {
|
|
waitUntil: 'domcontentloaded',
|
|
});
|
|
|
|
const html = await page.evaluate(() => document.body.innerHTML);
|
|
expect(html.length).toBeGreaterThan(0);
|
|
|
|
expect(browser?.isConnected()).toBe(true);
|
|
const sleepTime = 31 * 1000 - (Date.now() - startTime);
|
|
if (sleepTime > 0) {
|
|
await wait(sleepTime);
|
|
}
|
|
expect(browser?.isConnected()).toBe(false);
|
|
context = null;
|
|
}, 45000);
|
|
});
|
|
|
|
describe('getPlaywrightPage', () => {
|
|
it('playwright run', async () => {
|
|
const { getPlaywrightPage } = await import('./playwright');
|
|
const playwright = await getPlaywrightPage('https://www.google.com');
|
|
const page = playwright.page;
|
|
context = playwright.context;
|
|
const browser = context.browser();
|
|
const startTime = Date.now();
|
|
|
|
const html = await page.evaluate(() => document.body.innerHTML);
|
|
expect(html.length).toBeGreaterThan(0);
|
|
|
|
expect(browser?.isConnected()).toBe(true);
|
|
const sleepTime = 31 * 1000 - (Date.now() - startTime);
|
|
if (sleepTime > 0) {
|
|
await wait(sleepTime);
|
|
}
|
|
expect(browser?.isConnected()).toBe(false);
|
|
context = null;
|
|
}, 45000);
|
|
});
|