Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: set revision based on PUPPETEER_PRODUCT (#5643)
  • Loading branch information
mjzffr committed Apr 15, 2020
1 parent d817ae5 commit 35989a7
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 26 deletions.
10 changes: 5 additions & 5 deletions .travis.yml
Expand Up @@ -20,17 +20,17 @@ jobs:
before_install:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
# populate .local-firefox for launcher tests
- PUPPETEER_PRODUCT=firefox npm install
script: ./travis/chromium.sh
- node_js: "10.19.0"
dist: trusty
env:
- FIREFOX=true
- FIREFOX_HOME=$TRAVIS_HOME/firefox-latest
before_install:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
- "pyenv shell 3.6 && pip3 install --user mozdownload"
- "rm -rf $FIREFOX_HOME && mozdownload -t daily -d $FIREFOX_HOME/latest.tar.bz --log-level DEBUG"
- "cd $FIREFOX_HOME; tar -xvf latest.tar.bz; cd -; ls $FIREFOX_HOME/firefox/firefox"
# install step will add .local-chromium for launcher tests
- PUPPETEER_PRODUCT=firefox npm install
script:
- "BINARY=$FIREFOX_HOME/firefox/firefox npm run funit"
- "npm run funit"
8 changes: 6 additions & 2 deletions index.js
Expand Up @@ -29,10 +29,14 @@ Page.prototype.emulateMedia = Page.prototype.emulateMediaType;
// If node does not support async await, use the compiled version.
const Puppeteer = require('./lib/Puppeteer');
const packageJson = require('./package.json');
const preferredRevision = packageJson.puppeteer.chromium_revision;
let preferredRevision = packageJson.puppeteer.chromium_revision;
const isPuppeteerCore = packageJson.name === 'puppeteer-core';
// puppeteer-core ignores environment variables
const product = isPuppeteerCore ? undefined : process.env.PUPPETEER_PRODUCT || process.env.npm_config_puppeteer_product || process.env.npm_package_config_puppeteer_product;
if (!isPuppeteerCore && product === 'firefox')
preferredRevision = packageJson.puppeteer.firefox_revision;

const puppeteer = new Puppeteer(__dirname, preferredRevision, isPuppeteerCore);
const puppeteer = new Puppeteer(__dirname, preferredRevision, isPuppeteerCore, product);

// The introspection in `Helper.installAsyncStackHooks` references `Puppeteer._launcher`
// before the Puppeteer ctor is called, such that an invalid Launcher is selected at import,
Expand Down
19 changes: 11 additions & 8 deletions src/Launcher.js
Expand Up @@ -411,14 +411,7 @@ class FirefoxLauncher {
firefoxArguments.push(temporaryUserDataDir);
}

// replace 'latest' placeholder with actual downloaded revision
if (this._preferredRevision === 'latest') {
const browserFetcher = new BrowserFetcher(this._projectRoot, {product: this.product});
const localRevisions = await browserFetcher.localRevisions();
if (localRevisions[0])
this._preferredRevision = localRevisions[0];
}

await this._updateRevision();
let firefoxExecutable = executablePath;
if (!executablePath) {
const {missingText, executablePath} = resolveExecutablePath(this);
Expand Down Expand Up @@ -480,6 +473,16 @@ class FirefoxLauncher {
return resolveExecutablePath(this).executablePath;
}

async _updateRevision() {
// replace 'latest' placeholder with actual downloaded revision
if (this._preferredRevision === 'latest') {
const browserFetcher = new BrowserFetcher(this._projectRoot, { product: this.product });
const localRevisions = await browserFetcher.localRevisions();
if (localRevisions[0])
this._preferredRevision = localRevisions[0];
}
}

/**
* @return {string}
*/
Expand Down
14 changes: 10 additions & 4 deletions src/Puppeteer.js
Expand Up @@ -23,19 +23,23 @@ module.exports = class {
* @param {string} projectRoot
* @param {string} preferredRevision
* @param {boolean} isPuppeteerCore
* @param {string} productName
*/
constructor(projectRoot, preferredRevision, isPuppeteerCore) {
constructor(projectRoot, preferredRevision, isPuppeteerCore, productName) {
this._projectRoot = projectRoot;
this._preferredRevision = preferredRevision;
this._isPuppeteerCore = isPuppeteerCore;
// track changes to Launcher configuration via options or environment variables
this.__productName = productName;
}

/**
* @param {!(Launcher.LaunchOptions & Launcher.ChromeArgOptions & Launcher.BrowserOptions & {product?: string, extraPrefsFirefox?: !object})=} options
* @return {!Promise<!Puppeteer.Browser>}
*/
launch(options = {}) {
this._productName = options.product;
if (options.product)
this._productName = options.product;
return this._launcher.launch(options);
}

Expand All @@ -44,15 +48,17 @@ module.exports = class {
* @return {!Promise<!Puppeteer.Browser>}
*/
connect(options) {
this._productName = options.product;
if (options.product)
this._productName = options.product;
return this._launcher.connect(options);
}

/**
* @param {string} name
*/
set _productName(name) {
this._changedProduct = this.__productName !== name;
if (this.__productName !== name)
this._changedProduct = true;
this.__productName = name;
}

Expand Down
29 changes: 29 additions & 0 deletions test/launcher.spec.js
Expand Up @@ -353,6 +353,35 @@ describe('Launcher specs', function() {
await browser.close();
});
});
describe('Puppeteer.launch', function() {
let productName;

before(async() => {
const {puppeteer} = getTestState();
productName = puppeteer._productName;
});

after(async() => {
const {puppeteer} = getTestState();
puppeteer._lazyLauncher = undefined;
puppeteer._productName = productName;
});

it('should be able to launch different products', async() => {
const {puppeteer} = getTestState();

const products = ['firefox', 'chrome'];
for (const product of products) {
const browser = await puppeteer.launch({product});
const userAgent = await browser.userAgent();
await browser.close();
if (product === 'chrome')
expect(userAgent).toContain('Chrome');
else
expect(userAgent).toContain('Firefox');
}
});
});
describe('Puppeteer.connect', function() {
it('should be able to connect multiple times to the same browser', async() => {
const { puppeteer, defaultBrowserOptions} = getTestState();
Expand Down
18 changes: 11 additions & 7 deletions test/mocha-utils.js
Expand Up @@ -59,14 +59,18 @@ const defaultBrowserOptions = {
dumpio: !!process.env.DUMPIO,
};

(async() => {
if (defaultBrowserOptions.executablePath) {
console.warn(`WARN: running ${product} tests with ${defaultBrowserOptions.executablePath}`);
} else {
if (product === 'firefox')
await puppeteer._launcher._updateRevision();
const executablePath = puppeteer.executablePath();
if (!fs.existsSync(executablePath))
throw new Error(`Browser is not downloaded at ${executablePath}. Run 'npm install' and try to re-run tests`);
}
})();

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

const setupGoldenAssertions = () => {
const suffix = product.toLowerCase();
Expand Down

0 comments on commit 35989a7

Please sign in to comment.