fix: update codegen to produce set* instead of with* (#5738)

This commit is contained in:
Yury Semikhatsky 2021-03-05 14:05:48 -08:00 committed by GitHub
parent 70beef83d0
commit 976f35aaf7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 31 deletions

View file

@ -71,7 +71,7 @@ export class JavaLanguageGenerator implements LanguageGenerator {
if (signals.waitForNavigation) { if (signals.waitForNavigation) {
code = ` code = `
// ${pageAlias}.waitForNavigation(new Page.WaitForNavigationOptions().withUrl(${quote(signals.waitForNavigation.url)}), () -> // ${pageAlias}.waitForNavigation(new Page.WaitForNavigationOptions().setUrl(${quote(signals.waitForNavigation.url)}), () ->
${pageAlias}.waitForNavigation(() -> { ${pageAlias}.waitForNavigation(() -> {
${code} ${code}
});`; });`;
@ -131,7 +131,7 @@ export class JavaLanguageGenerator implements LanguageGenerator {
} }
generateFooter(saveStorage: string | undefined): string { generateFooter(saveStorage: string | undefined): string {
const storageStateLine = saveStorage ? `\n context.storageState(new BrowserContext.StorageStateOptions().withPath(${quote(saveStorage)}));` : ''; const storageStateLine = saveStorage ? `\n context.storageState(new BrowserContext.StorageStateOptions().setPath(${quote(saveStorage)}));` : '';
return `\n // ---------------------${storageStateLine} return `\n // ---------------------${storageStateLine}
} }
} }
@ -163,7 +163,7 @@ function formatLaunchOptions(options: any): string {
return ''; return '';
lines.push('new BrowserType.LaunchOptions()'); lines.push('new BrowserType.LaunchOptions()');
if (typeof options.headless === 'boolean') if (typeof options.headless === 'boolean')
lines.push(` .withHeadless(false)`); lines.push(` .setHeadless(false)`);
return lines.join('\n'); return lines.join('\n');
} }
@ -175,27 +175,27 @@ function formatContextOptions(contextOptions: BrowserContextOptions, deviceName:
const options: BrowserContextOptions = { ...device, ...contextOptions }; const options: BrowserContextOptions = { ...device, ...contextOptions };
lines.push('new Browser.NewContextOptions()'); lines.push('new Browser.NewContextOptions()');
if (options.colorScheme) if (options.colorScheme)
lines.push(` .withColorScheme(ColorScheme.${options.colorScheme.toUpperCase()})`); lines.push(` .setColorScheme(ColorScheme.${options.colorScheme.toUpperCase()})`);
if (options.geolocation) if (options.geolocation)
lines.push(` .withGeolocation(${options.geolocation.latitude}, ${options.geolocation.longitude})`); lines.push(` .setGeolocation(${options.geolocation.latitude}, ${options.geolocation.longitude})`);
if (options.locale) if (options.locale)
lines.push(` .withLocale("${options.locale}")`); lines.push(` .setLocale("${options.locale}")`);
if (options.proxy) if (options.proxy)
lines.push(` .withProxy(new Proxy("${options.proxy.server}"))`); lines.push(` .setProxy(new Proxy("${options.proxy.server}"))`);
if (options.timezoneId) if (options.timezoneId)
lines.push(` .withTimezoneId("${options.timezoneId}")`); lines.push(` .setTimezoneId("${options.timezoneId}")`);
if (options.userAgent) if (options.userAgent)
lines.push(` .withUserAgent("${options.userAgent}")`); lines.push(` .setUserAgent("${options.userAgent}")`);
if (options.viewport) if (options.viewport)
lines.push(` .withViewportSize(${options.viewport.width}, ${options.viewport.height})`); lines.push(` .setViewportSize(${options.viewport.width}, ${options.viewport.height})`);
if (options.deviceScaleFactor) if (options.deviceScaleFactor)
lines.push(` .withDeviceScaleFactor(${options.deviceScaleFactor})`); lines.push(` .setDeviceScaleFactor(${options.deviceScaleFactor})`);
if (options.isMobile) if (options.isMobile)
lines.push(` .withIsMobile(${options.isMobile})`); lines.push(` .setIsMobile(${options.isMobile})`);
if (options.hasTouch) if (options.hasTouch)
lines.push(` .withHasTouch(${options.hasTouch})`); lines.push(` .setHasTouch(${options.hasTouch})`);
if (options.storageState) if (options.storageState)
lines.push(` .withStorageStatePath(Paths.get(${quote(options.storageState as string)}))`); lines.push(` .setStorageStatePath(Paths.get(${quote(options.storageState as string)}))`);
return lines.join('\n'); return lines.join('\n');
} }

View file

@ -227,7 +227,7 @@ export class JavaScriptFormatter {
const extraSpaces = /^(for|while|if|try).*\(.*\)$/.test(previousLine) ? this._baseIndent : ''; const extraSpaces = /^(for|while|if|try).*\(.*\)$/.test(previousLine) ? this._baseIndent : '';
previousLine = line; previousLine = line;
const callCarryOver = line.startsWith('.with'); const callCarryOver = line.startsWith('.set');
line = spaces + extraSpaces + (callCarryOver ? this._baseIndent : '') + line; line = spaces + extraSpaces + (callCarryOver ? this._baseIndent : '') + line;
if (line.endsWith('{') || line.endsWith('[')) if (line.endsWith('{') || line.endsWith('['))
spaces += this._baseIndent; spaces += this._baseIndent;

View file

@ -552,7 +552,7 @@ await page.ClickAsync(\"text=link\");
expect(sources.get('<java>').text).toContain(` expect(sources.get('<java>').text).toContain(`
// Click text=link // Click text=link
// page.waitForNavigation(new Page.WaitForNavigationOptions().withUrl("about:blank#foo"), () -> // page.waitForNavigation(new Page.WaitForNavigationOptions().setUrl("about:blank#foo"), () ->
page.waitForNavigation(() -> { page.waitForNavigation(() -> {
page.click("text=link"); page.click("text=link");
});`); });`);

View file

@ -31,7 +31,7 @@ public class Example {
public static void main(String[] args) { public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) { try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.${browserName}().launch(new BrowserType.LaunchOptions() Browser browser = playwright.${browserName}().launch(new BrowserType.LaunchOptions()
.withHeadless(false)); .setHeadless(false));
BrowserContext context = browser.newContext();`; BrowserContext context = browser.newContext();`;
await cli.waitFor(expectedResult); await cli.waitFor(expectedResult);
expect(cli.text()).toContain(expectedResult); expect(cli.text()).toContain(expectedResult);
@ -40,7 +40,7 @@ public class Example {
it('should print the correct context options for custom settings', async ({ runCLI, browserName }) => { it('should print the correct context options for custom settings', async ({ runCLI, browserName }) => {
const cli = runCLI(['codegen', '--color-scheme=light', '--target=java', emptyHTML]); const cli = runCLI(['codegen', '--color-scheme=light', '--target=java', emptyHTML]);
const expectedResult = `BrowserContext context = browser.newContext(new Browser.NewContextOptions() const expectedResult = `BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.withColorScheme(ColorScheme.LIGHT));`; .setColorScheme(ColorScheme.LIGHT));`;
await cli.waitFor(expectedResult); await cli.waitFor(expectedResult);
expect(cli.text()).toContain(expectedResult); expect(cli.text()).toContain(expectedResult);
}); });
@ -48,11 +48,11 @@ it('should print the correct context options for custom settings', async ({ runC
it('should print the correct context options when using a device', async ({ runCLI }) => { it('should print the correct context options when using a device', async ({ runCLI }) => {
const cli = runCLI(['codegen', '--device=Pixel 2', '--target=java', emptyHTML]); const cli = runCLI(['codegen', '--device=Pixel 2', '--target=java', emptyHTML]);
const expectedResult = `BrowserContext context = browser.newContext(new Browser.NewContextOptions() const expectedResult = `BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.withUserAgent("Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36") .setUserAgent("Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36")
.withViewportSize(411, 731) .setViewportSize(411, 731)
.withDeviceScaleFactor(2.625) .setDeviceScaleFactor(2.625)
.withIsMobile(true) .setIsMobile(true)
.withHasTouch(true));`; .setHasTouch(true));`;
await cli.waitFor(expectedResult); await cli.waitFor(expectedResult);
expect(cli.text()).toContain(expectedResult); expect(cli.text()).toContain(expectedResult);
}); });
@ -60,12 +60,12 @@ it('should print the correct context options when using a device', async ({ runC
it('should print the correct context options when using a device and additional options', async ({ runCLI }) => { it('should print the correct context options when using a device and additional options', async ({ runCLI }) => {
const cli = runCLI(['codegen', '--color-scheme=light', '--device=iPhone 11', '--target=java', emptyHTML]); const cli = runCLI(['codegen', '--color-scheme=light', '--device=iPhone 11', '--target=java', emptyHTML]);
const expectedResult = `BrowserContext context = browser.newContext(new Browser.NewContextOptions() const expectedResult = `BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.withColorScheme(ColorScheme.LIGHT) .setColorScheme(ColorScheme.LIGHT)
.withUserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Mobile/15E148 Safari/604.1") .setUserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Mobile/15E148 Safari/604.1")
.withViewportSize(414, 896) .setViewportSize(414, 896)
.withDeviceScaleFactor(2) .setDeviceScaleFactor(2)
.withIsMobile(true) .setIsMobile(true)
.withHasTouch(true));`; .setHasTouch(true));`;
await cli.waitFor(expectedResult); await cli.waitFor(expectedResult);
expect(cli.text()).toContain(expectedResult); expect(cli.text()).toContain(expectedResult);
}); });
@ -76,11 +76,11 @@ it('should print load/save storage_state', async ({ runCLI, browserName, testInf
await fs.promises.writeFile(loadFileName, JSON.stringify({ cookies: [], origins: [] }), 'utf8'); await fs.promises.writeFile(loadFileName, JSON.stringify({ cookies: [], origins: [] }), 'utf8');
const cli = runCLI(['codegen', `--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, '--target=java', emptyHTML]); const cli = runCLI(['codegen', `--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, '--target=java', emptyHTML]);
const expectedResult1 = `BrowserContext context = browser.newContext(new Browser.NewContextOptions() const expectedResult1 = `BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.withStorageStatePath(Paths.get("${loadFileName}")));`; .setStorageStatePath(Paths.get("${loadFileName}")));`;
await cli.waitFor(expectedResult1); await cli.waitFor(expectedResult1);
const expectedResult2 = ` const expectedResult2 = `
// --------------------- // ---------------------
context.storageState(new BrowserContext.StorageStateOptions().withPath("${saveFileName}"))`; context.storageState(new BrowserContext.StorageStateOptions().setPath("${saveFileName}"))`;
await cli.waitFor(expectedResult2); await cli.waitFor(expectedResult2);
}); });