fix(codegen): generate all options for java (#6698)
This commit is contained in:
parent
f529f0a25d
commit
af2fec6bcf
|
|
@ -348,7 +348,7 @@ async function openPage(context: BrowserContext, url: string | undefined): Promi
|
||||||
if (url) {
|
if (url) {
|
||||||
if (fs.existsSync(url))
|
if (fs.existsSync(url))
|
||||||
url = 'file://' + path.resolve(url);
|
url = 'file://' + path.resolve(url);
|
||||||
else if (!url.startsWith('http') && !url.startsWith('file://') && !url.startsWith('about:'))
|
else if (!url.startsWith('http') && !url.startsWith('file://') && !url.startsWith('about:') && !url.startsWith('data:'))
|
||||||
url = 'http://' + url;
|
url = 'http://' + url;
|
||||||
await page.goto(url);
|
await page.goto(url);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ import type { BrowserContextOptions } from '../../../..';
|
||||||
import { LanguageGenerator, LanguageGeneratorOptions, toSignalMap } from './language';
|
import { LanguageGenerator, LanguageGeneratorOptions, toSignalMap } from './language';
|
||||||
import { ActionInContext } from './codeGenerator';
|
import { ActionInContext } from './codeGenerator';
|
||||||
import { Action, actionTitle } from './recorderActions';
|
import { Action, actionTitle } from './recorderActions';
|
||||||
import { toModifiers } from './utils';
|
import { MouseClickOptions, toModifiers } from './utils';
|
||||||
import deviceDescriptors from '../../deviceDescriptors';
|
import deviceDescriptors from '../../deviceDescriptors';
|
||||||
import { JavaScriptFormatter } from './javascript';
|
import { JavaScriptFormatter } from './javascript';
|
||||||
|
|
||||||
|
|
@ -54,7 +54,7 @@ export class JavaLanguageGenerator implements LanguageGenerator {
|
||||||
});`);
|
});`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const actionCall = this._generateActionCall(action);
|
const actionCall = this._generateActionCall(action, actionInContext.isMainFrame);
|
||||||
let code = `${subject}.${actionCall};`;
|
let code = `${subject}.${actionCall};`;
|
||||||
|
|
||||||
if (signals.popup) {
|
if (signals.popup) {
|
||||||
|
|
@ -84,7 +84,7 @@ export class JavaLanguageGenerator implements LanguageGenerator {
|
||||||
return formatter.format();
|
return formatter.format();
|
||||||
}
|
}
|
||||||
|
|
||||||
private _generateActionCall(action: Action): string {
|
private _generateActionCall(action: Action, isPage: boolean): string {
|
||||||
switch (action.name) {
|
switch (action.name) {
|
||||||
case 'openPage':
|
case 'openPage':
|
||||||
throw Error('Not reached');
|
throw Error('Not reached');
|
||||||
|
|
@ -94,7 +94,16 @@ export class JavaLanguageGenerator implements LanguageGenerator {
|
||||||
let method = 'click';
|
let method = 'click';
|
||||||
if (action.clickCount === 2)
|
if (action.clickCount === 2)
|
||||||
method = 'dblclick';
|
method = 'dblclick';
|
||||||
return `${method}(${quote(action.selector)})`;
|
const modifiers = toModifiers(action.modifiers);
|
||||||
|
const options: MouseClickOptions = {};
|
||||||
|
if (action.button !== 'left')
|
||||||
|
options.button = action.button;
|
||||||
|
if (modifiers.length)
|
||||||
|
options.modifiers = modifiers;
|
||||||
|
if (action.clickCount > 2)
|
||||||
|
options.clickCount = action.clickCount;
|
||||||
|
const optionsText = formatClickOptions(options, isPage);
|
||||||
|
return `${method}(${quote(action.selector)}${optionsText ? ', ' : ''}${optionsText})`;
|
||||||
}
|
}
|
||||||
case 'check':
|
case 'check':
|
||||||
return `check(${quote(action.selector)})`;
|
return `check(${quote(action.selector)})`;
|
||||||
|
|
@ -121,6 +130,7 @@ export class JavaLanguageGenerator implements LanguageGenerator {
|
||||||
formatter.add(`
|
formatter.add(`
|
||||||
import com.microsoft.playwright.*;
|
import com.microsoft.playwright.*;
|
||||||
import com.microsoft.playwright.options.*;
|
import com.microsoft.playwright.options.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
public class Example {
|
public class Example {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
@ -131,9 +141,8 @@ export class JavaLanguageGenerator implements LanguageGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
generateFooter(saveStorage: string | undefined): string {
|
generateFooter(saveStorage: string | undefined): string {
|
||||||
const storageStateLine = saveStorage ? `\n context.storageState(new BrowserContext.StorageStateOptions().setPath(${quote(saveStorage)}));` : '';
|
const storageStateLine = saveStorage ? `\n context.storageState(new BrowserContext.StorageStateOptions().setPath(${quote(saveStorage)}));\n` : '';
|
||||||
return `\n // ---------------------${storageStateLine}
|
return `${storageStateLine} }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}`;
|
}`;
|
||||||
}
|
}
|
||||||
|
|
@ -176,29 +185,46 @@ function formatContextOptions(contextOptions: BrowserContextOptions, deviceName:
|
||||||
const device = deviceName ? deviceDescriptors[deviceName] : {};
|
const device = deviceName ? deviceDescriptors[deviceName] : {};
|
||||||
const options: BrowserContextOptions = { ...device, ...contextOptions };
|
const options: BrowserContextOptions = { ...device, ...contextOptions };
|
||||||
lines.push('new Browser.NewContextOptions()');
|
lines.push('new Browser.NewContextOptions()');
|
||||||
|
if (options.acceptDownloads)
|
||||||
|
lines.push(` .setAcceptDownloads(true)`);
|
||||||
|
if (options.bypassCSP)
|
||||||
|
lines.push(` .setBypassCSP(true)`);
|
||||||
if (options.colorScheme)
|
if (options.colorScheme)
|
||||||
lines.push(` .setColorScheme(ColorScheme.${options.colorScheme.toUpperCase()})`);
|
lines.push(` .setColorScheme(ColorScheme.${options.colorScheme.toUpperCase()})`);
|
||||||
|
if (options.deviceScaleFactor)
|
||||||
|
lines.push(` .setDeviceScaleFactor(${options.deviceScaleFactor})`);
|
||||||
if (options.geolocation)
|
if (options.geolocation)
|
||||||
lines.push(` .setGeolocation(${options.geolocation.latitude}, ${options.geolocation.longitude})`);
|
lines.push(` .setGeolocation(${options.geolocation.latitude}, ${options.geolocation.longitude})`);
|
||||||
|
if (options.hasTouch)
|
||||||
|
lines.push(` .setHasTouch(${options.hasTouch})`);
|
||||||
|
if (options.isMobile)
|
||||||
|
lines.push(` .setIsMobile(${options.isMobile})`);
|
||||||
if (options.locale)
|
if (options.locale)
|
||||||
lines.push(` .setLocale("${options.locale}")`);
|
lines.push(` .setLocale("${options.locale}")`);
|
||||||
if (options.proxy)
|
if (options.proxy)
|
||||||
lines.push(` .setProxy(new Proxy("${options.proxy.server}"))`);
|
lines.push(` .setProxy(new Proxy("${options.proxy.server}"))`);
|
||||||
|
if (options.storageState)
|
||||||
|
lines.push(` .setStorageStatePath(Paths.get(${quote(options.storageState as string)}))`);
|
||||||
if (options.timezoneId)
|
if (options.timezoneId)
|
||||||
lines.push(` .setTimezoneId("${options.timezoneId}")`);
|
lines.push(` .setTimezoneId("${options.timezoneId}")`);
|
||||||
if (options.userAgent)
|
if (options.userAgent)
|
||||||
lines.push(` .setUserAgent("${options.userAgent}")`);
|
lines.push(` .setUserAgent("${options.userAgent}")`);
|
||||||
if (options.viewport)
|
if (options.viewport)
|
||||||
lines.push(` .setViewportSize(${options.viewport.width}, ${options.viewport.height})`);
|
lines.push(` .setViewportSize(${options.viewport.width}, ${options.viewport.height})`);
|
||||||
if (options.deviceScaleFactor)
|
return lines.join('\n');
|
||||||
lines.push(` .setDeviceScaleFactor(${options.deviceScaleFactor})`);
|
}
|
||||||
if (options.isMobile)
|
|
||||||
lines.push(` .setIsMobile(${options.isMobile})`);
|
|
||||||
if (options.hasTouch)
|
|
||||||
lines.push(` .setHasTouch(${options.hasTouch})`);
|
|
||||||
if (options.storageState)
|
|
||||||
lines.push(` .setStorageStatePath(Paths.get(${quote(options.storageState as string)}))`);
|
|
||||||
|
|
||||||
|
function formatClickOptions(options: MouseClickOptions, isPage: boolean) {
|
||||||
|
const lines = [];
|
||||||
|
if (options.button)
|
||||||
|
lines.push(` .setButton(MouseButton.${options.button.toUpperCase()})`);
|
||||||
|
if (options.modifiers)
|
||||||
|
lines.push(` .setModifiers(Arrays.asList(${options.modifiers.map(m => `KeyboardModifier.${m.toUpperCase()}`).join(', ')}))`);
|
||||||
|
if (options.clickCount)
|
||||||
|
lines.push(` .setClickCount(${options.clickCount})`);
|
||||||
|
if (!lines.length)
|
||||||
|
return '';
|
||||||
|
lines.unshift(`new ${isPage ? 'Page' : 'Frame'}.ClickOptions()`);
|
||||||
return lines.join('\n');
|
return lines.join('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -647,9 +647,9 @@ test.describe('cli codegen', () => {
|
||||||
expect(sources.get('<async python>').text).toContain(`
|
expect(sources.get('<async python>').text).toContain(`
|
||||||
await page.click("text=Click me", button="middle")`);
|
await page.click("text=Click me", button="middle")`);
|
||||||
|
|
||||||
// TODO: fix this for java
|
expect(sources.get('<java>').text).toContain(`
|
||||||
// expect(sources.get('<java>').text).toContain(`
|
page.click("text=Click me", new Page.ClickOptions()
|
||||||
// page.click("text=Click me", foo);`);
|
.setButton(MouseButton.MIDDLE));`);
|
||||||
|
|
||||||
expect(sources.get('<csharp>').text).toContain(`
|
expect(sources.get('<csharp>').text).toContain(`
|
||||||
await page.ClickAsync("text=Click me", new PageClickOptions
|
await page.ClickAsync("text=Click me", new PageClickOptions
|
||||||
|
|
|
||||||
|
|
@ -261,9 +261,9 @@ test.describe('cli codegen', () => {
|
||||||
page.click('text=Download')
|
page.click('text=Download')
|
||||||
]);`);
|
]);`);
|
||||||
|
|
||||||
// TODO: fix generated options in java.
|
|
||||||
expect(sources.get('<java>').text).toContain(`
|
expect(sources.get('<java>').text).toContain(`
|
||||||
BrowserContext context = browser.newContext(new Browser.NewContextOptions());`);
|
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
|
||||||
|
.setAcceptDownloads(true));`);
|
||||||
expect(sources.get('<java>').text).toContain(`
|
expect(sources.get('<java>').text).toContain(`
|
||||||
// Click text=Download
|
// Click text=Download
|
||||||
Download download = page.waitForDownload(() -> {
|
Download download = page.waitForDownload(() -> {
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ test('should print the correct imports and context options', async ({ runCLI, ch
|
||||||
const cli = runCLI(['--target=java', emptyHTML]);
|
const cli = runCLI(['--target=java', emptyHTML]);
|
||||||
const expectedResult = `import com.microsoft.playwright.*;
|
const expectedResult = `import com.microsoft.playwright.*;
|
||||||
import com.microsoft.playwright.options.*;
|
import com.microsoft.playwright.options.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
public class Example {
|
public class Example {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
@ -50,13 +51,13 @@ test('should print the correct context options when using a device', async ({ br
|
||||||
test.skip(browserName !== 'chromium');
|
test.skip(browserName !== 'chromium');
|
||||||
|
|
||||||
const cli = runCLI(['--device=Pixel 2', '--target=java', emptyHTML]);
|
const cli = runCLI(['--device=Pixel 2', '--target=java', emptyHTML]);
|
||||||
await cli.waitFor(`setHasTouch(true));`);
|
await cli.waitFor(`.setViewportSize(411, 731));`);
|
||||||
const expectedResult = `BrowserContext context = browser.newContext(new Browser.NewContextOptions()
|
const expectedResult = `BrowserContext context = browser.newContext(new Browser.NewContextOptions()
|
||||||
.setUserAgent("Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/XXXX Mobile Safari/537.36")
|
|
||||||
.setViewportSize(411, 731)
|
|
||||||
.setDeviceScaleFactor(2.625)
|
.setDeviceScaleFactor(2.625)
|
||||||
|
.setHasTouch(true)
|
||||||
.setIsMobile(true)
|
.setIsMobile(true)
|
||||||
.setHasTouch(true));`;
|
.setUserAgent("Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/XXXX Mobile Safari/537.36")
|
||||||
|
.setViewportSize(411, 731));`;
|
||||||
expect(cli.text().replace(/(.*Chrome\/)(.*?)( .*)/m, '$1XXXX$3')).toContain(expectedResult);
|
expect(cli.text().replace(/(.*Chrome\/)(.*?)( .*)/m, '$1XXXX$3')).toContain(expectedResult);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -66,11 +67,11 @@ test('should print the correct context options when using a device and additiona
|
||||||
const cli = runCLI(['--color-scheme=light', '--device=iPhone 11', '--target=java', emptyHTML]);
|
const cli = runCLI(['--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()
|
||||||
.setColorScheme(ColorScheme.LIGHT)
|
.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, 715)
|
|
||||||
.setDeviceScaleFactor(2)
|
.setDeviceScaleFactor(2)
|
||||||
|
.setHasTouch(true)
|
||||||
.setIsMobile(true)
|
.setIsMobile(true)
|
||||||
.setHasTouch(true));`;
|
.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, 715));`;
|
||||||
await cli.waitFor(expectedResult);
|
await cli.waitFor(expectedResult);
|
||||||
expect(cli.text()).toContain(expectedResult);
|
expect(cli.text()).toContain(expectedResult);
|
||||||
});
|
});
|
||||||
|
|
@ -85,7 +86,6 @@ test('should print load/save storage_state', async ({ runCLI, browserName }, tes
|
||||||
await cli.waitFor(expectedResult1);
|
await cli.waitFor(expectedResult1);
|
||||||
|
|
||||||
const expectedResult2 = `
|
const expectedResult2 = `
|
||||||
// ---------------------
|
|
||||||
context.storageState(new BrowserContext.StorageStateOptions().setPath("${saveFileName}"))`;
|
context.storageState(new BrowserContext.StorageStateOptions().setPath("${saveFileName}"))`;
|
||||||
await cli.waitFor(expectedResult2);
|
await cli.waitFor(expectedResult2);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue