Skip to content

Commit

Permalink
chore: readd browser setup hook (#10478)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightning00Blade committed Jul 3, 2023
1 parent 87aaed4 commit d0d738d
Show file tree
Hide file tree
Showing 43 changed files with 320 additions and 177 deletions.
4 changes: 3 additions & 1 deletion test/src/CDPSession.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
import expect from 'expect';
import {isErrorLike} from 'puppeteer-core/internal/util/ErrorLike.js';

import {getTestState} from './mocha-utils.js';
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';
import {waitEvent} from './utils.js';

describe('Target.createCDPSession', function () {
setupTestBrowserHooks();

it('should work', async () => {
const {page} = await getTestState();

Expand Down
8 changes: 4 additions & 4 deletions test/src/TargetManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ import {attachFrame} from './utils.js';

describe('TargetManager', () => {
/* We use a special browser for this test as we need the --site-per-process flag */
let testState: Awaited<ReturnType<typeof launch>> & {
let state: Awaited<ReturnType<typeof launch>> & {
browser: CDPBrowser;
};

beforeEach(async () => {
const {defaultBrowserOptions} = await getTestState({
skipLaunch: true,
});
testState = (await launch(
state = (await launch(
Object.assign({}, defaultBrowserOptions, {
args: (defaultBrowserOptions.args || []).concat([
'--site-per-process',
Expand All @@ -45,11 +45,11 @@ describe('TargetManager', () => {
});

afterEach(async () => {
await testState.close();
await state.close();
});

it('should handle targets', async () => {
const {server, context, browser} = testState;
const {server, context, browser} = state;

const targetManager = browser._targetManager();
expect(targetManager.getAvailableTargets().size).toBe(2);
Expand Down
4 changes: 3 additions & 1 deletion test/src/accessibility.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import assert from 'assert';
import expect from 'expect';
import {SerializedAXNode} from 'puppeteer-core/internal/common/Accessibility.js';

import {getTestState} from './mocha-utils.js';
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';

describe('Accessibility', function () {
setupTestBrowserHooks();

it('should work', async () => {
const {page, isFirefox} = await getTestState();

Expand Down
4 changes: 3 additions & 1 deletion test/src/ariaqueryhandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import expect from 'expect';
import {TimeoutError} from 'puppeteer';
import type {ElementHandle} from 'puppeteer-core/internal/api/ElementHandle.js';

import {getTestState} from './mocha-utils.js';
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';
import {attachFrame, detachFrame} from './utils.js';

describe('AriaQueryHandler', () => {
setupTestBrowserHooks();

describe('parseAriaSelector', () => {
it('should find button', async () => {
const {page} = await getTestState();
Expand Down
4 changes: 3 additions & 1 deletion test/src/browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

import expect from 'expect';

import {getTestState} from './mocha-utils.js';
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';

describe('Browser specs', function () {
setupTestBrowserHooks();

describe('Browser.version', function () {
it('should return version', async () => {
const {browser} = await getTestState();
Expand Down
5 changes: 3 additions & 2 deletions test/src/browsercontext.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
import expect from 'expect';
import {TimeoutError} from 'puppeteer';

import {getTestState} from './mocha-utils.js';
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';
import {waitEvent} from './utils.js';

describe('BrowserContext', function () {
setupTestBrowserHooks();

it('should have default context', async () => {
const {browser} = await getTestState({
skipContextCreation: true,
Expand Down Expand Up @@ -240,7 +242,6 @@ describe('BrowserContext', function () {
expect(browser.browserContexts()[0]!.id).toBeUndefined();

const context = await browser.createIncognitoBrowserContext();
console.log('2');
expect(browser.browserContexts()).toHaveLength(2);
expect(browser.browserContexts()[1]!.id).toBeDefined();
await context.close();
Expand Down
16 changes: 12 additions & 4 deletions test/src/chromiumonly.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
import {IncomingMessage} from 'http';

import expect from 'expect';
import {Deferred} from 'puppeteer-core/internal/util/Deferred.js';

import {getTestState, launch} from './mocha-utils.js';
import {getTestState, setupTestBrowserHooks, launch} from './mocha-utils.js';
import {waitEvent} from './utils.js';

// TODO: rename this test suite to launch/connect test suite as it actually
// works across browsers.
describe('Chromium-Specific Launcher tests', function () {
Expand Down Expand Up @@ -124,13 +124,19 @@ describe('Chromium-Specific Launcher tests', function () {
await close();
}
});
it('should fire "disconnected" when closing with pipe', async () => {
it('should fire "disconnected" when closing with pipe', async function () {
const {browser, close} = await launch({pipe: true});
try {
const disconnectedEventPromise = waitEvent(browser, 'disconnected');
// Emulate user exiting browser.
browser.process()!.kill();
await disconnectedEventPromise;
await Deferred.race([
disconnectedEventPromise,
Deferred.create({
message: `Failed in after Hook`,
timeout: this.timeout() - 1000,
}),
]);
} finally {
await close();
}
Expand All @@ -139,6 +145,8 @@ describe('Chromium-Specific Launcher tests', function () {
});

describe('Chromium-Specific Page Tests', function () {
setupTestBrowserHooks();

it('Page.setRequestInterception should work with intervention headers', async () => {
const {server, page} = await getTestState();

Expand Down
4 changes: 3 additions & 1 deletion test/src/click.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
import expect from 'expect';
import {KnownDevices} from 'puppeteer';

import {getTestState} from './mocha-utils.js';
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';
import {attachFrame} from './utils.js';

describe('Page.click', function () {
setupTestBrowserHooks();

it('should click the button', async () => {
const {page, server} = await getTestState();

Expand Down
9 changes: 8 additions & 1 deletion test/src/cookies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
*/
import expect from 'expect';

import {expectCookieEquals, getTestState, launch} from './mocha-utils.js';
import {
expectCookieEquals,
getTestState,
launch,
setupTestBrowserHooks,
} from './mocha-utils.js';

describe('Cookie specs', () => {
setupTestBrowserHooks();

describe('Page.cookies', function () {
it('should return no cookies in pristine browser context', async () => {
const {page, server} = await getTestState();
Expand Down
5 changes: 3 additions & 2 deletions test/src/coverage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

import expect from 'expect';

import {getTestState} from './mocha-utils.js';
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';

describe('Coverage specs', function () {
setupTestBrowserHooks();

describe('JSCoverage', function () {
it('should work', async () => {
const {page, server} = await getTestState();
Expand Down Expand Up @@ -269,7 +271,6 @@ describe('Coverage specs', function () {
await page.goto(server.PREFIX + '/csscoverage/media.html');
const coverage = await page.coverage.stopCSSCoverage();
expect(coverage).toHaveLength(1);
console.log(coverage);
expect(coverage[0]!.url).toContain('/csscoverage/media.html');
expect(coverage[0]!.ranges).toEqual([{start: 8, end: 40}]);
});
Expand Down
8 changes: 7 additions & 1 deletion test/src/defaultbrowsercontext.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@
*/
import expect from 'expect';

import {expectCookieEquals, getTestState} from './mocha-utils.js';
import {
expectCookieEquals,
getTestState,
setupTestBrowserHooks,
} from './mocha-utils.js';

describe('DefaultBrowserContext', function () {
setupTestBrowserHooks();

it('page.cookies() should work', async () => {
const {page, server} = await getTestState();

Expand Down
4 changes: 3 additions & 1 deletion test/src/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import expect from 'expect';
import sinon from 'sinon';

import {getTestState} from './mocha-utils.js';
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';

describe('Page.Events.Dialog', function () {
setupTestBrowserHooks();

it('should fire', async () => {
const {page} = await getTestState();

Expand Down
4 changes: 3 additions & 1 deletion test/src/drag-and-drop.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

import expect from 'expect';

import {getTestState} from './mocha-utils.js';
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';

describe('Input.drag', function () {
setupTestBrowserHooks();

it('should throw an exception if not enabled before usage', async () => {
const {page, server} = await getTestState();

Expand Down
3 changes: 3 additions & 0 deletions test/src/elementhandle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ import sinon from 'sinon';

import {
getTestState,
setupTestBrowserHooks,
shortWaitForArrayToHaveAtLeastNElements,
} from './mocha-utils.js';
import {attachFrame} from './utils.js';

describe('ElementHandle specs', function () {
setupTestBrowserHooks();

describe('ElementHandle.boundingBox', function () {
it('should work', async () => {
const {page, server} = await getTestState();
Expand Down
4 changes: 3 additions & 1 deletion test/src/emulation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
import expect from 'expect';
import {KnownDevices, PredefinedNetworkConditions} from 'puppeteer';

import {getTestState} from './mocha-utils.js';
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';

const iPhone = KnownDevices['iPhone 6'];
const iPhoneLandscape = KnownDevices['iPhone 6 landscape'];

describe('Emulation', () => {
setupTestBrowserHooks();

describe('Page.viewport', function () {
it('should get the proper viewport size', async () => {
const {page} = await getTestState();
Expand Down
4 changes: 3 additions & 1 deletion test/src/evaluation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

import expect from 'expect';

import {getTestState} from './mocha-utils.js';
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';
import {attachFrame} from './utils.js';

describe('Evaluation specs', function () {
setupTestBrowserHooks();

describe('Page.evaluate', function () {
it('should work', async () => {
const {page} = await getTestState();
Expand Down
5 changes: 3 additions & 2 deletions test/src/fixtures.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import path from 'path';

import expect from 'expect';

import {getTestState} from './mocha-utils.js';
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';
import {waitEvent} from './utils.js';

describe('Fixtures', function () {
setupTestBrowserHooks();

it('dumpio option should work with pipe option', async () => {
const {defaultBrowserOptions, puppeteerPath, headless} =
await getTestState();
Expand All @@ -49,7 +51,6 @@ describe('Fixtures', function () {
await new Promise(resolve => {
return res.on('close', resolve);
});
console.log(dumpioData);
expect(dumpioData).toContain('message from dumpio');
});
it('should dump browser process stderr', async () => {
Expand Down
4 changes: 3 additions & 1 deletion test/src/frame.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import expect from 'expect';
import {Frame} from 'puppeteer-core/internal/api/Frame.js';
import {CDPSession} from 'puppeteer-core/internal/common/Connection.js';

import {getTestState} from './mocha-utils.js';
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';
import {
attachFrame,
detachFrame,
Expand All @@ -28,6 +28,8 @@ import {
} from './utils.js';

describe('Frame specs', function () {
setupTestBrowserHooks();

describe('Frame.executionContext', function () {
it('should work', async () => {
const {page, server} = await getTestState();
Expand Down
4 changes: 3 additions & 1 deletion test/src/idle_override.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import expect from 'expect';
import {ElementHandle} from 'puppeteer-core/internal/api/ElementHandle.js';
import {Page} from 'puppeteer-core/internal/api/Page.js';

import {getTestState} from './mocha-utils.js';
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';

describe('Emulate idle state', () => {
setupTestBrowserHooks();

async function getIdleState(page: Page) {
const stateElement = (await page.$('#state')) as ElementHandle<HTMLElement>;
return await page.evaluate(element => {
Expand Down
Loading

0 comments on commit d0d738d

Please sign in to comment.