Skip to content

Commit

Permalink
fix(codegen): do not codegen non-existing fixtures (#32993)
Browse files Browse the repository at this point in the history
Closes #32981
  • Loading branch information
pavelfeldman authored Oct 8, 2024
1 parent 6ba5ee3 commit 7047c3a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
10 changes: 7 additions & 3 deletions packages/playwright-core/src/server/codegen/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class JavaScriptLanguageGenerator implements LanguageGenerator {

generateTestHeader(options: LanguageGeneratorOptions): string {
const formatter = new JavaScriptFormatter();
const useText = formatContextOptions(options.contextOptions, options.deviceName);
const useText = formatContextOptions(options.contextOptions, options.deviceName, this._isTest);
formatter.add(`
import { test, expect${options.deviceName ? ', devices' : ''} } from '@playwright/test';
${useText ? '\ntest.use(' + useText + ');\n' : ''}
Expand All @@ -157,7 +157,7 @@ ${useText ? '\ntest.use(' + useText + ');\n' : ''}
(async () => {
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();
}

Expand Down Expand Up @@ -199,8 +199,12 @@ function formatObjectOrVoid(value: any, indent = ' '): string {
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];
if (isTest) {
// No recordHAR fixture in test.
options = { ...options, recordHar: undefined };
}
if (!device)
return formatObjectOrVoid(options);
// Filter out all the properties from the device descriptor.
Expand Down
10 changes: 4 additions & 6 deletions tests/library/inspector/cli-codegen-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,11 @@ test('test', async ({ page }) => {`;
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 expectedResult = `
recordHar: {
mode: 'minimal',
path: '${harFileName.replace(/\\/g, '\\\\')}'
}`;
const expectedResult = `test.use({
serviceWorkers: 'block'
});`;
const cli = runCLI(['--target=playwright-test', `--save-har=${harFileName}`], {
autoExitWhen: expectedResult,
});
Expand Down

0 comments on commit 7047c3a

Please sign in to comment.