mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
test: flatten tests (#2337)
Today, we have tests split into multiple files, with files pulling tests from some other files. This patch starts explicitly gathering all tests from the same `test.js` file. Drive-By: move one test from `browser.spec.js` into `puppeteer.spec.js` since it starts browser itself.
This commit is contained in:
parent
06d61919ef
commit
5d106f79d8
@ -14,13 +14,10 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
const utils = require('./utils.js');
|
||||
|
||||
module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, puppeteer}) {
|
||||
module.exports.addTests = function({testRunner, expect, puppeteer, headless}) {
|
||||
const {describe, xdescribe, fdescribe} = testRunner;
|
||||
const {it, fit, xit} = testRunner;
|
||||
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
|
||||
const headless = defaultBrowserOptions.headless;
|
||||
|
||||
describe('Browser.version', function() {
|
||||
it('should return whether we are in headless', async({browser}) => {
|
||||
@ -48,39 +45,4 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
|
||||
await remoteBrowser.disconnect();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Browser.Events.disconnected', function() {
|
||||
it('should emitted when: browser gets closed, disconnected or underlying websocket gets closed', async() => {
|
||||
const originalBrowser = await puppeteer.launch(defaultBrowserOptions);
|
||||
const browserWSEndpoint = originalBrowser.wsEndpoint();
|
||||
const remoteBrowser1 = await puppeteer.connect({browserWSEndpoint});
|
||||
const remoteBrowser2 = await puppeteer.connect({browserWSEndpoint});
|
||||
|
||||
let disconnectedOriginal = 0;
|
||||
let disconnectedRemote1 = 0;
|
||||
let disconnectedRemote2 = 0;
|
||||
originalBrowser.on('disconnected', () => ++disconnectedOriginal);
|
||||
remoteBrowser1.on('disconnected', () => ++disconnectedRemote1);
|
||||
remoteBrowser2.on('disconnected', () => ++disconnectedRemote2);
|
||||
|
||||
await Promise.all([
|
||||
utils.waitEvent(remoteBrowser2, 'disconnected'),
|
||||
remoteBrowser2.disconnect(),
|
||||
]);
|
||||
|
||||
expect(disconnectedOriginal).toBe(0);
|
||||
expect(disconnectedRemote1).toBe(0);
|
||||
expect(disconnectedRemote2).toBe(1);
|
||||
|
||||
await Promise.all([
|
||||
utils.waitEvent(remoteBrowser1, 'disconnected'),
|
||||
utils.waitEvent(originalBrowser, 'disconnected'),
|
||||
originalBrowser.close(),
|
||||
]);
|
||||
|
||||
expect(disconnectedOriginal).toBe(1);
|
||||
expect(disconnectedRemote1).toBe(1);
|
||||
expect(disconnectedRemote2).toBe(1);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
@ -230,4 +230,4 @@ module.exports.addTests = function({testRunner, expect}) {
|
||||
|
||||
});
|
||||
});
|
||||
};
|
||||
};
|
||||
|
@ -176,4 +176,4 @@ module.exports.addTests = function({testRunner, expect}) {
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
};
|
||||
|
@ -17,11 +17,10 @@
|
||||
const path = require('path');
|
||||
const utils = require('./utils');
|
||||
|
||||
module.exports.addTests = function({testRunner, expect, PROJECT_ROOT}) {
|
||||
module.exports.addTests = function({testRunner, expect, DeviceDescriptors}) {
|
||||
const {describe, xdescribe, fdescribe} = testRunner;
|
||||
const {it, fit, xit} = testRunner;
|
||||
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
|
||||
const DeviceDescriptors = require(path.join(PROJECT_ROOT, 'DeviceDescriptors'));
|
||||
const iPhone = DeviceDescriptors['iPhone 6'];
|
||||
describe('input', function() {
|
||||
it('should click the button', async({page, server}) => {
|
||||
@ -441,4 +440,4 @@ module.exports.addTests = function({testRunner, expect, PROJECT_ROOT}) {
|
||||
};
|
||||
}
|
||||
});
|
||||
};
|
||||
};
|
||||
|
@ -113,4 +113,4 @@ module.exports.addTests = function({testRunner, expect}) {
|
||||
expect(aHandle.toString()).toBe('JSHandle@object');
|
||||
});
|
||||
});
|
||||
};
|
||||
};
|
||||
|
3514
test/page.spec.js
3514
test/page.spec.js
File diff suppressed because it is too large
Load Diff
@ -23,10 +23,11 @@ const readFileAsync = helper.promisify(fs.readFile);
|
||||
const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-');
|
||||
const utils = require('./utils');
|
||||
|
||||
module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, puppeteer, PROJECT_ROOT}) {
|
||||
module.exports.addTests = function({testRunner, expect, PROJECT_ROOT, defaultBrowserOptions}) {
|
||||
const {describe, xdescribe, fdescribe} = testRunner;
|
||||
const {it, fit, xit} = testRunner;
|
||||
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
|
||||
const puppeteer = require(PROJECT_ROOT);
|
||||
|
||||
describe('Puppeteer', function() {
|
||||
describe('BrowserFetcher', function() {
|
||||
@ -284,4 +285,40 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Browser.Events.disconnected', function() {
|
||||
it('should be emitted when: browser gets closed, disconnected or underlying websocket gets closed', async() => {
|
||||
const originalBrowser = await puppeteer.launch(defaultBrowserOptions);
|
||||
const browserWSEndpoint = originalBrowser.wsEndpoint();
|
||||
const remoteBrowser1 = await puppeteer.connect({browserWSEndpoint});
|
||||
const remoteBrowser2 = await puppeteer.connect({browserWSEndpoint});
|
||||
|
||||
let disconnectedOriginal = 0;
|
||||
let disconnectedRemote1 = 0;
|
||||
let disconnectedRemote2 = 0;
|
||||
originalBrowser.on('disconnected', () => ++disconnectedOriginal);
|
||||
remoteBrowser1.on('disconnected', () => ++disconnectedRemote1);
|
||||
remoteBrowser2.on('disconnected', () => ++disconnectedRemote2);
|
||||
|
||||
await Promise.all([
|
||||
utils.waitEvent(remoteBrowser2, 'disconnected'),
|
||||
remoteBrowser2.disconnect(),
|
||||
]);
|
||||
|
||||
expect(disconnectedOriginal).toBe(0);
|
||||
expect(disconnectedRemote1).toBe(0);
|
||||
expect(disconnectedRemote2).toBe(1);
|
||||
|
||||
await Promise.all([
|
||||
utils.waitEvent(remoteBrowser1, 'disconnected'),
|
||||
utils.waitEvent(originalBrowser, 'disconnected'),
|
||||
originalBrowser.close(),
|
||||
]);
|
||||
|
||||
expect(disconnectedOriginal).toBe(1);
|
||||
expect(disconnectedRemote1).toBe(1);
|
||||
expect(disconnectedRemote2).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
};
|
||||
|
75
test/test.js
75
test/test.js
@ -21,13 +21,13 @@ const GOLDEN_DIR = path.join(__dirname, 'golden');
|
||||
const OUTPUT_DIR = path.join(__dirname, 'output');
|
||||
const {TestRunner, Reporter, Matchers} = require('../utils/testrunner/');
|
||||
|
||||
|
||||
const {helper} = require('../lib/helper');
|
||||
if (process.env.COVERAGE)
|
||||
helper.recordPublicAPICoverage();
|
||||
|
||||
const PROJECT_ROOT = fs.existsSync(path.join(__dirname, '..', 'package.json')) ? path.join(__dirname, '..') : path.join(__dirname, '..', '..');
|
||||
const puppeteer = require(PROJECT_ROOT);
|
||||
const DeviceDescriptors = require(path.join(PROJECT_ROOT, 'DeviceDescriptors'));
|
||||
|
||||
const YELLOW_COLOR = '\x1b[33m';
|
||||
const RESET_COLOR = '\x1b[0m';
|
||||
@ -57,12 +57,11 @@ if (parallelArgIndex !== -1)
|
||||
require('events').defaultMaxListeners *= parallel;
|
||||
|
||||
const timeout = slowMo ? 0 : 10 * 1000;
|
||||
const runner = new TestRunner({timeout, parallel});
|
||||
new Reporter(runner);
|
||||
const {beforeAll, beforeEach, afterAll, describe, xit, it} = runner;
|
||||
const testRunner = new TestRunner({timeout, parallel});
|
||||
const {expect} = new Matchers({
|
||||
toBeGolden: GoldenUtils.compare.bind(null, GOLDEN_DIR, OUTPUT_DIR)
|
||||
});
|
||||
const {describe, it, xit, beforeAll, afterAll, beforeEach, afterEach} = testRunner;
|
||||
|
||||
if (fs.existsSync(OUTPUT_DIR))
|
||||
rm(OUTPUT_DIR);
|
||||
@ -88,11 +87,6 @@ beforeAll(async state => {
|
||||
state.httpsServer.EMPTY_PAGE = `https://localhost:${httpsPort}/empty.html`;
|
||||
});
|
||||
|
||||
beforeEach(async({server, httpsServer}) => {
|
||||
server.reset();
|
||||
httpsServer.reset();
|
||||
});
|
||||
|
||||
afterAll(async({server, httpsServer}) => {
|
||||
await Promise.all([
|
||||
server.stop(),
|
||||
@ -100,28 +94,53 @@ afterAll(async({server, httpsServer}) => {
|
||||
]);
|
||||
});
|
||||
|
||||
const testFiles = [
|
||||
'page.spec.js',
|
||||
'puppeteer.spec.js'
|
||||
];
|
||||
beforeEach(async({server, httpsServer}) => {
|
||||
server.reset();
|
||||
httpsServer.reset();
|
||||
});
|
||||
|
||||
testFiles
|
||||
.map(file => path.join(__dirname, file))
|
||||
.forEach(file =>
|
||||
require(file).addTests({
|
||||
testRunner: runner,
|
||||
expect,
|
||||
defaultBrowserOptions,
|
||||
puppeteer,
|
||||
PROJECT_ROOT
|
||||
})
|
||||
);
|
||||
// Top-level tests that launch Browser themselves.
|
||||
require('./puppeteer.spec.js').addTests({testRunner, expect, PROJECT_ROOT, defaultBrowserOptions});
|
||||
|
||||
describe('Page', function() {
|
||||
beforeAll(async state => {
|
||||
state.browser = await puppeteer.launch(defaultBrowserOptions);
|
||||
});
|
||||
|
||||
afterAll(async state => {
|
||||
await state.browser.close();
|
||||
state.browser = null;
|
||||
});
|
||||
|
||||
beforeEach(async state => {
|
||||
state.page = await state.browser.newPage();
|
||||
});
|
||||
|
||||
afterEach(async state => {
|
||||
await state.page.close();
|
||||
state.page = null;
|
||||
});
|
||||
|
||||
// Page-level tests that are given a browser and a page.
|
||||
require('./CDPSession.spec.js').addTests({testRunner, expect});
|
||||
require('./browser.spec.js').addTests({testRunner, expect, puppeteer, headless});
|
||||
require('./cookies.spec.js').addTests({testRunner, expect});
|
||||
require('./coverage.spec.js').addTests({testRunner, expect});
|
||||
require('./elementhandle.spec.js').addTests({testRunner, expect});
|
||||
require('./frame.spec.js').addTests({testRunner, expect});
|
||||
require('./input.spec.js').addTests({testRunner, expect, DeviceDescriptors});
|
||||
require('./jshandle.spec.js').addTests({testRunner, expect});
|
||||
require('./network.spec.js').addTests({testRunner, expect});
|
||||
require('./page.spec.js').addTests({testRunner, expect, puppeteer, DeviceDescriptors, headless});
|
||||
require('./target.spec.js').addTests({testRunner, expect});
|
||||
require('./tracing.spec.js').addTests({testRunner, expect});
|
||||
});
|
||||
|
||||
if (process.env.COVERAGE) {
|
||||
describe('COVERAGE', function(){
|
||||
const coverage = helper.publicAPICoverage();
|
||||
const disabled = new Set(['page.bringToFront']);
|
||||
if (!defaultBrowserOptions.headless)
|
||||
if (!headless)
|
||||
disabled.add('page.pdf');
|
||||
|
||||
for (const method of coverage.keys()) {
|
||||
@ -132,8 +151,10 @@ if (process.env.COVERAGE) {
|
||||
});
|
||||
}
|
||||
|
||||
if (process.env.CI && runner.hasFocusedTestsOrSuites()) {
|
||||
if (process.env.CI && testRunner.hasFocusedTestsOrSuites()) {
|
||||
console.error('ERROR: "focused" tests/suites are prohibitted on bots. Remove any "fit"/"fdescribe" declarations.');
|
||||
process.exit(1);
|
||||
}
|
||||
runner.run();
|
||||
|
||||
new Reporter(testRunner);
|
||||
testRunner.run();
|
||||
|
@ -53,4 +53,4 @@ module.exports.addTests = function({testRunner, expect}) {
|
||||
await page.tracing.stop();
|
||||
});
|
||||
});
|
||||
};
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user