Skip to content

Commit

Permalink
fix: update codegen to produce set* instead of with* (#5738) (#5740)
Browse files Browse the repository at this point in the history
  • Loading branch information
yury-s authored Mar 5, 2021
1 parent 07438f6 commit 3fcc57c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
28 changes: 14 additions & 14 deletions src/server/supplements/recorder/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class JavaLanguageGenerator implements LanguageGenerator {

if (signals.waitForNavigation) {
code = `
// ${pageAlias}.waitForNavigation(new Page.WaitForNavigationOptions().withUrl(${quote(signals.waitForNavigation.url)}), () ->
// ${pageAlias}.waitForNavigation(new Page.WaitForNavigationOptions().setUrl(${quote(signals.waitForNavigation.url)}), () ->
${pageAlias}.waitForNavigation(() -> {
${code}
});`;
Expand Down Expand Up @@ -131,7 +131,7 @@ export class JavaLanguageGenerator implements LanguageGenerator {
}

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}
}
}
Expand Down Expand Up @@ -163,7 +163,7 @@ function formatLaunchOptions(options: any): string {
return '';
lines.push('new BrowserType.LaunchOptions()');
if (typeof options.headless === 'boolean')
lines.push(` .withHeadless(false)`);
lines.push(` .setHeadless(false)`);
return lines.join('\n');
}

Expand All @@ -175,27 +175,27 @@ function formatContextOptions(contextOptions: BrowserContextOptions, deviceName:
const options: BrowserContextOptions = { ...device, ...contextOptions };
lines.push('new Browser.NewContextOptions()');
if (options.colorScheme)
lines.push(` .withColorScheme(ColorScheme.${options.colorScheme.toUpperCase()})`);
lines.push(` .setColorScheme(ColorScheme.${options.colorScheme.toUpperCase()})`);
if (options.geolocation)
lines.push(` .withGeolocation(${options.geolocation.latitude}, ${options.geolocation.longitude})`);
lines.push(` .setGeolocation(${options.geolocation.latitude}, ${options.geolocation.longitude})`);
if (options.locale)
lines.push(` .withLocale("${options.locale}")`);
lines.push(` .setLocale("${options.locale}")`);
if (options.proxy)
lines.push(` .withProxy(new Proxy("${options.proxy.server}"))`);
lines.push(` .setProxy(new Proxy("${options.proxy.server}"))`);
if (options.timezoneId)
lines.push(` .withTimezoneId("${options.timezoneId}")`);
lines.push(` .setTimezoneId("${options.timezoneId}")`);
if (options.userAgent)
lines.push(` .withUserAgent("${options.userAgent}")`);
lines.push(` .setUserAgent("${options.userAgent}")`);
if (options.viewport)
lines.push(` .withViewportSize(${options.viewport.width}, ${options.viewport.height})`);
lines.push(` .setViewportSize(${options.viewport.width}, ${options.viewport.height})`);
if (options.deviceScaleFactor)
lines.push(` .withDeviceScaleFactor(${options.deviceScaleFactor})`);
lines.push(` .setDeviceScaleFactor(${options.deviceScaleFactor})`);
if (options.isMobile)
lines.push(` .withIsMobile(${options.isMobile})`);
lines.push(` .setIsMobile(${options.isMobile})`);
if (options.hasTouch)
lines.push(` .withHasTouch(${options.hasTouch})`);
lines.push(` .setHasTouch(${options.hasTouch})`);
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');
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/supplements/recorder/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export class JavaScriptFormatter {
const extraSpaces = /^(for|while|if|try).*\(.*\)$/.test(previousLine) ? this._baseIndent : '';
previousLine = line;

const callCarryOver = line.startsWith('.with');
const callCarryOver = line.startsWith('.set');
line = spaces + extraSpaces + (callCarryOver ? this._baseIndent : '') + line;
if (line.endsWith('{') || line.endsWith('['))
spaces += this._baseIndent;
Expand Down
2 changes: 1 addition & 1 deletion test/cli/cli-codegen-1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ await page.ClickAsync(\"text=link\");

expect(sources.get('<java>').text).toContain(`
// Click text=link
// page.waitForNavigation(new Page.WaitForNavigationOptions().withUrl("about:blank#foo"), () ->
// page.waitForNavigation(new Page.WaitForNavigationOptions().setUrl("about:blank#foo"), () ->
page.waitForNavigation(() -> {
page.click("text=link");
});`);
Expand Down
30 changes: 15 additions & 15 deletions test/cli/cli-codegen-java.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.${browserName}().launch(new BrowserType.LaunchOptions()
.withHeadless(false));
.setHeadless(false));
BrowserContext context = browser.newContext();`;
await cli.waitFor(expectedResult);
expect(cli.text()).toContain(expectedResult);
Expand All @@ -40,32 +40,32 @@ public class Example {
it('should print the correct context options for custom settings', async ({ runCLI, browserName }) => {
const cli = runCLI(['--color-scheme=light', 'codegen', '--target=java', emptyHTML]);
const expectedResult = `BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.withColorScheme(ColorScheme.LIGHT));`;
.setColorScheme(ColorScheme.LIGHT));`;
await cli.waitFor(expectedResult);
expect(cli.text()).toContain(expectedResult);
});

it('should print the correct context options when using a device', async ({ runCLI }) => {
const cli = runCLI(['--device=Pixel 2', 'codegen', '--target=java', emptyHTML]);
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")
.withViewportSize(411, 731)
.withDeviceScaleFactor(2.625)
.withIsMobile(true)
.withHasTouch(true));`;
.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")
.setViewportSize(411, 731)
.setDeviceScaleFactor(2.625)
.setIsMobile(true)
.setHasTouch(true));`;
await cli.waitFor(expectedResult);
expect(cli.text()).toContain(expectedResult);
});

it('should print the correct context options when using a device and additional options', async ({ runCLI }) => {
const cli = runCLI(['--color-scheme=light', '--device=iPhone 11', 'codegen', '--target=java', emptyHTML]);
const expectedResult = `BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.withColorScheme(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")
.withViewportSize(414, 896)
.withDeviceScaleFactor(2)
.withIsMobile(true)
.withHasTouch(true));`;
.setColorScheme(ColorScheme.LIGHT)
.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")
.setViewportSize(414, 896)
.setDeviceScaleFactor(2)
.setIsMobile(true)
.setHasTouch(true));`;
await cli.waitFor(expectedResult);
expect(cli.text()).toContain(expectedResult);
});
Expand All @@ -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');
const cli = runCLI([`--load-storage=${loadFileName}`, `--save-storage=${saveFileName}`, 'codegen', '--target=java', emptyHTML]);
const expectedResult1 = `BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.withStorageStatePath(Paths.get("${loadFileName}")));`;
.setStorageStatePath(Paths.get("${loadFileName}")));`;
await cli.waitFor(expectedResult1);

const expectedResult2 = `
// ---------------------
context.storageState(new BrowserContext.StorageStateOptions().withPath("${saveFileName}"))`;
context.storageState(new BrowserContext.StorageStateOptions().setPath("${saveFileName}"))`;
await cli.waitFor(expectedResult2);
});

0 comments on commit 3fcc57c

Please sign in to comment.