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 () => {
const {browser} = await getTestState();
const {context} = await getTestState();
let called = 0;
const handler = async (target: Target) => {
called++;
@ -39,9 +39,9 @@ describe('Target.createCDPSession', function () {
}
await target.createCDPSession();
};
browser.browserContexts()[0]!.on('targetcreated', handler);
await browser.newPage();
browser.browserContexts()[0]!.off('targetcreated', handler);
context.on('targetcreated', handler);
await context.newPage();
context.off('targetcreated', handler);
});
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(
Object.assign({userDataDir}, headfulOptions)
);
try {
const headfulPage = await headfulBrowser.newPage();
await headfulPage.goto(server.EMPTY_PAGE);
await headfulPage.evaluate(() => {
return (document.cookie =
'foo=true; expires=Fri, 31 Dec 9999 23:59:59 GMT');
});
} finally {
await headfulBrowser.close();
}
// Read the cookie from headless chrome
const headlessBrowser = await launchBrowser(
Object.assign({userDataDir}, headlessOptions)
);
let cookie = '';
try {
const headlessPage = await headlessBrowser.newPage();
await headlessPage.goto(server.EMPTY_PAGE);
const cookie = await headlessPage.evaluate(() => {
cookie = await headlessPage.evaluate(() => {
return document.cookie;
});
} finally {
await headlessBrowser.close();
}
// This might throw. See https://github.com/puppeteer/puppeteer/issues/2778
try {
rmSync(userDataDir);

View File

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

View File

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