fix(cli): pdf/screenshot did not save artifacts (#21352)

As of today we support CLI argument wise `--save-storage` and
`--save-har` in pdf/screenshot CLI command. But they are not working,
since we just close the browser rather than the context where we take
care of saving the artifacts.

`launchContext` has 4 usages:

- open/codegen there closing the browser / `SIGINT` will close it
gracefully
- pdf/screenshot there we will now close the page which will then
[here](b00579edb7/packages/playwright-core/src/cli/cli.ts?q=cli.ts#L503)
close the browser.

I did not find any tests except the [installation
test](3e84ab4701/tests/installation/playwright-cli-screenshot-should-work.spec.ts),
I'm happy to add one for both command if requested.

Fixes https://github.com/microsoft/playwright/issues/20718
This commit is contained in:
Max Schmitt 2023-03-02 22:05:39 +01:00 committed by GitHub
parent b00579edb7
commit e1e69cbbcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -600,13 +600,14 @@ async function waitForPage(page: Page, captureOptions: CaptureOptions) {
}
async function screenshot(options: Options, captureOptions: CaptureOptions, url: string, path: string) {
const { browser, context } = await launchContext(options, true);
const { context } = await launchContext(options, true);
console.log('Navigating to ' + url);
const page = await openPage(context, url);
await waitForPage(page, captureOptions);
console.log('Capturing screenshot into ' + path);
await page.screenshot({ path, fullPage: !!captureOptions.fullPage });
await browser.close();
// launchContext takes care of closing the browser.
await page.close();
}
async function pdf(options: Options, captureOptions: CaptureOptions, url: string, path: string) {
@ -614,13 +615,14 @@ async function pdf(options: Options, captureOptions: CaptureOptions, url: string
console.error('PDF creation is only working with Chromium');
process.exit(1);
}
const { browser, context } = await launchContext({ ...options, browser: 'chromium' }, true);
const { context } = await launchContext({ ...options, browser: 'chromium' }, true);
console.log('Navigating to ' + url);
const page = await openPage(context, url);
await waitForPage(page, captureOptions);
console.log('Saving as pdf into ' + path);
await page.pdf!({ path });
await browser.close();
// launchContext takes care of closing the browser.
await page.close();
}
function lookupBrowserType(options: Options): BrowserType {