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:
Andrey Lushnikov 2018-04-09 16:38:00 -07:00 committed by GitHub
parent 06d61919ef
commit 5d106f79d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 1829 additions and 1852 deletions

View File

@ -14,13 +14,10 @@
* limitations under the License. * limitations under the License.
*/ */
const utils = require('./utils.js'); module.exports.addTests = function({testRunner, expect, puppeteer, headless}) {
module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, puppeteer}) {
const {describe, xdescribe, fdescribe} = testRunner; const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner; const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
const headless = defaultBrowserOptions.headless;
describe('Browser.version', function() { describe('Browser.version', function() {
it('should return whether we are in headless', async({browser}) => { 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(); 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);
});
});
}; };

View File

@ -17,11 +17,10 @@
const path = require('path'); const path = require('path');
const utils = require('./utils'); 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 {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner; const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
const DeviceDescriptors = require(path.join(PROJECT_ROOT, 'DeviceDescriptors'));
const iPhone = DeviceDescriptors['iPhone 6']; const iPhone = DeviceDescriptors['iPhone 6'];
describe('input', function() { describe('input', function() {
it('should click the button', async({page, server}) => { it('should click the button', async({page, server}) => {

View File

@ -18,53 +18,12 @@ const path = require('path');
const utils = require('./utils'); const utils = require('./utils');
const {waitEvent, getPDFPages, cssPixelsToInches} = require('./utils'); const {waitEvent, getPDFPages, cssPixelsToInches} = require('./utils');
module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, puppeteer, PROJECT_ROOT}) { module.exports.addTests = function({testRunner, expect, puppeteer, DeviceDescriptors, headless}) {
const {describe, xdescribe, fdescribe} = testRunner; const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner; const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
const DeviceDescriptors = require(path.join(PROJECT_ROOT, 'DeviceDescriptors'));
const iPhone = DeviceDescriptors['iPhone 6']; const iPhone = DeviceDescriptors['iPhone 6'];
const iPhoneLandscape = DeviceDescriptors['iPhone 6 landscape']; const iPhoneLandscape = DeviceDescriptors['iPhone 6 landscape'];
const headless = defaultBrowserOptions.headless;
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;
});
const testFiles = [
'browser.spec.js',
'elementhandle.spec.js',
'jshandle.spec.js',
'tracing.spec.js',
'frame.spec.js',
'input.spec.js',
'network.spec.js',
'cookies.spec.js',
'target.spec.js',
'CDPSession.spec.js',
'coverage.spec.js'
];
testFiles
.map(file => path.join(__dirname, file))
.forEach(file =>
require(file).addTests({testRunner, expect, defaultBrowserOptions, PROJECT_ROOT, puppeteer})
);
describe('Page.close', function() { describe('Page.close', function() {
it('should reject all promises when page is closed', async({browser}) => { it('should reject all promises when page is closed', async({browser}) => {
@ -1882,5 +1841,4 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
await closedPromise; await closedPromise;
}); });
}); });
});
}; };

View File

@ -23,10 +23,11 @@ const readFileAsync = helper.promisify(fs.readFile);
const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-'); const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-');
const utils = require('./utils'); 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 {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner; const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
const puppeteer = require(PROJECT_ROOT);
describe('Puppeteer', function() { describe('Puppeteer', function() {
describe('BrowserFetcher', 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);
});
});
}; };

View File

@ -21,13 +21,13 @@ const GOLDEN_DIR = path.join(__dirname, 'golden');
const OUTPUT_DIR = path.join(__dirname, 'output'); const OUTPUT_DIR = path.join(__dirname, 'output');
const {TestRunner, Reporter, Matchers} = require('../utils/testrunner/'); const {TestRunner, Reporter, Matchers} = require('../utils/testrunner/');
const {helper} = require('../lib/helper'); const {helper} = require('../lib/helper');
if (process.env.COVERAGE) if (process.env.COVERAGE)
helper.recordPublicAPICoverage(); helper.recordPublicAPICoverage();
const PROJECT_ROOT = fs.existsSync(path.join(__dirname, '..', 'package.json')) ? path.join(__dirname, '..') : path.join(__dirname, '..', '..'); const PROJECT_ROOT = fs.existsSync(path.join(__dirname, '..', 'package.json')) ? path.join(__dirname, '..') : path.join(__dirname, '..', '..');
const puppeteer = require(PROJECT_ROOT); const puppeteer = require(PROJECT_ROOT);
const DeviceDescriptors = require(path.join(PROJECT_ROOT, 'DeviceDescriptors'));
const YELLOW_COLOR = '\x1b[33m'; const YELLOW_COLOR = '\x1b[33m';
const RESET_COLOR = '\x1b[0m'; const RESET_COLOR = '\x1b[0m';
@ -57,12 +57,11 @@ if (parallelArgIndex !== -1)
require('events').defaultMaxListeners *= parallel; require('events').defaultMaxListeners *= parallel;
const timeout = slowMo ? 0 : 10 * 1000; const timeout = slowMo ? 0 : 10 * 1000;
const runner = new TestRunner({timeout, parallel}); const testRunner = new TestRunner({timeout, parallel});
new Reporter(runner);
const {beforeAll, beforeEach, afterAll, describe, xit, it} = runner;
const {expect} = new Matchers({ const {expect} = new Matchers({
toBeGolden: GoldenUtils.compare.bind(null, GOLDEN_DIR, OUTPUT_DIR) toBeGolden: GoldenUtils.compare.bind(null, GOLDEN_DIR, OUTPUT_DIR)
}); });
const {describe, it, xit, beforeAll, afterAll, beforeEach, afterEach} = testRunner;
if (fs.existsSync(OUTPUT_DIR)) if (fs.existsSync(OUTPUT_DIR))
rm(OUTPUT_DIR); rm(OUTPUT_DIR);
@ -88,11 +87,6 @@ beforeAll(async state => {
state.httpsServer.EMPTY_PAGE = `https://localhost:${httpsPort}/empty.html`; state.httpsServer.EMPTY_PAGE = `https://localhost:${httpsPort}/empty.html`;
}); });
beforeEach(async({server, httpsServer}) => {
server.reset();
httpsServer.reset();
});
afterAll(async({server, httpsServer}) => { afterAll(async({server, httpsServer}) => {
await Promise.all([ await Promise.all([
server.stop(), server.stop(),
@ -100,28 +94,53 @@ afterAll(async({server, httpsServer}) => {
]); ]);
}); });
const testFiles = [ beforeEach(async({server, httpsServer}) => {
'page.spec.js', server.reset();
'puppeteer.spec.js' httpsServer.reset();
]; });
testFiles // Top-level tests that launch Browser themselves.
.map(file => path.join(__dirname, file)) require('./puppeteer.spec.js').addTests({testRunner, expect, PROJECT_ROOT, defaultBrowserOptions});
.forEach(file =>
require(file).addTests({ describe('Page', function() {
testRunner: runner, beforeAll(async state => {
expect, state.browser = await puppeteer.launch(defaultBrowserOptions);
defaultBrowserOptions, });
puppeteer,
PROJECT_ROOT 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) { if (process.env.COVERAGE) {
describe('COVERAGE', function(){ describe('COVERAGE', function(){
const coverage = helper.publicAPICoverage(); const coverage = helper.publicAPICoverage();
const disabled = new Set(['page.bringToFront']); const disabled = new Set(['page.bringToFront']);
if (!defaultBrowserOptions.headless) if (!headless)
disabled.add('page.pdf'); disabled.add('page.pdf');
for (const method of coverage.keys()) { 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.'); console.error('ERROR: "focused" tests/suites are prohibitted on bots. Remove any "fit"/"fdescribe" declarations.');
process.exit(1); process.exit(1);
} }
runner.run();
new Reporter(testRunner);
testRunner.run();