Skip to content

Commit

Permalink
chore(test): prepare tests to be run with Puppeteer-Firefox (#3833)
Browse files Browse the repository at this point in the history
This patch aligns Puppeteer testing infrastructure with the approach
we use in Puppeteer-Firefox.

This patch:
- makes all tests accept Puppeteer object as a function argument
  rather than require it statically. This way we can pass either
  Puppeteer or Puppeteer-Firefox to drive tests.
- renames the `puppeteer.spec.js` into `launcher.spec.js`. The
  `puppeteer.spec.js` is now the entry point for all cross-browsers
  tests.
  • Loading branch information
aslushnikov committed Jan 24, 2019
1 parent 7585e6d commit 1a79955
Show file tree
Hide file tree
Showing 31 changed files with 574 additions and 544 deletions.
4 changes: 1 addition & 3 deletions test/browser.spec.js
Expand Up @@ -13,10 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const utils = require('./utils');
const puppeteer = utils.requireRoot('index');

module.exports.addTests = function({testRunner, expect, headless}) {
module.exports.addTests = function({testRunner, expect, headless, puppeteer}) {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
Expand Down
3 changes: 1 addition & 2 deletions test/browsercontext.spec.js
Expand Up @@ -15,10 +15,9 @@
*/

const utils = require('./utils');
const puppeteer = utils.requireRoot('index');
const {TimeoutError} = utils.requireRoot('Errors');

module.exports.addTests = function({testRunner, expect}) {
module.exports.addTests = function({testRunner, expect, puppeteer}) {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
Expand Down
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
3 changes: 1 addition & 2 deletions test/headful.spec.js
Expand Up @@ -21,7 +21,6 @@ const {helper} = require('../lib/helper');
const rmAsync = helper.promisify(require('rimraf'));
const utils = require('./utils');
const {waitEvent} = utils;
const puppeteer = utils.requireRoot('index.js');
const mkdtempAsync = helper.promisify(fs.mkdtemp);

const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-');
Expand All @@ -40,7 +39,7 @@ function waitForBackgroundPageTarget(browser) {
});
}

module.exports.addTests = function({testRunner, expect, defaultBrowserOptions}) {
module.exports.addTests = function({testRunner, expect, puppeteer, defaultBrowserOptions}) {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
Expand Down
5 changes: 1 addition & 4 deletions test/ignorehttpserrors.spec.js
Expand Up @@ -14,10 +14,7 @@
* limitations under the License.
*/

const utils = require('./utils');
const puppeteer = utils.requireRoot('index.js');

module.exports.addTests = function({testRunner, expect, defaultBrowserOptions}) {
module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, puppeteer}) {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
Expand Down
446 changes: 446 additions & 0 deletions test/launcher.spec.js

Large diffs are not rendered by default.

511 changes: 98 additions & 413 deletions test/puppeteer.spec.js

Large diffs are not rendered by default.

142 changes: 25 additions & 117 deletions test/test.js
Expand Up @@ -13,37 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const fs = require('fs');
const rm = require('rimraf').sync;
const path = require('path');
const {TestServer} = require('../utils/testserver/');
const GoldenUtils = require('./golden-utils');
const GOLDEN_DIR = path.join(__dirname, 'golden');
const OUTPUT_DIR = path.join(__dirname, 'output');
const {TestRunner, Reporter, Matchers} = require('../utils/testrunner/');
const {TestRunner, Reporter} = require('../utils/testrunner/');
const utils = require('./utils');

const {helper, assert} = require('../lib/helper');
if (process.env.COVERAGE)
helper.recordPublicAPICoverage();

const puppeteer = utils.requireRoot('index');

const YELLOW_COLOR = '\x1b[33m';
const RESET_COLOR = '\x1b[0m';

const headless = (process.env.HEADLESS || 'true').trim().toLowerCase() === 'true';
const executablePath = process.env.CHROME;

if (executablePath)
console.warn(`${YELLOW_COLOR}WARN: running tests with ${executablePath}${RESET_COLOR}`);
// Make sure the `npm install` was run after the chromium roll.
assert(fs.existsSync(puppeteer.executablePath()), `Chromium is not Downloaded. Run 'npm install' and try to re-run tests`);

const slowMo = parseInt((process.env.SLOW_MO || '0').trim(), 10);
const defaultBrowserOptions = {
handleSIGINT: false,
executablePath,
executablePath: process.env.CHROME,
slowMo,
headless,
dumpio: (process.env.DUMPIO || 'false').trim().toLowerCase() === 'true',
Expand All @@ -59,14 +38,8 @@ require('events').defaultMaxListeners *= parallel;

const timeout = slowMo ? 0 : 10 * 1000;
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);

console.log('Testing on Node', process.version);

beforeAll(async state => {
Expand Down Expand Up @@ -102,99 +75,34 @@ beforeEach(async({server, httpsServer}) => {
httpsServer.reset();
});

describe('Browser', function() {
beforeAll(async state => {
state.browser = await puppeteer.launch(defaultBrowserOptions);
});

afterAll(async state => {
await state.browser.close();
state.browser = null;
});

beforeEach(async(state, test) => {
const rl = require('readline').createInterface({input: state.browser.process().stderr});
test.output = '';
rl.on('line', onLine);
state.tearDown = () => {
rl.removeListener('line', onLine);
rl.close();
};
function onLine(line) {
test.output += line + '\n';
}
});
describe('Chromium', () => {
const {helper} = require('../lib/helper');
if (process.env.COVERAGE)
helper.recordPublicAPICoverage();

afterEach(async state => {
state.tearDown();
require('./puppeteer.spec.js').addTests({
product: 'Chromium',
puppeteer: utils.requireRoot('index'),
defaultBrowserOptions,
testRunner,
});

describe('Page', function() {
beforeEach(async state => {
state.context = await state.browser.createIncognitoBrowserContext();
state.page = await state.context.newPage();
});

afterEach(async state => {
// This closes all pages.
await state.context.close();
state.context = null;
state.page = null;
if (process.env.COVERAGE) {
describe('COVERAGE', function() {
const coverage = helper.publicAPICoverage();
const disabled = new Set(['page.bringToFront']);
if (!headless)
disabled.add('page.pdf');

for (const method of coverage.keys()) {
(disabled.has(method) ? xit : it)(`public api '${method}' should be called`, async({page, server}) => {
if (!coverage.get(method))
throw new Error('NOT CALLED!');
});
}
});

// Page-level tests that are given a browser, a context and a page.
// Each test is launched in a new browser context.
require('./CDPSession.spec.js').addTests({testRunner, expect});
require('./accessibility.spec.js').addTests({testRunner, expect});
require('./browser.spec.js').addTests({testRunner, expect, headless});
require('./cookies.spec.js').addTests({testRunner, expect});
require('./coverage.spec.js').addTests({testRunner, expect});
require('./elementhandle.spec.js').addTests({testRunner, expect});
require('./queryselector.spec.js').addTests({testRunner, expect});
require('./waittask.spec.js').addTests({testRunner, expect});
require('./frame.spec.js').addTests({testRunner, expect});
require('./input.spec.js').addTests({testRunner, expect});
require('./mouse.spec.js').addTests({testRunner, expect});
require('./keyboard.spec.js').addTests({testRunner, expect});
require('./touchscreen.spec.js').addTests({testRunner, expect});
require('./click.spec.js').addTests({testRunner, expect});
require('./jshandle.spec.js').addTests({testRunner, expect});
require('./network.spec.js').addTests({testRunner, expect});
require('./page.spec.js').addTests({testRunner, expect, headless});
require('./dialog.spec.js').addTests({testRunner, expect, headless});
require('./navigation.spec.js').addTests({testRunner, expect, headless});
require('./evaluation.spec.js').addTests({testRunner, expect, headless});
require('./emulation.spec.js').addTests({testRunner, expect, headless});
require('./screenshot.spec.js').addTests({testRunner, expect});
require('./target.spec.js').addTests({testRunner, expect});
require('./worker.spec.js').addTests({testRunner, expect});
});

// Browser-level tests that are given a browser.
require('./browsercontext.spec.js').addTests({testRunner, expect});
}
});

// Top-level tests that launch Browser themselves.
require('./ignorehttpserrors.spec.js').addTests({testRunner, expect, defaultBrowserOptions});
require('./puppeteer.spec.js').addTests({testRunner, expect, defaultBrowserOptions});
require('./headful.spec.js').addTests({testRunner, expect, defaultBrowserOptions});
require('./tracing.spec.js').addTests({testRunner, expect, defaultBrowserOptions});

if (process.env.COVERAGE) {
describe('COVERAGE', function() {
const coverage = helper.publicAPICoverage();
const disabled = new Set(['page.bringToFront']);
if (!headless)
disabled.add('page.pdf');

for (const method of coverage.keys()) {
(disabled.has(method) ? xit : it)(`public api '${method}' should be called`, async({page, server}) => {
expect(coverage.get(method)).toBe(true);
});
}
});
}

if (process.env.CI && testRunner.hasFocusedTestsOrSuites()) {
console.error('ERROR: "focused" tests/suites are prohibitted on bots. Remove any "fit"/"fdescribe" declarations.');
process.exit(1);
Expand Down
4 changes: 1 addition & 3 deletions test/tracing.spec.js
Expand Up @@ -16,10 +16,8 @@

const fs = require('fs');
const path = require('path');
const utils = require('./utils');
const puppeteer = utils.requireRoot('index');

module.exports.addTests = function({testRunner, expect, defaultBrowserOptions}) {
module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, puppeteer}) {
const {describe, xdescribe, fdescribe} = testRunner;
const {it, fit, xit} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
Expand Down

0 comments on commit 1a79955

Please sign in to comment.