Playwright 似乎默认处于隐身模式,我想禁用此功能,以便在浏览不同网页时可以让 cookie 保持一致。
我已经翻阅了整个 Playwright 文档,他们指出新的浏览器上下文默认为隐身模式,但没有提供禁用此选项的方法。
您可以使用 launchPersistentContext 在正常模式下创建新上下文
这是一个打开非隐身 chromium 窗口的示例测试。
import { test, expect, BrowserContext, Page, chromium } from '@playwright/test';
test('test', async () => {
const browser: BrowserContext = await chromium.launchPersistentContext('', { headless: false, channel: 'chrome' });
// remove extra tab in browser window hack
const pages: Page[] = browser.pages();
const page: Page = pages[0];
await page.goto('https://www.example.com');
await expect(page.locator('div').filter({ hasText: 'example' })).toBeVisible();
});
我引用了这个视频:https://www.youtube.com/watch?v=-5uIFaBx738 它比 RoseQuartz 引用的 Playwright 文档更深入地解释了一些细节。