Skip to content

Commit

Permalink
test: Distinguish Juggler unit tests from Firefox
Browse files Browse the repository at this point in the history
The funit script is renamed to fjunit (j for Juggler, which is
used only by the experimental puppeteer-firefox package.

In contrast, the funit script now refers to running Puppeteer
unit tests against the main puppeteer package with Firefox.
To do so with Firefox Nightly, run:

`BINARY=path/to/firefox npm run funit`

A number of changes in this patch make it easier to run
Puppeteer unit tests in Mozilla's CI.
  • Loading branch information
mjzffr committed Nov 9, 2019
1 parent 87f3714 commit 53de1a3
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 47 deletions.
4 changes: 2 additions & 2 deletions .cirrus.yml
Expand Up @@ -24,12 +24,12 @@ task:

task:
matrix:
- name: Firefox (node8 + linux)
- name: Firefox Juggler (node8 + linux)
container:
dockerfile: .ci/node8/Dockerfile.linux
xvfb_start_background_script: Xvfb :99 -ac -screen 0 1024x768x24
install_script: npm install --unsafe-perm && cd experimental/puppeteer-firefox && npm install --unsafe-perm
test_script: npm run funit
test_script: npm run fjunit

task:
osx_instance:
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -19,7 +19,7 @@ script:
- 'if [ "$NODE8" = "true" ]; then npm run lint; fi'
- 'if [ "$NODE8" = "true" ]; then npm run coverage; fi'
- 'if [ "$FIREFOX" = "true" ]; then cd experimental/puppeteer-firefox && npm i && cd ../..; fi'
- 'if [ "$FIREFOX" = "true" ]; then npm run funit; fi'
- 'if [ "$FIREFOX" = "true" ]; then npm run fjunit; fi'
- 'if [ "$NODE8" = "true" ]; then npm run test-doclint; fi'
- 'if [ "$NODE8" = "true" ]; then npm run test-types; fi'
- 'if [ "$NODE8" = "true" ]; then npm run bundle; fi'
Expand Down
11 changes: 9 additions & 2 deletions CONTRIBUTING.md
Expand Up @@ -199,10 +199,10 @@ npm run unit -- --break-on-failure
HEADLESS=false npm run unit
```

- To run tests with custom Chromium executable:
- To run tests with custom browser executable:

```bash
CHROME=<path-to-executable> npm run unit
BINARY=<path-to-executable> npm run unit
```

- To run tests in slow-mode:
Expand All @@ -211,6 +211,13 @@ CHROME=<path-to-executable> npm run unit
HEADLESS=false SLOW_MO=500 npm run unit
```

- To run tests with additional Launcher options:

```bash
EXTRA_LAUNCH_OPTIONS='{"args": ["--user-data-dir=some/path"], "handleSIGINT": true}' npm run unit
```


- To debug a test, "focus" a test first and then run:

```bash
Expand Down
6 changes: 3 additions & 3 deletions experimental/puppeteer-firefox/.cirrus.yml
Expand Up @@ -7,7 +7,7 @@ task:
dockerfile: .ci/node8/Dockerfile.linux
xvfb_start_background_script: Xvfb :99 -ac -screen 0 1024x768x24
install_script: npm install
test_script: npm run funit
test_script: npm run fjunit

task:
name: node8 (macOS)
Expand All @@ -19,7 +19,7 @@ task:
- brew install node@8
- brew link --force node@8
install_script: npm install
test_script: npm run funit
test_script: npm run fjunit

# task:
# allow_failures: true
Expand All @@ -28,4 +28,4 @@ task:
# os_version: 2016
# name: node8 (windows)
# install_script: npm install --unsafe-perm
# test_script: npm run funit
# test_script: npm run fjunit
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -12,7 +12,8 @@
},
"scripts": {
"unit": "node test/test.js",
"funit": "BROWSER=firefox node test/test.js",
"fjunit": "PUPPETEER_PRODUCT=juggler node test/test.js",
"funit": "PUPPETEER_PRODUCT=firefox node test/test.js",
"debug-unit": "node --inspect-brk test/test.js",
"test-doclint": "node utils/doclint/check_public_api/test/test.js && node utils/doclint/preprocessor/test.js",
"test": "npm run lint --silent && npm run coverage && npm run test-doclint && npm run test-types && node utils/testrunner/test/test.js",
Expand Down
14 changes: 7 additions & 7 deletions test/fixtures.spec.js
Expand Up @@ -16,7 +16,7 @@

const path = require('path');

module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, puppeteer, puppeteerPath, CHROME}) {
module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, puppeteer, puppeteerPath, JUGGLER}) {
const {describe, xdescribe, fdescribe, describe_fails_ffox} = testRunner;
const {it, fit, xit, it_fails_ffox} = testRunner;
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
Expand All @@ -38,16 +38,16 @@ module.exports.addTests = function({testRunner, expect, defaultBrowserOptions, p
const options = Object.assign({}, defaultBrowserOptions, {dumpio: true});
const res = spawn('node',
[path.join(__dirname, 'fixtures', 'dumpio.js'), puppeteerPath, JSON.stringify(options)]);
if (CHROME)
res.stderr.on('data', data => dumpioData += data.toString('utf8'));
else
if (JUGGLER)
res.stdout.on('data', data => dumpioData += data.toString('utf8'));
else
res.stderr.on('data', data => dumpioData += data.toString('utf8'));
await new Promise(resolve => res.on('close', resolve));

if (CHROME)
expect(dumpioData).toContain('DevTools listening on ws://');
else
if (JUGGLER)
expect(dumpioData).toContain('Juggler listening on ws://');
else
expect(dumpioData).toContain('DevTools listening on ws://');
});
it('should close the browser when the node process closes', async({ server }) => {
const {spawn, execSync} = require('child_process');
Expand Down
30 changes: 20 additions & 10 deletions test/puppeteer.spec.js
Expand Up @@ -28,31 +28,40 @@ module.exports.addTests = ({testRunner, product, puppeteerPath}) => {
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;

const CHROME = product === 'Chromium';
const FFOX = product === 'Firefox';
const FFOX = (product === 'Firefox' || product === 'Juggler');
const JUGGLER = product === 'Juggler';

const puppeteer = require(puppeteerPath);

const headless = (process.env.HEADLESS || 'true').trim().toLowerCase() === 'true';
const slowMo = parseInt((process.env.SLOW_MO || '0').trim(), 10);
let extraLaunchOptions = {};
try {
extraLaunchOptions = JSON.parse(process.env.EXTRA_LAUNCH_OPTIONS || '{}');
} catch (e) {
console.warn(`${YELLOW_COLOR}Error parsing EXTRA_LAUNCH_OPTIONS: ${e.message}. Skipping.${RESET_COLOR}`);
}

const defaultBrowserOptions = {
const defaultBrowserOptions = Object.assign({
handleSIGINT: false,
executablePath: CHROME ? process.env.CHROME : process.env.FFOX,
executablePath: process.env.BINARY,
slowMo,
headless,
dumpio: !!process.env.DUMPIO,
};
}, extraLaunchOptions);


if (defaultBrowserOptions.executablePath) {
console.warn(`${YELLOW_COLOR}WARN: running ${product} tests with ${defaultBrowserOptions.executablePath}${RESET_COLOR}`);
} else {
// Make sure the `npm install` was run after the chromium roll.
if (!fs.existsSync(puppeteer.executablePath()))
throw new Error(`Browser is not downloaded. Run 'npm install' and try to re-run tests`);
const path = puppeteer.executablePath();
if (!fs.existsSync(path))
throw new Error(`Browser is not downloaded at ${path}. Run 'npm install' and try to re-run tests`);
}

const GOLDEN_DIR = path.join(__dirname, 'golden-' + product.toLowerCase());
const OUTPUT_DIR = path.join(__dirname, 'output-' + product.toLowerCase());
const suffix = JUGGLER ? 'firefox' : product.toLowerCase();
const GOLDEN_DIR = path.join(__dirname, 'golden-' + suffix);
const OUTPUT_DIR = path.join(__dirname, 'output-' + suffix);
if (fs.existsSync(OUTPUT_DIR))
rm(OUTPUT_DIR);
const {expect} = new Matchers({
Expand All @@ -64,6 +73,7 @@ module.exports.addTests = ({testRunner, product, puppeteerPath}) => {
product,
FFOX,
CHROME,
JUGGLER,
puppeteer,
expect,
defaultBrowserOptions,
Expand All @@ -72,7 +82,7 @@ module.exports.addTests = ({testRunner, product, puppeteerPath}) => {
};

beforeAll(async() => {
if (FFOX && defaultBrowserOptions.executablePath)
if (JUGGLER && defaultBrowserOptions.executablePath)
await require('../experimental/puppeteer-firefox/misc/install-preferences')(defaultBrowserOptions.executablePath);
});

Expand Down
56 changes: 36 additions & 20 deletions test/test.js
Expand Up @@ -76,28 +76,44 @@ const CHROMIUM_NO_COVERAGE = new Set([
'page.emulateMedia', // Legacy alias for `page.emulateMediaType`.
]);

if (process.env.BROWSER === 'firefox') {
testRunner.addTestDSL('it_fails_ffox', 'skip');
testRunner.addSuiteDSL('describe_fails_ffox', 'skip');
describe('Firefox', () => {
require('./puppeteer.spec.js').addTests({
product: 'Firefox',
puppeteerPath: path.resolve(__dirname, '../experimental/puppeteer-firefox/'),
testRunner,
switch (process.env.PUPPETEER_PRODUCT) {
case 'firefox':
testRunner.addTestDSL('it_fails_ffox', 'skip');
testRunner.addSuiteDSL('describe_fails_ffox', 'skip');
describe('Firefox', () => {
require('./puppeteer.spec.js').addTests({
product: 'Firefox',
puppeteerPath: utils.projectRoot(),
testRunner,
});
if (process.env.COVERAGE)
utils.recordAPICoverage(testRunner, require('../lib/api'), require('../lib/Events').Events, CHROMIUM_NO_COVERAGE);
});
});
} else {
testRunner.addTestDSL('it_fails_ffox', 'run');
testRunner.addSuiteDSL('describe_fails_ffox', 'run');
describe('Chromium', () => {
require('./puppeteer.spec.js').addTests({
product: 'Chromium',
puppeteerPath: utils.projectRoot(),
testRunner,
break;
case 'juggler':
testRunner.addTestDSL('it_fails_ffox', 'skip');
testRunner.addSuiteDSL('describe_fails_ffox', 'skip');
describe('Firefox (Juggler)', () => {
require('./puppeteer.spec.js').addTests({
product: 'Juggler',
puppeteerPath: path.resolve(__dirname, '../experimental/puppeteer-firefox/'),
testRunner,
});
});
break;
case 'chrome':
default:
testRunner.addTestDSL('it_fails_ffox', 'run');
testRunner.addSuiteDSL('describe_fails_ffox', 'run');
describe('Chromium', () => {
require('./puppeteer.spec.js').addTests({
product: 'Chromium',
puppeteerPath: utils.projectRoot(),
testRunner,
});
if (process.env.COVERAGE)
utils.recordAPICoverage(testRunner, require('../lib/api'), require('../lib/Events').Events, CHROMIUM_NO_COVERAGE);
});
if (process.env.COVERAGE)
utils.recordAPICoverage(testRunner, require('../lib/api'), require('../lib/Events').Events, CHROMIUM_NO_COVERAGE);
});
}

if (process.env.CI && testRunner.hasFocusedTestsOrSuites()) {
Expand Down
2 changes: 1 addition & 1 deletion utils/protocol-types-generator/index.js
Expand Up @@ -3,7 +3,7 @@ const path = require('path');
const puppeteer = require('../..');
module.exports = puppeteer.launch({
pipe: false,
executablePath: process.env.CHROME,
executablePath: process.env.BINARY,
}).then(async browser => {
const origin = browser.wsEndpoint().match(/ws:\/\/([0-9A-Za-z:\.]*)\//)[1];
const page = await browser.newPage();
Expand Down

0 comments on commit 53de1a3

Please sign in to comment.