fix(codegen): do not codegen non-existing fixtures (#32993)

Closes https://github.com/microsoft/playwright/issues/32981
This commit is contained in:
Pavel Feldman 2024-10-07 17:12:36 -07:00 committed by GitHub
parent 6ba5ee3a83
commit 7047c3a6c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 9 deletions

View file

@ -138,7 +138,7 @@ export class JavaScriptLanguageGenerator implements LanguageGenerator {
generateTestHeader(options: LanguageGeneratorOptions): string { generateTestHeader(options: LanguageGeneratorOptions): string {
const formatter = new JavaScriptFormatter(); const formatter = new JavaScriptFormatter();
const useText = formatContextOptions(options.contextOptions, options.deviceName); const useText = formatContextOptions(options.contextOptions, options.deviceName, this._isTest);
formatter.add(` formatter.add(`
import { test, expect${options.deviceName ? ', devices' : ''} } from '@playwright/test'; import { test, expect${options.deviceName ? ', devices' : ''} } from '@playwright/test';
${useText ? '\ntest.use(' + useText + ');\n' : ''} ${useText ? '\ntest.use(' + useText + ');\n' : ''}
@ -157,7 +157,7 @@ ${useText ? '\ntest.use(' + useText + ');\n' : ''}
(async () => { (async () => {
const browser = await ${options.browserName}.launch(${formatObjectOrVoid(options.launchOptions)}); const browser = await ${options.browserName}.launch(${formatObjectOrVoid(options.launchOptions)});
const context = await browser.newContext(${formatContextOptions(options.contextOptions, options.deviceName)});`); const context = await browser.newContext(${formatContextOptions(options.contextOptions, options.deviceName, false)});`);
return formatter.format(); return formatter.format();
} }
@ -199,8 +199,12 @@ function formatObjectOrVoid(value: any, indent = ' '): string {
return result === '{}' ? '' : result; return result === '{}' ? '' : result;
} }
function formatContextOptions(options: BrowserContextOptions, deviceName: string | undefined): string { function formatContextOptions(options: BrowserContextOptions, deviceName: string | undefined, isTest: boolean): string {
const device = deviceName && deviceDescriptors[deviceName]; const device = deviceName && deviceDescriptors[deviceName];
if (isTest) {
// No recordHAR fixture in test.
options = { ...options, recordHar: undefined };
}
if (!device) if (!device)
return formatObjectOrVoid(options); return formatObjectOrVoid(options);
// Filter out all the properties from the device descriptor. // Filter out all the properties from the device descriptor.

View file

@ -85,13 +85,11 @@ test('test', async ({ page }) => {`;
await cli.waitFor(expectedResult); await cli.waitFor(expectedResult);
}); });
test('should work with --save-har', async ({ runCLI }, testInfo) => { test('should not generate recordHAR with --save-har', async ({ runCLI }, testInfo) => {
const harFileName = testInfo.outputPath('har.har'); const harFileName = testInfo.outputPath('har.har');
const expectedResult = ` const expectedResult = `test.use({
recordHar: { serviceWorkers: 'block'
mode: 'minimal', });`;
path: '${harFileName.replace(/\\/g, '\\\\')}'
}`;
const cli = runCLI(['--target=playwright-test', `--save-har=${harFileName}`], { const cli = runCLI(['--target=playwright-test', `--save-har=${harFileName}`], {
autoExitWhen: expectedResult, autoExitWhen: expectedResult,
}); });