fix(codegen): generate routeFromHAR for --save-har option (#33480)

This commit is contained in:
Yury Semikhatsky 2024-11-11 11:59:20 -08:00 committed by GitHub
parent e3ed9fa7c3
commit e691ca7fbf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 55 additions and 59 deletions

View file

@ -171,6 +171,8 @@ export class CSharpLanguageGenerator implements LanguageGenerator {
using var playwright = await Playwright.CreateAsync(); using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.${toPascal(options.browserName)}.LaunchAsync(${formatObject(options.launchOptions, ' ', 'BrowserTypeLaunchOptions')}); await using var browser = await playwright.${toPascal(options.browserName)}.LaunchAsync(${formatObject(options.launchOptions, ' ', 'BrowserTypeLaunchOptions')});
var context = await browser.NewContextAsync(${formatContextOptions(options.contextOptions, options.deviceName)});`); var context = await browser.NewContextAsync(${formatContextOptions(options.contextOptions, options.deviceName)});`);
if (options.contextOptions.recordHar)
formatter.add(` await context.RouteFromHARAsync(${quote(options.contextOptions.recordHar.path)});`);
formatter.newLine(); formatter.newLine();
return formatter.format(); return formatter.format();
} }
@ -196,6 +198,8 @@ export class CSharpLanguageGenerator implements LanguageGenerator {
formatter.add(` [${this._mode === 'nunit' ? 'Test' : 'TestMethod'}] formatter.add(` [${this._mode === 'nunit' ? 'Test' : 'TestMethod'}]
public async Task MyTest() public async Task MyTest()
{`); {`);
if (options.contextOptions.recordHar)
formatter.add(` await context.RouteFromHARAsync(${quote(options.contextOptions.recordHar.path)});`);
return formatter.format(); return formatter.format();
} }
@ -261,32 +265,22 @@ function toPascal(value: string): string {
return value[0].toUpperCase() + value.slice(1); return value[0].toUpperCase() + value.slice(1);
} }
function convertContextOptions(options: BrowserContextOptions): any { function formatContextOptions(contextOptions: BrowserContextOptions, deviceName: string | undefined): string {
const result: any = { ...options }; let options = { ...contextOptions };
if (options.recordHar) { // recordHAR is replaced with routeFromHAR in the generated code.
result['recordHarPath'] = options.recordHar.path; delete options.recordHar;
result['recordHarContent'] = options.recordHar.content;
result['recordHarMode'] = options.recordHar.mode;
result['recordHarOmitContent'] = options.recordHar.omitContent;
result['recordHarUrlFilter'] = options.recordHar.urlFilter;
delete result.recordHar;
}
return result;
}
function formatContextOptions(options: BrowserContextOptions, deviceName: string | undefined): string {
const device = deviceName && deviceDescriptors[deviceName]; const device = deviceName && deviceDescriptors[deviceName];
if (!device) { if (!device) {
if (!Object.entries(options).length) if (!Object.entries(options).length)
return ''; return '';
return formatObject(convertContextOptions(options), ' ', 'BrowserNewContextOptions'); return formatObject(options, ' ', 'BrowserNewContextOptions');
} }
options = sanitizeDeviceOptions(device, options); options = sanitizeDeviceOptions(device, options);
if (!Object.entries(options).length) if (!Object.entries(options).length)
return `playwright.Devices[${quote(deviceName!)}]`; return `playwright.Devices[${quote(deviceName!)}]`;
return formatObject(convertContextOptions(options), ' ', `BrowserNewContextOptions(playwright.Devices[${quote(deviceName!)}])`); return formatObject(options, ' ', `BrowserNewContextOptions(playwright.Devices[${quote(deviceName!)}])`);
} }
class CSharpFormatter { class CSharpFormatter {

View file

@ -170,6 +170,8 @@ export class JavaLanguageGenerator implements LanguageGenerator {
try (Playwright playwright = Playwright.create()) { try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.${options.browserName}().launch(${formatLaunchOptions(options.launchOptions)}); Browser browser = playwright.${options.browserName}().launch(${formatLaunchOptions(options.launchOptions)});
BrowserContext context = browser.newContext(${formatContextOptions(options.contextOptions, options.deviceName)});`); BrowserContext context = browser.newContext(${formatContextOptions(options.contextOptions, options.deviceName)});`);
if (options.contextOptions.recordHar)
formatter.add(` context.routeFromHAR(${quote(options.contextOptions.recordHar.path)});`);
return formatter.format(); return formatter.format();
} }
@ -240,16 +242,6 @@ function formatContextOptions(contextOptions: BrowserContextOptions, deviceName:
lines.push(` .setLocale(${quote(options.locale)})`); lines.push(` .setLocale(${quote(options.locale)})`);
if (options.proxy) if (options.proxy)
lines.push(` .setProxy(new Proxy(${quote(options.proxy.server)}))`); lines.push(` .setProxy(new Proxy(${quote(options.proxy.server)}))`);
if (options.recordHar?.content)
lines.push(` .setRecordHarContent(HarContentPolicy.${options.recordHar?.content.toUpperCase()})`);
if (options.recordHar?.mode)
lines.push(` .setRecordHarMode(HarMode.${options.recordHar?.mode.toUpperCase()})`);
if (options.recordHar?.omitContent)
lines.push(` .setRecordHarOmitContent(true)`);
if (options.recordHar?.path)
lines.push(` .setRecordHarPath(Paths.get(${quote(options.recordHar.path)}))`);
if (options.recordHar?.urlFilter)
lines.push(` .setRecordHarUrlFilter(${quote(options.recordHar.urlFilter as string)})`);
if (options.serviceWorkers) if (options.serviceWorkers)
lines.push(` .setServiceWorkers(ServiceWorkerPolicy.${options.serviceWorkers.toUpperCase()})`); lines.push(` .setServiceWorkers(ServiceWorkerPolicy.${options.serviceWorkers.toUpperCase()})`);
if (options.storageState) if (options.storageState)

View file

@ -145,6 +145,8 @@ export class JavaScriptLanguageGenerator implements LanguageGenerator {
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' : ''}
test('test', async ({ page }) => {`); test('test', async ({ page }) => {`);
if (options.contextOptions.recordHar)
formatter.add(` await page.routeFromHAR(${quote(options.contextOptions.recordHar.path)});`);
return formatter.format(); return formatter.format();
} }
@ -160,6 +162,8 @@ ${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, false)});`); const context = await browser.newContext(${formatContextOptions(options.contextOptions, options.deviceName, false)});`);
if (options.contextOptions.recordHar)
formatter.add(` await context.routeFromHAR(${quote(options.contextOptions.recordHar.path)});`);
return formatter.format(); return formatter.format();
} }
@ -203,10 +207,8 @@ function formatObjectOrVoid(value: any, indent = ' '): string {
function formatContextOptions(options: BrowserContextOptions, deviceName: string | undefined, isTest: boolean): string { function formatContextOptions(options: BrowserContextOptions, deviceName: string | undefined, isTest: boolean): string {
const device = deviceName && deviceDescriptors[deviceName]; const device = deviceName && deviceDescriptors[deviceName];
if (isTest) { // recordHAR is replaced with routeFromHAR in the generated code.
// No recordHAR fixture in test. options = { ...options, recordHar: undefined };
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

@ -151,6 +151,8 @@ from playwright.sync_api import Page, expect
${fixture} ${fixture}
def test_example(page: Page) -> None {`); def test_example(page: Page) -> None {`);
if (options.contextOptions.recordHar)
formatter.add(` page.route_from_har(${quote(options.contextOptions.recordHar.path)})`);
} else if (this._isAsync) { } else if (this._isAsync) {
formatter.add(` formatter.add(`
import asyncio import asyncio
@ -161,6 +163,8 @@ from playwright.async_api import Playwright, async_playwright, expect
async def run(playwright: Playwright) -> None { async def run(playwright: Playwright) -> None {
browser = await playwright.${options.browserName}.launch(${formatOptions(options.launchOptions, false)}) browser = await playwright.${options.browserName}.launch(${formatOptions(options.launchOptions, false)})
context = await browser.new_context(${formatContextOptions(options.contextOptions, options.deviceName)})`); context = await browser.new_context(${formatContextOptions(options.contextOptions, options.deviceName)})`);
if (options.contextOptions.recordHar)
formatter.add(` await page.route_from_har(${quote(options.contextOptions.recordHar.path)})`);
} else { } else {
formatter.add(` formatter.add(`
import re import re
@ -170,6 +174,8 @@ from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None { def run(playwright: Playwright) -> None {
browser = playwright.${options.browserName}.launch(${formatOptions(options.launchOptions, false)}) browser = playwright.${options.browserName}.launch(${formatOptions(options.launchOptions, false)})
context = browser.new_context(${formatContextOptions(options.contextOptions, options.deviceName)})`); context = browser.new_context(${formatContextOptions(options.contextOptions, options.deviceName)})`);
if (options.contextOptions.recordHar)
formatter.add(` context.route_from_har(${quote(options.contextOptions.recordHar.path)})`);
} }
return formatter.format(); return formatter.format();
} }
@ -232,24 +238,13 @@ function formatOptions(value: any, hasArguments: boolean, asDict?: boolean): str
}).join(', '); }).join(', ');
} }
function convertContextOptions(options: BrowserContextOptions): any {
const result: any = { ...options };
if (options.recordHar) {
result['record_har_path'] = options.recordHar.path;
result['record_har_content'] = options.recordHar.content;
result['record_har_mode'] = options.recordHar.mode;
result['record_har_omit_content'] = options.recordHar.omitContent;
result['record_har_url_filter'] = options.recordHar.urlFilter;
delete result.recordHar;
}
return result;
}
function formatContextOptions(options: BrowserContextOptions, deviceName: string | undefined, asDict?: boolean): string { function formatContextOptions(options: BrowserContextOptions, deviceName: string | undefined, asDict?: boolean): string {
// recordHAR is replaced with routeFromHAR in the generated code.
options = { ...options, recordHar: undefined };
const device = deviceName && deviceDescriptors[deviceName]; const device = deviceName && deviceDescriptors[deviceName];
if (!device) if (!device)
return formatOptions(convertContextOptions(options), false, asDict); return formatOptions(options, false, asDict);
return `**playwright.devices[${quote(deviceName!)}]` + formatOptions(convertContextOptions(sanitizeDeviceOptions(device, options)), true, asDict); return `**playwright.devices[${quote(deviceName!)}]` + formatOptions(sanitizeDeviceOptions(device, options), true, asDict);
} }
class PythonFormatter { class PythonFormatter {

View file

@ -170,13 +170,7 @@ await context.StorageStateAsync(new BrowserContextStorageStateOptions
test('should work with --save-har', async ({ runCLI }, testInfo) => { test('should work with --save-har', async ({ runCLI }, testInfo) => {
const harFileName = testInfo.outputPath('har.har'); const harFileName = testInfo.outputPath('har.har');
const expectedResult = ` const expectedResult = `await context.RouteFromHARAsync(${JSON.stringify(harFileName)});`;
var context = await browser.NewContextAsync(new BrowserNewContextOptions
{
RecordHarMode = HarMode.Minimal,
RecordHarPath = ${JSON.stringify(harFileName)},
ServiceWorkers = ServiceWorkerPolicy.Block,
});`;
const cli = runCLI(['--target=csharp', `--save-har=${harFileName}`], { const cli = runCLI(['--target=csharp', `--save-har=${harFileName}`], {
autoExitWhen: expectedResult, autoExitWhen: expectedResult,
}); });
@ -204,6 +198,17 @@ for (const testFramework of ['nunit', 'mstest'] as const) {
} }
`); `);
}); });
test(`should work with --save-har in ${testFramework}`, async ({ runCLI }, testInfo) => {
const harFileName = testInfo.outputPath('har.har');
const expectedResult = `await context.RouteFromHARAsync("${harFileName}");`;
const cli = runCLI([`--target=csharp-${testFramework}`, `--save-har=${harFileName}`], {
autoExitWhen: expectedResult,
});
await cli.waitForCleanExit();
const json = JSON.parse(fs.readFileSync(harFileName, 'utf-8'));
expect(json.log.creator.name).toBe('Playwright');
});
} }
test(`should print a valid basic program in mstest`, async ({ runCLI }) => { test(`should print a valid basic program in mstest`, async ({ runCLI }) => {

View file

@ -91,10 +91,7 @@ test('should print load/save storage_state', async ({ runCLI, browserName }, tes
test('should work with --save-har', async ({ runCLI }, testInfo) => { test('should work with --save-har', async ({ runCLI }, testInfo) => {
const harFileName = testInfo.outputPath('har.har'); const harFileName = testInfo.outputPath('har.har');
const expectedResult = `BrowserContext context = browser.newContext(new Browser.NewContextOptions() const expectedResult = `context.routeFromHAR(${JSON.stringify(harFileName)});`;
.setRecordHarMode(HarMode.MINIMAL)
.setRecordHarPath(Paths.get(${JSON.stringify(harFileName)}))
.setServiceWorkers(ServiceWorkerPolicy.BLOCK));`;
const cli = runCLI(['--target=java', `--save-har=${harFileName}`], { const cli = runCLI(['--target=java', `--save-har=${harFileName}`], {
autoExitWhen: expectedResult, autoExitWhen: expectedResult,
}); });

View file

@ -146,7 +146,7 @@ asyncio.run(main())
test('should work with --save-har', async ({ runCLI }, testInfo) => { test('should work with --save-har', async ({ runCLI }, testInfo) => {
const harFileName = testInfo.outputPath('har.har'); const harFileName = testInfo.outputPath('har.har');
const expectedResult = `context = await browser.new_context(record_har_mode="minimal", record_har_path=${JSON.stringify(harFileName)}, service_workers="block")`; const expectedResult = `await page.route_from_har(${JSON.stringify(harFileName)})`;
const cli = runCLI(['--target=python-async', `--save-har=${harFileName}`], { const cli = runCLI(['--target=python-async', `--save-har=${harFileName}`], {
autoExitWhen: expectedResult, autoExitWhen: expectedResult,
}); });

View file

@ -87,8 +87,19 @@ test('test', async ({ page }) => {`;
test('should not generate recordHAR 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 = `test.use({ const expectedResult = ` await page.routeFromHAR('${harFileName.replace(/\\/g, '\\\\')}');`;
serviceWorkers: 'block' 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');
});
test('should generate routeFromHAR with --save-har', async ({ runCLI }, testInfo) => {
const harFileName = testInfo.outputPath('har.har');
const expectedResult = `test('test', async ({ page }) => {
await page.routeFromHAR('${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,