test: fix leaky newPage calls (#11868)

This commit is contained in:
Alex Rudenko 2024-02-07 12:15:36 +01:00 committed by GitHub
parent 7d1541de68
commit 4227be34c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 31 additions and 24 deletions

View File

@ -30,7 +30,7 @@ describe('Target.createCDPSession', function () {
}); });
it('should not report created targets for custom CDP sessions', async () => { it('should not report created targets for custom CDP sessions', async () => {
const {browser} = await getTestState(); const {context} = await getTestState();
let called = 0; let called = 0;
const handler = async (target: Target) => { const handler = async (target: Target) => {
called++; called++;
@ -39,9 +39,9 @@ describe('Target.createCDPSession', function () {
} }
await target.createCDPSession(); await target.createCDPSession();
}; };
browser.browserContexts()[0]!.on('targetcreated', handler); context.on('targetcreated', handler);
await browser.newPage(); await context.newPage();
browser.browserContexts()[0]!.off('targetcreated', handler); context.off('targetcreated', handler);
}); });
it('should send events', async () => { it('should send events', async () => {

View File

@ -64,23 +64,30 @@ const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-');
const headfulBrowser = await launchBrowser( const headfulBrowser = await launchBrowser(
Object.assign({userDataDir}, headfulOptions) Object.assign({userDataDir}, headfulOptions)
); );
try {
const headfulPage = await headfulBrowser.newPage(); const headfulPage = await headfulBrowser.newPage();
await headfulPage.goto(server.EMPTY_PAGE); await headfulPage.goto(server.EMPTY_PAGE);
await headfulPage.evaluate(() => { await headfulPage.evaluate(() => {
return (document.cookie = return (document.cookie =
'foo=true; expires=Fri, 31 Dec 9999 23:59:59 GMT'); 'foo=true; expires=Fri, 31 Dec 9999 23:59:59 GMT');
}); });
} finally {
await headfulBrowser.close(); await headfulBrowser.close();
}
// Read the cookie from headless chrome // Read the cookie from headless chrome
const headlessBrowser = await launchBrowser( const headlessBrowser = await launchBrowser(
Object.assign({userDataDir}, headlessOptions) Object.assign({userDataDir}, headlessOptions)
); );
let cookie = '';
try {
const headlessPage = await headlessBrowser.newPage(); const headlessPage = await headlessBrowser.newPage();
await headlessPage.goto(server.EMPTY_PAGE); await headlessPage.goto(server.EMPTY_PAGE);
const cookie = await headlessPage.evaluate(() => { cookie = await headlessPage.evaluate(() => {
return document.cookie; return document.cookie;
}); });
} finally {
await headlessBrowser.close(); await headlessBrowser.close();
}
// This might throw. See https://github.com/puppeteer/puppeteer/issues/2778 // This might throw. See https://github.com/puppeteer/puppeteer/issues/2778
try { try {
rmSync(userDataDir); rmSync(userDataDir);

View File

@ -42,9 +42,9 @@ describe('Page', function () {
expect(error.message).toContain('Protocol error'); expect(error.message).toContain('Protocol error');
}); });
it('should not be visible in browser.pages', async () => { it('should not be visible in browser.pages', async () => {
const {browser} = await getTestState(); const {browser, context} = await getTestState();
const newPage = await browser.newPage(); const newPage = await context.newPage();
expect(await browser.pages()).toContain(newPage); expect(await browser.pages()).toContain(newPage);
await newPage.close(); await newPage.close();
expect(await browser.pages()).not.toContain(newPage); expect(await browser.pages()).not.toContain(newPage);
@ -2239,9 +2239,9 @@ describe('Page', function () {
describe('Page.bringToFront', function () { describe('Page.bringToFront', function () {
it('should work', async () => { it('should work', async () => {
const {browser} = await getTestState(); const {context} = await getTestState();
const page1 = await browser.newPage(); const page1 = await context.newPage();
const page2 = await browser.newPage(); const page2 = await context.newPage();
await page1.bringToFront(); await page1.bringToFront();
expect( expect(

View File

@ -285,7 +285,7 @@ describe('Target', function () {
describe('Browser.waitForTarget', () => { describe('Browser.waitForTarget', () => {
it('should wait for a target', async () => { it('should wait for a target', async () => {
const {browser, server} = await getTestState(); const {browser, server, context} = await getTestState();
let resolved = false; let resolved = false;
const targetPromise = browser.waitForTarget( const targetPromise = browser.waitForTarget(
@ -306,7 +306,7 @@ describe('Target', function () {
throw error; throw error;
} }
}); });
const page = await browser.newPage(); const page = await context.newPage();
expect(resolved).toBe(false); expect(resolved).toBe(false);
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
try { try {