chore: use launch helper (#10344)

This commit is contained in:
Nikolay Vitkov 2023-06-12 10:44:18 +02:00 committed by GitHub
parent d89072a368
commit e3e68a99d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 824 additions and 682 deletions

View File

@ -15,7 +15,7 @@ module.exports = {
files: ['*.spec.ts'],
rules: {
'no-restricted-syntax': [
'warn',
'error',
{
message:
'Use helper command `launch` to make sure the browsers get cleaned',

View File

@ -401,6 +401,12 @@
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[launcher.spec] Launcher specs Puppeteer Puppeteer.launch should be able to launch Firefox",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["chrome"],
"expectations": ["SKIP"]
},
{
"testIdPattern": "[launcher.spec] Launcher specs Puppeteer Puppeteer.launch should work with no default arguments",
"platforms": ["darwin", "linux", "win32"],
@ -1145,12 +1151,6 @@
"parameters": ["cdp", "firefox"],
"expectations": ["SKIP"]
},
{
"testIdPattern": "[launcher.spec] Launcher specs Puppeteer Puppeteer.launch should be able to launch Firefox",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["cdp", "chrome"],
"expectations": ["FAIL"]
},
{
"testIdPattern": "[launcher.spec] Launcher specs Puppeteer Puppeteer.launch should be able to launch Firefox",
"platforms": ["darwin", "linux", "win32"],

View File

@ -15,46 +15,39 @@
*/
import expect from 'expect';
import {
CDPBrowser,
CDPBrowserContext,
} from 'puppeteer-core/internal/common/Browser.js';
import {CDPBrowser} from 'puppeteer-core/internal/common/Browser.js';
import {getTestState} from './mocha-utils'; // eslint-disable-line import/extensions
import {getTestState, launch} from './mocha-utils.js';
import {attachFrame} from './utils.js';
describe('TargetManager', () => {
/* We use a special browser for this test as we need the --site-per-process flag */
let browser: CDPBrowser;
let context: CDPBrowserContext;
let testState: Awaited<ReturnType<typeof launch>> & {
browser: CDPBrowser;
};
before(async () => {
const {puppeteer, defaultBrowserOptions} = getTestState();
browser = (await puppeteer.launch(
beforeEach(async () => {
const {defaultBrowserOptions} = getTestState();
testState = (await launch(
Object.assign({}, defaultBrowserOptions, {
args: (defaultBrowserOptions.args || []).concat([
'--site-per-process',
'--remote-debugging-port=21222',
'--host-rules=MAP * 127.0.0.1',
]),
})
)) as CDPBrowser;
});
beforeEach(async () => {
context = await browser.createIncognitoBrowserContext();
}),
{createPage: false}
)) as Awaited<ReturnType<typeof launch>> & {
browser: CDPBrowser;
};
});
afterEach(async () => {
await context.close();
});
after(async () => {
await browser.close();
await testState.close();
});
it('should handle targets', async () => {
const {server} = getTestState();
const {server, context, browser} = testState;
const targetManager = browser._targetManager();
expect(targetManager.getAvailableTargets().size).toBe(2);

View File

@ -19,6 +19,7 @@ import expect from 'expect';
import {
getTestState,
launch,
setupTestBrowserHooks,
setupTestPageAndContextHooks,
} from './mocha-utils.js';
@ -29,111 +30,129 @@ describe('Chromium-Specific Launcher tests', function () {
it('should be able to connect using browserUrl, with and without trailing slash', async () => {
const {defaultBrowserOptions, puppeteer} = getTestState();
const originalBrowser = await puppeteer.launch(
const {close} = await launch(
Object.assign({}, defaultBrowserOptions, {
args: ['--remote-debugging-port=21222'],
})
);
const browserURL = 'http://127.0.0.1:21222';
try {
const browserURL = 'http://127.0.0.1:21222';
const browser1 = await puppeteer.connect({browserURL});
const page1 = await browser1.newPage();
expect(
await page1.evaluate(() => {
return 7 * 8;
})
).toBe(56);
browser1.disconnect();
const browser1 = await puppeteer.connect({browserURL});
const page1 = await browser1.newPage();
expect(
await page1.evaluate(() => {
return 7 * 8;
})
).toBe(56);
browser1.disconnect();
const browser2 = await puppeteer.connect({
browserURL: browserURL + '/',
});
const page2 = await browser2.newPage();
expect(
await page2.evaluate(() => {
return 8 * 7;
})
).toBe(56);
browser2.disconnect();
await originalBrowser.close();
const browser2 = await puppeteer.connect({
browserURL: browserURL + '/',
});
const page2 = await browser2.newPage();
expect(
await page2.evaluate(() => {
return 8 * 7;
})
).toBe(56);
browser2.disconnect();
} finally {
await close();
}
});
it('should throw when using both browserWSEndpoint and browserURL', async () => {
const {defaultBrowserOptions, puppeteer} = getTestState();
const originalBrowser = await puppeteer.launch(
const {browser, close} = await launch(
Object.assign({}, defaultBrowserOptions, {
args: ['--remote-debugging-port=21222'],
})
);
const browserURL = 'http://127.0.0.1:21222';
try {
const browserURL = 'http://127.0.0.1:21222';
let error!: Error;
await puppeteer
.connect({
browserURL,
browserWSEndpoint: originalBrowser.wsEndpoint(),
})
.catch(error_ => {
return (error = error_);
});
expect(error.message).toContain(
'Exactly one of browserWSEndpoint, browserURL or transport'
);
await originalBrowser.close();
let error!: Error;
await puppeteer
.connect({
browserURL,
browserWSEndpoint: browser.wsEndpoint(),
})
.catch(error_ => {
return (error = error_);
});
expect(error.message).toContain(
'Exactly one of browserWSEndpoint, browserURL or transport'
);
} finally {
await close();
}
});
it('should throw when trying to connect to non-existing browser', async () => {
const {defaultBrowserOptions, puppeteer} = getTestState();
const originalBrowser = await puppeteer.launch(
const {close} = await launch(
Object.assign({}, defaultBrowserOptions, {
args: ['--remote-debugging-port=21222'],
})
);
const browserURL = 'http://127.0.0.1:32333';
try {
const browserURL = 'http://127.0.0.1:32333';
let error!: Error;
await puppeteer.connect({browserURL}).catch(error_ => {
return (error = error_);
});
expect(error.message).toContain(
'Failed to fetch browser webSocket URL from'
);
await originalBrowser.close();
let error!: Error;
await puppeteer.connect({browserURL}).catch(error_ => {
return (error = error_);
});
expect(error.message).toContain(
'Failed to fetch browser webSocket URL from'
);
} finally {
await close();
}
});
});
describe('Puppeteer.launch |pipe| option', function () {
it('should support the pipe option', async () => {
const {defaultBrowserOptions, puppeteer} = getTestState();
const {defaultBrowserOptions} = getTestState();
const options = Object.assign({pipe: true}, defaultBrowserOptions);
const browser = await puppeteer.launch(options);
expect(await browser.pages()).toHaveLength(1);
expect(browser.wsEndpoint()).toBe('');
const page = await browser.newPage();
expect(await page.evaluate('11 * 11')).toBe(121);
await page.close();
await browser.close();
const {browser, close} = await launch(options, {createPage: false});
try {
expect(await browser.pages()).toHaveLength(1);
expect(browser.wsEndpoint()).toBe('');
const page = await browser.newPage();
expect(await page.evaluate('11 * 11')).toBe(121);
await page.close();
} finally {
await close();
}
});
it('should support the pipe argument', async () => {
const {defaultBrowserOptions, puppeteer} = getTestState();
const {defaultBrowserOptions} = getTestState();
const options = Object.assign({}, defaultBrowserOptions);
options.args = ['--remote-debugging-pipe'].concat(options.args || []);
const browser = await puppeteer.launch(options);
expect(browser.wsEndpoint()).toBe('');
const page = await browser.newPage();
expect(await page.evaluate('11 * 11')).toBe(121);
await page.close();
await browser.close();
const {browser, close} = await launch(options);
try {
expect(browser.wsEndpoint()).toBe('');
const page = await browser.newPage();
expect(await page.evaluate('11 * 11')).toBe(121);
await page.close();
} finally {
await close();
}
});
it('should fire "disconnected" when closing with pipe', async () => {
const {defaultBrowserOptions, puppeteer} = getTestState();
const {defaultBrowserOptions} = getTestState();
const options = Object.assign({pipe: true}, defaultBrowserOptions);
const browser = await puppeteer.launch(options);
const disconnectedEventPromise = waitEvent(browser, 'disconnected');
// Emulate user exiting browser.
browser.process()!.kill();
await disconnectedEventPromise;
const {browser, close} = await launch(options);
try {
const disconnectedEventPromise = waitEvent(browser, 'disconnected');
// Emulate user exiting browser.
browser.process()!.kill();
await disconnectedEventPromise;
} finally {
await close();
}
});
});
});

View File

@ -20,6 +20,7 @@ import {
getTestState,
setupTestBrowserHooks,
setupTestPageAndContextHooks,
launch,
} from './mocha-utils.js';
describe('Cookie specs', () => {
@ -480,16 +481,15 @@ describe('Cookie specs', () => {
]);
});
it('should set secure same-site cookies from a frame', async () => {
const {httpsServer, puppeteer, defaultBrowserOptions} = getTestState();
const {httpsServer, defaultBrowserOptions} = getTestState();
const browser = await puppeteer.launch({
const {browser, close} = await launch({
...defaultBrowserOptions,
ignoreHTTPSErrors: true,
});
const page = await browser.newPage();
try {
const page = await browser.newPage();
await page.goto(httpsServer.PREFIX + '/grid.html');
await page.evaluate(src => {
let fulfill!: () => void;
@ -533,8 +533,7 @@ describe('Cookie specs', () => {
]
);
} finally {
await page.close();
await browser.close();
await close();
}
});
});

View File

@ -19,13 +19,10 @@ import os from 'os';
import path from 'path';
import expect from 'expect';
import {
PuppeteerLaunchOptions,
PuppeteerNode,
} from 'puppeteer-core/internal/node/PuppeteerNode.js';
import {PuppeteerLaunchOptions} from 'puppeteer-core/internal/node/PuppeteerNode.js';
import {rmSync} from 'puppeteer-core/internal/node/util/fs.js';
import {getTestState} from './mocha-utils.js';
import {getTestState, launch} from './mocha-utils.js';
const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-');
@ -59,7 +56,7 @@ describe('headful tests', function () {
headless: boolean;
devtools: boolean;
};
const browsers: any[] = [];
const browsers: Array<() => Promise<void>> = [];
beforeEach(() => {
const {server, defaultBrowserOptions} = getTestState();
@ -96,29 +93,24 @@ describe('headful tests', function () {
});
});
async function launchBrowser(puppeteer: PuppeteerNode, options: any) {
const browser = await puppeteer.launch(options);
browsers.push(browser);
async function launchBrowser(options: any) {
const {browser, close} = await launch(options, {createContext: false});
browsers.push(close);
return browser;
}
afterEach(() => {
for (const i in browsers) {
const browser = browsers[i];
if (browser.isConnected()) {
browser.close();
}
delete browsers[i];
}
afterEach(async () => {
await Promise.all(
browsers.map((close, index) => {
delete browsers[index];
return close();
})
);
});
describe('HEADFUL', function () {
it('background_page target type should be available', async () => {
const {puppeteer} = getTestState();
const browserWithExtension = await launchBrowser(
puppeteer,
extensionOptions
);
const browserWithExtension = await launchBrowser(extensionOptions);
const page = await browserWithExtension.newPage();
const backgroundPageTarget = await browserWithExtension.waitForTarget(
target => {
@ -130,8 +122,8 @@ describe('headful tests', function () {
expect(backgroundPageTarget).toBeTruthy();
});
it('service_worker target type should be available', async () => {
const {puppeteer, defaultBrowserOptions} = getTestState();
const browserWithExtension = await launchBrowser(puppeteer, {
const {defaultBrowserOptions} = getTestState();
const browserWithExtension = await launchBrowser({
...defaultBrowserOptions,
headless: false,
args: [
@ -150,11 +142,7 @@ describe('headful tests', function () {
expect(serviceWorkerTarget).toBeTruthy();
});
it('target.page() should return a background_page', async function () {
const {puppeteer} = getTestState();
const browserWithExtension = await launchBrowser(
puppeteer,
extensionOptions
);
const browserWithExtension = await launchBrowser(extensionOptions);
const backgroundPageTarget = await browserWithExtension.waitForTarget(
target => {
return target.type() === 'background_page';
@ -175,7 +163,7 @@ describe('headful tests', function () {
});
it('target.page() should return a DevTools page if custom isPageTarget is provided', async function () {
const {puppeteer} = getTestState();
const originalBrowser = await launchBrowser(puppeteer, devtoolsOptions);
const originalBrowser = await launchBrowser(devtoolsOptions);
const browserWSEndpoint = originalBrowser.wsEndpoint();
@ -197,25 +185,21 @@ describe('headful tests', function () {
})
).toBe(6);
expect(await browser.pages()).toContainEqual(page);
await browser.close();
});
it('should have default url when launching browser', async function () {
const {puppeteer} = getTestState();
const browser = await launchBrowser(puppeteer, extensionOptions);
const browser = await launchBrowser(extensionOptions);
const pages = (await browser.pages()).map((page: {url: () => any}) => {
return page.url();
});
expect(pages).toEqual(['about:blank']);
await browser.close();
});
it('headless should be able to read cookies written by headful', async () => {
/* Needs investigation into why but this fails consistently on Windows CI. */
const {server, puppeteer} = getTestState();
const {server} = getTestState();
const userDataDir = await mkdtemp(TMP_FOLDER);
// Write a cookie in headful chrome
const headfulBrowser = await launchBrowser(
puppeteer,
Object.assign({userDataDir}, headfulOptions)
);
const headfulPage = await headfulBrowser.newPage();
@ -227,7 +211,6 @@ describe('headful tests', function () {
await headfulBrowser.close();
// Read the cookie from headless chrome
const headlessBrowser = await launchBrowser(
puppeteer,
Object.assign({userDataDir}, headlessOptions)
);
const headlessPage = await headlessBrowser.newPage();
@ -244,10 +227,10 @@ describe('headful tests', function () {
});
// TODO: Support OOOPIF. @see https://github.com/puppeteer/puppeteer/issues/2548
it.skip('OOPIF: should report google.com frame', async () => {
const {server, puppeteer} = getTestState();
const {server} = getTestState();
// https://google.com is isolated by default in Chromium embedder.
const browser = await launchBrowser(puppeteer, headfulOptions);
const browser = await launchBrowser(headfulOptions);
const page = await browser.newPage();
await page.goto(server.EMPTY_PAGE);
await page.setRequestInterception(true);
@ -270,12 +253,11 @@ describe('headful tests', function () {
})
.sort();
expect(urls).toEqual([server.EMPTY_PAGE, 'https://google.com/']);
await browser.close();
});
it('OOPIF: should expose events within OOPIFs', async () => {
const {server, puppeteer} = getTestState();
const {server} = getTestState();
const browser = await launchBrowser(puppeteer, forcedOopifOptions);
const browser = await launchBrowser(forcedOopifOptions);
const page = await browser.newPage();
// Setup our session listeners to observe OOPIF activity.
@ -333,9 +315,9 @@ describe('headful tests', function () {
expect(requests).toContain(`http://oopifdomain:${server.PORT}/fetch`);
});
it('should close browser with beforeunload page', async () => {
const {server, puppeteer} = getTestState();
const {server} = getTestState();
const browser = await launchBrowser(puppeteer, headfulOptions);
const browser = await launchBrowser(headfulOptions);
const page = await browser.newPage();
await page.goto(server.PREFIX + '/beforeunload.html');
// We have to interact with a page so that 'beforeunload' handlers
@ -344,10 +326,7 @@ describe('headful tests', function () {
await browser.close();
});
it('should open devtools when "devtools: true" option is given', async () => {
const {puppeteer} = getTestState();
const browser = await launchBrowser(
puppeteer,
Object.assign({devtools: true}, headfulOptions)
);
const context = await browser.createIncognitoBrowserContext();
@ -363,8 +342,7 @@ describe('headful tests', function () {
describe('Page.bringToFront', function () {
it('should work', async () => {
const {puppeteer} = getTestState();
const browser = await launchBrowser(puppeteer, headfulOptions);
const browser = await launchBrowser(headfulOptions);
const page1 = await browser.newPage();
const page2 = await browser.newPage();
@ -400,8 +378,8 @@ describe('headful tests', function () {
describe('Page.screenshot', function () {
it('should run in parallel in multiple pages', async () => {
const {server, puppeteer} = getTestState();
const browser = await puppeteer.launch(headfulOptions);
const {server} = getTestState();
const browser = await launchBrowser(headfulOptions);
const context = await browser.createIncognitoBrowserContext();
const N = 2;

View File

@ -39,6 +39,7 @@ describe('ignoreHTTPSErrors', function () {
{ignoreHTTPSErrors: true},
defaultBrowserOptions
);
// eslint-disable-next-line no-restricted-syntax
browser = await puppeteer.launch(options);
});

File diff suppressed because it is too large Load Diff

View File

@ -364,47 +364,69 @@ export const createTimeout = <T>(
});
};
const browserCleanups: Array<() => Promise<void>> = [];
let browserCleanups: Array<() => Promise<void>> = [];
export const launch = async (
options: PuppeteerLaunchOptions
launchOptions: PuppeteerLaunchOptions,
options: {
createContext?: boolean;
createPage?: boolean;
} = {}
): Promise<
PuppeteerTestState & {
close: () => Promise<void>;
}
> => {
const {createContext = true, createPage = true} = options;
const close = async () => {
let cleanup = browserCleanups.pop();
while (cleanup) {
await cleanup();
cleanup = browserCleanups.pop();
try {
while (cleanup) {
await cleanup();
cleanup = browserCleanups.pop();
}
} catch (error) {
// If the browser was closed by other mean swallow the error
// and mark he browser as closed
if ((error as any)?.message.includes('Connection closed')) {
browserCleanups = [];
return;
}
throw error;
}
};
try {
const browser = await puppeteer.launch({
...defaultBrowserOptions,
...options,
...launchOptions,
});
browserCleanups.push(() => {
return browser.close();
});
const context = await browser.createIncognitoBrowserContext();
browserCleanups.push(() => {
return context.close();
});
let context: BrowserContext;
let page: Page;
if (createContext) {
context = await browser.createIncognitoBrowserContext();
browserCleanups.push(() => {
return context.close();
});
const page = await context.newPage();
browserCleanups.push(() => {
return page.close();
});
if (createPage) {
page = await context.newPage();
browserCleanups.push(() => {
return page.close();
});
}
}
return {
...getTestState(),
browser,
context,
page,
context: context!,
page: page!,
close,
};
} catch (error) {

View File

@ -30,6 +30,7 @@ describeWithDebugLogs('OOPIF', function () {
before(async () => {
const {puppeteer, defaultBrowserOptions} = getTestState();
// eslint-disable-next-line no-restricted-syntax
browser = await puppeteer.launch(
Object.assign({}, defaultBrowserOptions, {
args: (defaultBrowserOptions.args || []).concat([

View File

@ -21,9 +21,8 @@ import os from 'os';
import {TestServer} from '@pptr/testserver';
import expect from 'expect';
import type {Browser} from 'puppeteer-core/internal/api/Browser.js';
import {getTestState} from './mocha-utils.js';
import {getTestState, launch} from './mocha-utils.js';
let HOSTNAME = os.hostname();
@ -52,12 +51,13 @@ function getEmptyPageUrl(server: TestServer): string {
}
describe('request proxy', () => {
let browser: Browser;
let proxiedRequestUrls: string[];
let proxyServer: Server;
let proxyServerUrl: string;
const defaultArgs = [
'--disable-features=NetworkTimeServiceQuerying', // We disable this in tests so that proxy-related tests don't intercept queries from this service in headful.
// We disable this in tests so that proxy-related tests
// don't intercept queries from this service in headful.
'--disable-features=NetworkTimeServiceQuerying',
];
beforeEach(() => {
@ -97,8 +97,6 @@ describe('request proxy', () => {
});
afterEach(async () => {
await browser.close();
await new Promise((resolve, reject) => {
proxyServer.close(error => {
if (error) {
@ -111,27 +109,29 @@ describe('request proxy', () => {
});
it('should proxy requests when configured', async () => {
const {puppeteer, defaultBrowserOptions, server} = getTestState();
const {defaultBrowserOptions, server} = getTestState();
const emptyPageUrl = getEmptyPageUrl(server);
browser = await puppeteer.launch({
const {browser, close} = await launch({
...defaultBrowserOptions,
args: [...defaultArgs, `--proxy-server=${proxyServerUrl}`],
});
try {
const page = await browser.newPage();
const response = (await page.goto(emptyPageUrl))!;
const page = await browser.newPage();
const response = (await page.goto(emptyPageUrl))!;
expect(response.ok()).toBe(true);
expect(proxiedRequestUrls).toEqual([emptyPageUrl]);
expect(response.ok()).toBe(true);
expect(proxiedRequestUrls).toEqual([emptyPageUrl]);
} finally {
await close();
}
});
it('should respect proxy bypass list', async () => {
const {puppeteer, defaultBrowserOptions, server} = getTestState();
const {defaultBrowserOptions, server} = getTestState();
const emptyPageUrl = getEmptyPageUrl(server);
browser = await puppeteer.launch({
const {browser, close} = await launch({
...defaultBrowserOptions,
args: [
...defaultArgs,
@ -139,39 +139,43 @@ describe('request proxy', () => {
`--proxy-bypass-list=${new URL(emptyPageUrl).host}`,
],
});
try {
const page = await browser.newPage();
const response = (await page.goto(emptyPageUrl))!;
const page = await browser.newPage();
const response = (await page.goto(emptyPageUrl))!;
expect(response.ok()).toBe(true);
expect(proxiedRequestUrls).toEqual([]);
expect(response.ok()).toBe(true);
expect(proxiedRequestUrls).toEqual([]);
} finally {
await close();
}
});
describe('in incognito browser context', () => {
it('should proxy requests when configured at browser level', async () => {
const {puppeteer, defaultBrowserOptions, server} = getTestState();
const {defaultBrowserOptions, server} = getTestState();
const emptyPageUrl = getEmptyPageUrl(server);
browser = await puppeteer.launch({
const {browser, close} = await launch({
...defaultBrowserOptions,
args: [...defaultArgs, `--proxy-server=${proxyServerUrl}`],
});
try {
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
const response = (await page.goto(emptyPageUrl))!;
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
const response = (await page.goto(emptyPageUrl))!;
expect(response.ok()).toBe(true);
expect(proxiedRequestUrls).toEqual([emptyPageUrl]);
expect(response.ok()).toBe(true);
expect(proxiedRequestUrls).toEqual([emptyPageUrl]);
} finally {
await close();
}
});
it('should respect proxy bypass list when configured at browser level', async () => {
const {puppeteer, defaultBrowserOptions, server} = getTestState();
const {defaultBrowserOptions, server} = getTestState();
const emptyPageUrl = getEmptyPageUrl(server);
browser = await puppeteer.launch({
const {browser, close} = await launch({
...defaultBrowserOptions,
args: [
...defaultArgs,
@ -179,58 +183,64 @@ describe('request proxy', () => {
`--proxy-bypass-list=${new URL(emptyPageUrl).host}`,
],
});
try {
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
const response = (await page.goto(emptyPageUrl))!;
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
const response = (await page.goto(emptyPageUrl))!;
expect(response.ok()).toBe(true);
expect(proxiedRequestUrls).toEqual([]);
expect(response.ok()).toBe(true);
expect(proxiedRequestUrls).toEqual([]);
} finally {
await close();
}
});
/**
* See issues #7873, #7719, and #7698.
*/
it('should proxy requests when configured at context level', async () => {
const {puppeteer, defaultBrowserOptions, server} = getTestState();
const {defaultBrowserOptions, server} = getTestState();
const emptyPageUrl = getEmptyPageUrl(server);
browser = await puppeteer.launch({
const {browser, close} = await launch({
...defaultBrowserOptions,
args: defaultArgs,
});
try {
const context = await browser.createIncognitoBrowserContext({
proxyServer: proxyServerUrl,
});
const page = await context.newPage();
const response = (await page.goto(emptyPageUrl))!;
const context = await browser.createIncognitoBrowserContext({
proxyServer: proxyServerUrl,
});
const page = await context.newPage();
const response = (await page.goto(emptyPageUrl))!;
expect(response.ok()).toBe(true);
expect(proxiedRequestUrls).toEqual([emptyPageUrl]);
expect(response.ok()).toBe(true);
expect(proxiedRequestUrls).toEqual([emptyPageUrl]);
} finally {
await close();
}
});
it('should respect proxy bypass list when configured at context level', async () => {
const {puppeteer, defaultBrowserOptions, server} = getTestState();
const {defaultBrowserOptions, server} = getTestState();
const emptyPageUrl = getEmptyPageUrl(server);
browser = await puppeteer.launch({
const {browser, close} = await launch({
...defaultBrowserOptions,
args: defaultArgs,
});
try {
const context = await browser.createIncognitoBrowserContext({
proxyServer: proxyServerUrl,
proxyBypassList: [new URL(emptyPageUrl).host],
});
const page = await context.newPage();
const response = (await page.goto(emptyPageUrl))!;
const context = await browser.createIncognitoBrowserContext({
proxyServer: proxyServerUrl,
proxyBypassList: [new URL(emptyPageUrl).host],
});
const page = await context.newPage();
const response = (await page.goto(emptyPageUrl))!;
expect(response.ok()).toBe(true);
expect(proxiedRequestUrls).toEqual([]);
expect(response.ok()).toBe(true);
expect(proxiedRequestUrls).toEqual([]);
} finally {
await close();
}
});
});
});

View File

@ -20,6 +20,7 @@ import {
getTestState,
setupTestBrowserHooks,
setupTestPageAndContextHooks,
launch,
} from './mocha-utils.js';
describe('Screenshots', function () {
@ -223,9 +224,9 @@ describe('Screenshots', function () {
expect(screenshot).toBeGolden('screenshot-element-bounding-box.png');
});
it('should work with a null viewport', async () => {
const {defaultBrowserOptions, puppeteer, server} = getTestState();
const {defaultBrowserOptions, server} = getTestState();
const browser = await puppeteer.launch({
const {browser, close} = await launch({
...defaultBrowserOptions,
defaultViewport: null,
});
@ -240,7 +241,7 @@ describe('Screenshots', function () {
const screenshot = await elementHandle.screenshot();
expect(screenshot).toBeTruthy();
} finally {
await browser.close();
await close();
}
});
it('should take into account padding and border', async () => {

View File

@ -18,35 +18,30 @@ import fs from 'fs';
import path from 'path';
import expect from 'expect';
import {Browser} from 'puppeteer-core/internal/api/Browser.js';
import {Page} from 'puppeteer-core/internal/api/Page.js';
import {getTestState} from './mocha-utils.js';
import {getTestState, launch} from './mocha-utils.js';
describe('Tracing', function () {
let outputFile!: string;
let browser!: Browser;
let page!: Page;
let testState: Awaited<ReturnType<typeof launch>>;
/* we manually manage the browser here as we want a new browser for each
* individual test, which isn't the default behaviour of getTestState()
*/
beforeEach(async () => {
const {defaultBrowserOptions, puppeteer} = getTestState();
browser = await puppeteer.launch(defaultBrowserOptions);
page = await browser.newPage();
const {defaultBrowserOptions} = getTestState();
testState = await launch(defaultBrowserOptions);
outputFile = path.join(__dirname, 'trace.json');
});
afterEach(async () => {
await browser.close();
await testState.close();
if (fs.existsSync(outputFile)) {
fs.unlinkSync(outputFile);
}
});
it('should output a trace', async () => {
const {server} = getTestState();
const {server, page} = testState;
await page.tracing.start({screenshots: true, path: outputFile});
await page.goto(server.PREFIX + '/grid.html');
@ -55,6 +50,7 @@ describe('Tracing', function () {
});
it('should run with custom categories if provided', async () => {
const {page} = testState;
await page.tracing.start({
path: outputFile,
categories: ['-*', 'disabled-by-default-devtools.timeline.frame'],
@ -77,6 +73,7 @@ describe('Tracing', function () {
});
it('should run with default categories', async () => {
const {page} = testState;
await page.tracing.start({
path: outputFile,
});
@ -92,6 +89,7 @@ describe('Tracing', function () {
);
});
it('should throw if tracing on two pages', async () => {
const {page, browser} = testState;
await page.tracing.start({path: outputFile});
const newPage = await browser.newPage();
let error!: Error;
@ -103,7 +101,7 @@ describe('Tracing', function () {
await page.tracing.stop();
});
it('should return a buffer', async () => {
const {server} = getTestState();
const {page, server} = testState;
await page.tracing.start({screenshots: true, path: outputFile});
await page.goto(server.PREFIX + '/grid.html');
@ -112,7 +110,7 @@ describe('Tracing', function () {
expect(trace.toString()).toEqual(buf.toString());
});
it('should work without options', async () => {
const {server} = getTestState();
const {page, server} = testState;
await page.tracing.start();
await page.goto(server.PREFIX + '/grid.html');
@ -121,7 +119,7 @@ describe('Tracing', function () {
});
it('should return undefined in case of Buffer error', async () => {
const {server} = getTestState();
const {page, server} = testState;
await page.tracing.start({screenshots: true});
await page.goto(server.PREFIX + '/grid.html');
@ -139,7 +137,7 @@ describe('Tracing', function () {
});
it('should support a buffer without a path', async () => {
const {server} = getTestState();
const {page, server} = testState;
await page.tracing.start({screenshots: true});
await page.goto(server.PREFIX + '/grid.html');
@ -148,6 +146,7 @@ describe('Tracing', function () {
});
it('should properly fail if readProtocolStream errors out', async () => {
const {page} = testState;
await page.tracing.start({path: __dirname});
let error!: Error;