Last active
June 10, 2021 12:38
-
-
Save muuvmuuv/b74aeb2e7479ef78590c55b2a0786100 to your computer and use it in GitHub Desktop.
Use puppeteer browser with Cypress to avoid installing browsers on your system yourself or in CI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer') | |
/** | |
* Because no browsers are bundled with Cypress we will use the already installed | |
* puppeteer binary that we use for Karma. This function returns information | |
* about the latest downloaded binary for Chrome by Puppeteer. It works for now | |
* but might throw errors in future, it is a workaround not a plugin. | |
* | |
* @see https://docs.cypress.io/guides/guides/launching-browsers.html#Customize-available-browsers | |
*/ | |
async function getPuppeteerBrowser() { | |
const browserFetcher = puppeteer.createBrowserFetcher() | |
const revisions = await browserFetcher.localRevisions() | |
const revision = revisions[0] // we only care about the first revision | |
const info = browserFetcher.revisionInfo(revision) | |
/** | |
* We only care about chrome and we do not want to get interrupted by a browser | |
* window so it is started headless. | |
*/ | |
const browser = await puppeteer.launch({ | |
product: 'chrome', | |
headless: true, | |
}) | |
let browserVersion = await browser.version() | |
browserVersion = browserVersion.replace('HeadlessChrome/', '') | |
await browser.close() // !!! | |
return { | |
name: 'chrome', | |
channel: 'stable', | |
family: 'chromium', | |
displayName: 'Chrome', | |
version: browserVersion, | |
path: info.executablePath, | |
majorVersion: browserVersion.split('.')[0], | |
} | |
} | |
module.exports = async (_, config) => { | |
const chromeBrowser = await getPuppeteerBrowser() | |
config.browsers.push(chromeBrowser) | |
return config | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment