chore: add url option to routeFromHAR call if codegen launched with --save-har-glob (#34048)
This commit is contained in:
parent
d9e5ca06bf
commit
c2d057ba23
|
@ -171,8 +171,10 @@ export class CSharpLanguageGenerator implements LanguageGenerator {
|
|||
using var playwright = await Playwright.CreateAsync();
|
||||
await using var browser = await playwright.${toPascal(options.browserName)}.LaunchAsync(${formatObject(options.launchOptions, ' ', 'BrowserTypeLaunchOptions')});
|
||||
var context = await browser.NewContextAsync(${formatContextOptions(options.contextOptions, options.deviceName)});`);
|
||||
if (options.contextOptions.recordHar)
|
||||
formatter.add(` await context.RouteFromHARAsync(${quote(options.contextOptions.recordHar.path)});`);
|
||||
if (options.contextOptions.recordHar) {
|
||||
const url = options.contextOptions.recordHar.urlFilter;
|
||||
formatter.add(` await context.RouteFromHARAsync(${quote(options.contextOptions.recordHar.path)}${url ? `, ${formatObject({ url }, ' ', 'BrowserContextRouteFromHAROptions')}` : ''});`);
|
||||
}
|
||||
formatter.newLine();
|
||||
return formatter.format();
|
||||
}
|
||||
|
@ -198,8 +200,10 @@ export class CSharpLanguageGenerator implements LanguageGenerator {
|
|||
formatter.add(` [${this._mode === 'nunit' ? 'Test' : 'TestMethod'}]
|
||||
public async Task MyTest()
|
||||
{`);
|
||||
if (options.contextOptions.recordHar)
|
||||
formatter.add(` await context.RouteFromHARAsync(${quote(options.contextOptions.recordHar.path)});`);
|
||||
if (options.contextOptions.recordHar) {
|
||||
const url = options.contextOptions.recordHar.urlFilter;
|
||||
formatter.add(` await Context.RouteFromHARAsync(${quote(options.contextOptions.recordHar.path)}${url ? `, ${formatObject({ url }, ' ', 'BrowserContextRouteFromHAROptions')}` : ''});`);
|
||||
}
|
||||
return formatter.format();
|
||||
}
|
||||
|
||||
|
|
|
@ -150,28 +150,38 @@ export class JavaLanguageGenerator implements LanguageGenerator {
|
|||
import com.microsoft.playwright.Page;
|
||||
import com.microsoft.playwright.options.*;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
${options.contextOptions.recordHar ? `import java.nio.file.Paths;\n` : ''}import org.junit.jupiter.api.*;
|
||||
import static com.microsoft.playwright.assertions.PlaywrightAssertions.*;
|
||||
|
||||
@UsePlaywright
|
||||
public class TestExample {
|
||||
@Test
|
||||
void test(Page page) {`);
|
||||
if (options.contextOptions.recordHar) {
|
||||
const url = options.contextOptions.recordHar.urlFilter;
|
||||
const recordHarOptions = typeof url === 'string' ? `, new Page.RouteFromHAROptions()
|
||||
.setUrl(${quote(url)})` : '';
|
||||
formatter.add(` page.routeFromHAR(Paths.get(${quote(options.contextOptions.recordHar.path)})${recordHarOptions});`);
|
||||
}
|
||||
return formatter.format();
|
||||
}
|
||||
formatter.add(`
|
||||
import com.microsoft.playwright.*;
|
||||
import com.microsoft.playwright.options.*;
|
||||
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
|
||||
import java.util.*;
|
||||
${options.contextOptions.recordHar ? `import java.nio.file.Paths;\n` : ''}import java.util.*;
|
||||
|
||||
public class Example {
|
||||
public static void main(String[] args) {
|
||||
try (Playwright playwright = Playwright.create()) {
|
||||
Browser browser = playwright.${options.browserName}().launch(${formatLaunchOptions(options.launchOptions)});
|
||||
BrowserContext context = browser.newContext(${formatContextOptions(options.contextOptions, options.deviceName)});`);
|
||||
if (options.contextOptions.recordHar)
|
||||
formatter.add(` context.routeFromHAR(${quote(options.contextOptions.recordHar.path)});`);
|
||||
if (options.contextOptions.recordHar) {
|
||||
const url = options.contextOptions.recordHar.urlFilter;
|
||||
const recordHarOptions = typeof url === 'string' ? `, new BrowserContext.RouteFromHAROptions()
|
||||
.setUrl(${quote(url)})` : '';
|
||||
formatter.add(` context.routeFromHAR(Paths.get(${quote(options.contextOptions.recordHar.path)})${recordHarOptions});`);
|
||||
}
|
||||
return formatter.format();
|
||||
}
|
||||
|
||||
|
|
|
@ -147,8 +147,10 @@ export class JavaScriptLanguageGenerator implements LanguageGenerator {
|
|||
import { test, expect${options.deviceName ? ', devices' : ''} } from '@playwright/test';
|
||||
${useText ? '\ntest.use(' + useText + ');\n' : ''}
|
||||
test('test', async ({ page }) => {`);
|
||||
if (options.contextOptions.recordHar)
|
||||
formatter.add(` await page.routeFromHAR(${quote(options.contextOptions.recordHar.path)});`);
|
||||
if (options.contextOptions.recordHar) {
|
||||
const url = options.contextOptions.recordHar.urlFilter;
|
||||
formatter.add(` await page.routeFromHAR(${quote(options.contextOptions.recordHar.path)}${url ? `, ${formatOptions({ url }, false)}` : ''});`);
|
||||
}
|
||||
return formatter.format();
|
||||
}
|
||||
|
||||
|
|
|
@ -137,6 +137,7 @@ export class PythonLanguageGenerator implements LanguageGenerator {
|
|||
|
||||
generateHeader(options: LanguageGeneratorOptions): string {
|
||||
const formatter = new PythonFormatter();
|
||||
const recordHar = options.contextOptions.recordHar;
|
||||
if (this._isPyTest) {
|
||||
const contextOptions = formatContextOptions(options.contextOptions, options.deviceName, true /* asDict */);
|
||||
const fixture = contextOptions ? `
|
||||
|
@ -146,13 +147,13 @@ def browser_context_args(browser_context_args, playwright) {
|
|||
return {${contextOptions}}
|
||||
}
|
||||
` : '';
|
||||
formatter.add(`${options.deviceName ? 'import pytest\n' : ''}import re
|
||||
formatter.add(`${options.deviceName || contextOptions ? 'import pytest\n' : ''}import re
|
||||
from playwright.sync_api import Page, expect
|
||||
${fixture}
|
||||
|
||||
def test_example(page: Page) -> None {`);
|
||||
if (options.contextOptions.recordHar)
|
||||
formatter.add(` page.route_from_har(${quote(options.contextOptions.recordHar.path)})`);
|
||||
if (recordHar)
|
||||
formatter.add(` page.route_from_har(${quote(recordHar.path)}${typeof recordHar.urlFilter === 'string' ? `, url=${quote(recordHar.urlFilter)}` : ''})`);
|
||||
} else if (this._isAsync) {
|
||||
formatter.add(`
|
||||
import asyncio
|
||||
|
@ -163,8 +164,8 @@ from playwright.async_api import Playwright, async_playwright, expect
|
|||
async def run(playwright: Playwright) -> None {
|
||||
browser = await playwright.${options.browserName}.launch(${formatOptions(options.launchOptions, false)})
|
||||
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)})`);
|
||||
if (recordHar)
|
||||
formatter.add(` await context.route_from_har(${quote(recordHar.path)}${typeof recordHar.urlFilter === 'string' ? `, url=${quote(recordHar.urlFilter)}` : ''})`);
|
||||
} else {
|
||||
formatter.add(`
|
||||
import re
|
||||
|
@ -174,8 +175,8 @@ from playwright.sync_api import Playwright, sync_playwright, expect
|
|||
def run(playwright: Playwright) -> None {
|
||||
browser = playwright.${options.browserName}.launch(${formatOptions(options.launchOptions, false)})
|
||||
context = browser.new_context(${formatContextOptions(options.contextOptions, options.deviceName)})`);
|
||||
if (options.contextOptions.recordHar)
|
||||
formatter.add(` context.route_from_har(${quote(options.contextOptions.recordHar.path)})`);
|
||||
if (recordHar)
|
||||
formatter.add(` context.route_from_har(${quote(recordHar.path)}${typeof recordHar.urlFilter === 'string' ? `, url=${quote(recordHar.urlFilter)}` : ''})`);
|
||||
}
|
||||
return formatter.format();
|
||||
}
|
||||
|
|
|
@ -179,6 +179,20 @@ test('should work with --save-har', async ({ runCLI }, testInfo) => {
|
|||
expect(json.log.creator.name).toBe('Playwright');
|
||||
});
|
||||
|
||||
test('should work with --save-har and --save-har-glob', async ({ runCLI }, testInfo) => {
|
||||
const harFileName = testInfo.outputPath('har.har');
|
||||
const expectedResult = `await context.RouteFromHARAsync(${JSON.stringify(harFileName)}, new BrowserContextRouteFromHAROptions
|
||||
{
|
||||
Url = "**/*.js",
|
||||
});`;
|
||||
const cli = runCLI(['--target=csharp', `--save-har=${harFileName}`, '--save-har-glob=**/*.js'], {
|
||||
autoExitWhen: expectedResult,
|
||||
});
|
||||
await cli.waitForCleanExit();
|
||||
const json = JSON.parse(fs.readFileSync(harFileName, 'utf-8'));
|
||||
expect(json.log.creator.name).toBe('Playwright');
|
||||
});
|
||||
|
||||
for (const testFramework of ['nunit', 'mstest'] as const) {
|
||||
test(`should not print context options method override in ${testFramework} if no options were passed`, async ({ runCLI }) => {
|
||||
const cli = runCLI([`--target=csharp-${testFramework}`, emptyHTML]);
|
||||
|
@ -201,7 +215,7 @@ 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(${JSON.stringify(harFileName)});`;
|
||||
const expectedResult = `await Context.RouteFromHARAsync(${JSON.stringify(harFileName)});`;
|
||||
const cli = runCLI([`--target=csharp-${testFramework}`, `--save-har=${harFileName}`], {
|
||||
autoExitWhen: expectedResult,
|
||||
});
|
||||
|
@ -209,6 +223,20 @@ for (const testFramework of ['nunit', 'mstest'] as const) {
|
|||
const json = JSON.parse(fs.readFileSync(harFileName, 'utf-8'));
|
||||
expect(json.log.creator.name).toBe('Playwright');
|
||||
});
|
||||
|
||||
test(`should work with --save-har and --save-har-glob in ${testFramework}`, async ({ runCLI }, testInfo) => {
|
||||
const harFileName = testInfo.outputPath('har.har');
|
||||
const expectedResult = `await Context.RouteFromHARAsync(${JSON.stringify(harFileName)}, new BrowserContextRouteFromHAROptions
|
||||
{
|
||||
Url = "**/*.js",
|
||||
});`;
|
||||
const cli = runCLI([`--target=csharp-${testFramework}`, `--save-har=${harFileName}`, '--save-har-glob=**/*.js'], {
|
||||
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 }) => {
|
||||
|
|
|
@ -89,10 +89,24 @@ test('should print load/save storage_state', async ({ runCLI, browserName }, tes
|
|||
await cli.waitFor(expectedResult2);
|
||||
});
|
||||
|
||||
test('should work with --save-har', async ({ runCLI }, testInfo) => {
|
||||
test('should work with --save-har and --save-har-glob as java-library', async ({ runCLI }, testInfo) => {
|
||||
const harFileName = testInfo.outputPath('har.har');
|
||||
const expectedResult = `context.routeFromHAR(${JSON.stringify(harFileName)});`;
|
||||
const cli = runCLI(['--target=java', `--save-har=${harFileName}`], {
|
||||
const expectedResult = `context.routeFromHAR(Paths.get(${JSON.stringify(harFileName)}), new BrowserContext.RouteFromHAROptions()
|
||||
.setUrl("**/*.js"));`;
|
||||
const cli = runCLI(['--target=java', `--save-har=${harFileName}`, '--save-har-glob=**/*.js'], {
|
||||
autoExitWhen: expectedResult,
|
||||
});
|
||||
|
||||
await cli.waitForCleanExit();
|
||||
const json = JSON.parse(fs.readFileSync(harFileName, 'utf-8'));
|
||||
expect(json.log.creator.name).toBe('Playwright');
|
||||
});
|
||||
|
||||
test('should work with --save-har and --save-har-glob as java-junit', async ({ runCLI }, testInfo) => {
|
||||
const harFileName = testInfo.outputPath('har.har');
|
||||
const expectedResult = `page.routeFromHAR(Paths.get(${JSON.stringify(harFileName)}), new Page.RouteFromHAROptions()
|
||||
.setUrl("**/*.js"));`;
|
||||
const cli = runCLI(['--target=java-junit', `--save-har=${harFileName}`, '--save-har-glob=**/*.js'], {
|
||||
autoExitWhen: expectedResult,
|
||||
});
|
||||
|
||||
|
|
|
@ -69,3 +69,25 @@ def test_example(page: Page) -> None:
|
|||
page.goto("${emptyHTML}")
|
||||
`);
|
||||
});
|
||||
|
||||
test('should work with --save-har', async ({ runCLI }, testInfo) => {
|
||||
const harFileName = testInfo.outputPath('har.har');
|
||||
const expectedResult = `page.route_from_har(${JSON.stringify(harFileName)})`;
|
||||
const cli = runCLI(['--target=python-pytest', `--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 work with --save-har and --save-har-glob', async ({ runCLI }, testInfo) => {
|
||||
const harFileName = testInfo.outputPath('har.har');
|
||||
const expectedResult = `page.route_from_har(${JSON.stringify(harFileName)}, url="**/*.js")`;
|
||||
const cli = runCLI(['--target=python-pytest', `--save-har=${harFileName}`, '--save-har-glob=**/*.js'], {
|
||||
autoExitWhen: expectedResult,
|
||||
});
|
||||
await cli.waitForCleanExit();
|
||||
const json = JSON.parse(fs.readFileSync(harFileName, 'utf-8'));
|
||||
expect(json.log.creator.name).toBe('Playwright');
|
||||
});
|
||||
|
|
|
@ -146,7 +146,7 @@ asyncio.run(main())
|
|||
|
||||
test('should work with --save-har', async ({ runCLI }, testInfo) => {
|
||||
const harFileName = testInfo.outputPath('har.har');
|
||||
const expectedResult = `await page.route_from_har(${JSON.stringify(harFileName)})`;
|
||||
const expectedResult = `await context.route_from_har(${JSON.stringify(harFileName)})`;
|
||||
const cli = runCLI(['--target=python-async', `--save-har=${harFileName}`], {
|
||||
autoExitWhen: expectedResult,
|
||||
});
|
||||
|
@ -154,3 +154,14 @@ test('should work with --save-har', async ({ runCLI }, testInfo) => {
|
|||
const json = JSON.parse(fs.readFileSync(harFileName, 'utf-8'));
|
||||
expect(json.log.creator.name).toBe('Playwright');
|
||||
});
|
||||
|
||||
test('should work with --save-har and --save-har-glob', async ({ runCLI }, testInfo) => {
|
||||
const harFileName = testInfo.outputPath('har.har');
|
||||
const expectedResult = `await context.route_from_har(${JSON.stringify(harFileName)}, url="**/*.js")`;
|
||||
const cli = runCLI(['--target=python-async', `--save-har=${harFileName}`, '--save-har-glob=**/*.js'], {
|
||||
autoExitWhen: expectedResult,
|
||||
});
|
||||
await cli.waitForCleanExit();
|
||||
const json = JSON.parse(fs.readFileSync(harFileName, 'utf-8'));
|
||||
expect(json.log.creator.name).toBe('Playwright');
|
||||
});
|
||||
|
|
|
@ -129,3 +129,25 @@ with sync_playwright() as playwright:
|
|||
`;
|
||||
await cli.waitFor(expectedResult2);
|
||||
});
|
||||
|
||||
test('should work with --save-har', async ({ runCLI }, testInfo) => {
|
||||
const harFileName = testInfo.outputPath('har.har');
|
||||
const expectedResult = `context.route_from_har(${JSON.stringify(harFileName)})`;
|
||||
const cli = runCLI(['--target=python-async', `--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 work with --save-har and --save-har-glob', async ({ runCLI }, testInfo) => {
|
||||
const harFileName = testInfo.outputPath('har.har');
|
||||
const expectedResult = `context.route_from_har(${JSON.stringify(harFileName)}, url="**/*.js")`;
|
||||
const cli = runCLI(['--target=python-async', `--save-har=${harFileName}`, '--save-har-glob=**/*.js'], {
|
||||
autoExitWhen: expectedResult,
|
||||
});
|
||||
await cli.waitForCleanExit();
|
||||
const json = JSON.parse(fs.readFileSync(harFileName, 'utf-8'));
|
||||
expect(json.log.creator.name).toBe('Playwright');
|
||||
});
|
||||
|
|
|
@ -108,3 +108,18 @@ test('should generate routeFromHAR with --save-har', async ({ runCLI }, testInfo
|
|||
const json = JSON.parse(fs.readFileSync(harFileName, 'utf-8'));
|
||||
expect(json.log.creator.name).toBe('Playwright');
|
||||
});
|
||||
|
||||
test('should generate routeFromHAR with --save-har and --save-har-glob', async ({ runCLI }, testInfo) => {
|
||||
const harFileName = testInfo.outputPath('har.har');
|
||||
const expectedResult = `test('test', async ({ page }) => {
|
||||
await page.routeFromHAR('${harFileName.replace(/\\/g, '\\\\')}', {
|
||||
url: '**/*.js'
|
||||
});
|
||||
});`;
|
||||
const cli = runCLI(['--target=playwright-test', `--save-har=${harFileName}`, '--save-har-glob=**/*.js'], {
|
||||
autoExitWhen: expectedResult,
|
||||
});
|
||||
await cli.waitForCleanExit();
|
||||
const json = JSON.parse(fs.readFileSync(harFileName, 'utf-8'));
|
||||
expect(json.log.creator.name).toBe('Playwright');
|
||||
});
|
Loading…
Reference in New Issue