Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(browser): Add new
browser
command and sub-commands
Implements #394
- Loading branch information
Göran Sander
committed
Jul 16, 2023
1 parent
b688758
commit c5db184
Showing
9 changed files
with
612 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* eslint-disable no-console */ | ||
const { test, expect, describe } = require('@jest/globals'); | ||
|
||
const { browserInstalled } = require('../lib/browser/browser-installed'); | ||
const { browserInstall } = require('../lib/browser/browser-install'); | ||
const { browserUninstallAll } = require('../lib/browser/browser-uninstall'); | ||
|
||
const defaultTestTimeout = process.env.BSI_TEST_TIMEOUT || 600000; // 10 minute default timeout | ||
|
||
console.log(`Jest timeout: ${defaultTestTimeout}`); | ||
|
||
const options = { | ||
loglevel: process.env.BSI_LOG_LEVEL || 'info', | ||
}; | ||
|
||
jest.setTimeout(defaultTestTimeout); | ||
|
||
describe('test browser list_installed command', () => { | ||
/** | ||
* List installed browsers | ||
* Should return an array of installed browser, zero or more entries | ||
*/ | ||
test('list installed browsers, should be zero or more', async () => { | ||
const installedBrowsers = await browserInstalled(options); | ||
expect(installedBrowsers.length).toBeGreaterThanOrEqual(0); | ||
}); | ||
}); | ||
|
||
describe('complex/combiation scenarios', () => { | ||
/** | ||
* Remove all installed browsers | ||
* Should return true. | ||
* | ||
* Install four differenct browsers (3 chrome versions, 1 firefox). | ||
* There should now be four installed browsers | ||
* | ||
* Remove all installed browsers. | ||
* Should return true. | ||
* | ||
* There should then be zero installed browsers. | ||
*/ | ||
test('install and uninstall several browsers', async () => { | ||
// Remove all installed browsers | ||
const uninstallRes1 = await browserUninstallAll(options); | ||
expect(uninstallRes1).toEqual(true); | ||
|
||
// There should now be zero installed browsers | ||
const installedBrowsers1 = await browserInstalled(options); | ||
expect(installedBrowsers1.length).toEqual(0); | ||
|
||
// Install four different browsers | ||
await browserInstall({ browser: 'chrome', browserVersion: 'stable' }); // Same as previous, should not install another browser | ||
await browserInstall({ browser: 'chrome', browserVersion: 'dev' }); | ||
await browserInstall({ browser: 'chrome', browserVersion: 'canary' }); | ||
await browserInstall({ browser: 'firefox', browserVersion: 'latest' }); // Same as previous, should not install another browser | ||
|
||
// There should now be four installed browsers | ||
const installedBrowsers2 = await browserInstalled(options); | ||
expect(installedBrowsers2.length).toEqual(4); | ||
|
||
// Remove all installed browsers | ||
const uninstallRes2 = await browserUninstallAll(options); | ||
expect(uninstallRes2).toEqual(true); | ||
|
||
// There should now be zero installed browsers | ||
const installedBrowsers3 = await browserInstalled(options); | ||
expect(installedBrowsers3.length).toEqual(0); | ||
}); | ||
}); | ||
|
||
describe('install/uninstall browser scenarios', () => { | ||
/** | ||
* Install a browser that exists | ||
* Should return true. | ||
* There should then be one installed browser. | ||
*/ | ||
test('install a browser that exists', async () => { | ||
// Remove all installed browsers | ||
const uninstallRes1 = await browserUninstallAll(options); | ||
expect(uninstallRes1).toEqual(true); | ||
|
||
// There should now be zero installed browsers | ||
const installedBrowsers1 = await browserInstalled(options); | ||
expect(installedBrowsers1.length).toEqual(0); | ||
|
||
// Install a browser | ||
await browserInstall({ browser: 'chrome', browserVersion: 'stable' }); | ||
|
||
// There should now be one installed browser | ||
const installedBrowsers2 = await browserInstalled(options); | ||
expect(installedBrowsers2.length).toEqual(1); | ||
|
||
// Remove all installed browsers | ||
const uninstallRes2 = await browserUninstallAll(options); | ||
expect(uninstallRes2).toEqual(true); | ||
|
||
// There should now be zero installed browsers | ||
const installedBrowsers3 = await browserInstalled(options); | ||
expect(installedBrowsers3.length).toEqual(0); | ||
}); | ||
|
||
/** | ||
* Install a browser that does not exist | ||
* Should throw an error. | ||
* There should then be zero installed browsers. | ||
*/ | ||
test('install a browser that does not exist', async () => { | ||
// Remove all installed browsers | ||
const uninstallRes1 = await browserUninstallAll(options); | ||
expect(uninstallRes1).toEqual(true); | ||
|
||
// There should now be zero installed browsers | ||
const installedBrowsers1 = await browserInstalled(options); | ||
expect(installedBrowsers1.length).toEqual(0); | ||
|
||
// Install a browser | ||
await expect( | ||
browserInstall({ browser: 'chrome', browserVersion: 'non-existent' }) | ||
).rejects.toThrow(); | ||
|
||
// There should now be zero installed browsers | ||
const installedBrowsers2 = await browserInstalled(options); | ||
expect(installedBrowsers2.length).toEqual(0); | ||
}); | ||
}); |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const { install, resolveBuildId, detectBrowserPlatform } = require('@puppeteer/browsers'); | ||
const path = require('path'); | ||
const { homedir } = require('os'); | ||
|
||
const { logger, setLoggingLevel, bsiExecutablePath, isPkg } = require('../../globals'); | ||
|
||
/** | ||
* Install browser | ||
* Returns true if browser installed successfully | ||
* @param {object} options | ||
* @param {string} options.browser - Browser to install | ||
* @param {string} options.browserVersion - Browser version to install | ||
* @returns {boolean} - True if browser installed successfully | ||
* @throws {Error} - If browser not installed successfully | ||
* @throws {Error} - If browser version not found | ||
* @throws {Error} - If error installing browser | ||
* @throws {Error} - If error resolving browser build id | ||
* @throws {Error} - If error detecting browser platform | ||
* @throws {Error} - If error getting browser cache path | ||
* @throws {Error} - If error getting browser executable path | ||
*/ | ||
// eslint-disable-next-line no-unused-vars | ||
const browserInstall = async (options, _command) => { | ||
try { | ||
// Set log level | ||
setLoggingLevel(options.loglevel); | ||
|
||
logger.verbose('Starting browser install'); | ||
logger.verbose(`Running as standalone app: ${isPkg}`); | ||
logger.debug(`BSI executable path: ${bsiExecutablePath}`); | ||
logger.debug(`Options: ${JSON.stringify(options, null, 2)}`); | ||
|
||
// Install browser | ||
const browserPath = path.join(homedir(), '.cache/puppeteer'); | ||
logger.debug(`Browser cache path: ${browserPath}`); | ||
|
||
const platform = await detectBrowserPlatform(); | ||
logger.debug(`Detected browser platform: ${platform}`); | ||
|
||
// Determine which browser version to install | ||
const buildId = await resolveBuildId(options.browser, platform, options.browserVersion); | ||
logger.info( | ||
`Resolved browser build id: "${buildId}" for browser "${options.browser}" version "${options.browserVersion}"` | ||
); | ||
logger.info('Installing browser...'); | ||
|
||
const browser = await install({ | ||
browser: options.browser, | ||
buildId, | ||
cacheDir: browserPath, | ||
}); | ||
|
||
logger.info(`Browser "${browser.browser}" version "${browser.buildId}" installed`); | ||
|
||
return true; | ||
} catch (err) { | ||
// Check if error is due to browser version missing | ||
if (err.message.includes('Download failed: server returned code 404.')) { | ||
logger.error(`Browser version "${options.browserVersion}" not found`); | ||
} else { | ||
logger.error(`Error installing browser: ${err}`); | ||
} | ||
|
||
throw err; | ||
} | ||
}; | ||
|
||
module.exports = { browserInstall }; |
This file contains 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
Oops, something went wrong.