Skip to content

Commit

Permalink
fix(codegen): do not codegen non-existing fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Oct 7, 2024
1 parent 6f16b6c commit 84144e7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 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
15 changes: 0 additions & 15 deletions tests/library/inspector/cli-codegen-test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,3 @@ test.use({
test('test', async ({ page }) => {`;
await cli.waitFor(expectedResult);
});

test('should work with --save-har', async ({ runCLI }, testInfo) => {
const harFileName = testInfo.outputPath('har.har');
const expectedResult = `
recordHar: {
mode: 'minimal',
path: '${harFileName.replace(/\\/g, '\\\\')}'
}`;
const cli = runCLI(['--target=playwright-test', `--save-har=${harFileName}`], {
autoExitWhen: expectedResult,
});
await cli.waitForCleanExit();
const json = JSON.parse(fs.readFileSync(harFileName, 'utf-8'));
expect(json.log.creator.name).toBe('Playwright');
});

0 comments on commit 84144e7

Please sign in to comment.