2021-03-03 14:32:09 -08:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import type { BrowserContextOptions } from '../../../..';
|
|
|
|
import { LanguageGenerator, LanguageGeneratorOptions, toSignalMap } from './language';
|
|
|
|
import { ActionInContext } from './codeGenerator';
|
|
|
|
import { Action, actionTitle } from './recorderActions';
|
2021-08-23 12:22:19 -04:00
|
|
|
import { escapeWithQuotes, MouseClickOptions, toModifiers } from './utils';
|
2021-04-23 18:34:52 -07:00
|
|
|
import deviceDescriptors from '../../deviceDescriptors';
|
2021-03-03 14:32:09 -08:00
|
|
|
import { JavaScriptFormatter } from './javascript';
|
|
|
|
|
|
|
|
export class JavaLanguageGenerator implements LanguageGenerator {
|
|
|
|
id = 'java';
|
2021-06-10 16:52:59 -07:00
|
|
|
fileName = 'Java';
|
2021-03-03 14:32:09 -08:00
|
|
|
highlighter = 'java';
|
|
|
|
|
|
|
|
generateAction(actionInContext: ActionInContext): string {
|
|
|
|
const { action, pageAlias } = actionInContext;
|
|
|
|
const formatter = new JavaScriptFormatter(6);
|
|
|
|
formatter.newLine();
|
|
|
|
formatter.add('// ' + actionTitle(action));
|
|
|
|
|
|
|
|
if (action.name === 'openPage') {
|
|
|
|
formatter.add(`Page ${pageAlias} = context.newPage();`);
|
|
|
|
if (action.url && action.url !== 'about:blank' && action.url !== 'chrome://newtab/')
|
|
|
|
formatter.add(`${pageAlias}.navigate("${action.url}");`);
|
|
|
|
return formatter.format();
|
|
|
|
}
|
|
|
|
|
|
|
|
const subject = actionInContext.isMainFrame ? pageAlias :
|
|
|
|
(actionInContext.frameName ?
|
|
|
|
`${pageAlias}.frame(${quote(actionInContext.frameName)})` :
|
|
|
|
`${pageAlias}.frameByUrl(${quote(actionInContext.frameUrl)})`);
|
|
|
|
|
|
|
|
const signals = toSignalMap(action);
|
|
|
|
|
|
|
|
if (signals.dialog) {
|
|
|
|
formatter.add(` ${pageAlias}.onceDialog(dialog -> {
|
|
|
|
System.out.println(String.format("Dialog message: %s", dialog.message()));
|
|
|
|
dialog.dismiss();
|
|
|
|
});`);
|
|
|
|
}
|
|
|
|
|
2021-05-21 16:17:25 -07:00
|
|
|
const actionCall = this._generateActionCall(action, actionInContext.isMainFrame);
|
2021-03-03 14:32:09 -08:00
|
|
|
let code = `${subject}.${actionCall};`;
|
|
|
|
|
|
|
|
if (signals.popup) {
|
|
|
|
code = `Page ${signals.popup.popupAlias} = ${pageAlias}.waitForPopup(() -> {
|
|
|
|
${code}
|
|
|
|
});`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (signals.download) {
|
|
|
|
code = `Download download = ${pageAlias}.waitForDownload(() -> {
|
|
|
|
${code}
|
|
|
|
});`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (signals.waitForNavigation) {
|
|
|
|
code = `
|
2021-03-05 14:05:48 -08:00
|
|
|
// ${pageAlias}.waitForNavigation(new Page.WaitForNavigationOptions().setUrl(${quote(signals.waitForNavigation.url)}), () ->
|
2021-03-03 14:32:09 -08:00
|
|
|
${pageAlias}.waitForNavigation(() -> {
|
|
|
|
${code}
|
|
|
|
});`;
|
|
|
|
}
|
|
|
|
|
|
|
|
formatter.add(code);
|
|
|
|
|
|
|
|
if (signals.assertNavigation)
|
|
|
|
formatter.add(`// assert ${pageAlias}.url().equals(${quote(signals.assertNavigation.url)});`);
|
|
|
|
return formatter.format();
|
|
|
|
}
|
|
|
|
|
2021-05-21 16:17:25 -07:00
|
|
|
private _generateActionCall(action: Action, isPage: boolean): string {
|
2021-03-03 14:32:09 -08:00
|
|
|
switch (action.name) {
|
|
|
|
case 'openPage':
|
|
|
|
throw Error('Not reached');
|
|
|
|
case 'closePage':
|
|
|
|
return 'close()';
|
|
|
|
case 'click': {
|
|
|
|
let method = 'click';
|
|
|
|
if (action.clickCount === 2)
|
|
|
|
method = 'dblclick';
|
2021-05-21 16:17:25 -07:00
|
|
|
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})`;
|
2021-03-03 14:32:09 -08:00
|
|
|
}
|
|
|
|
case 'check':
|
|
|
|
return `check(${quote(action.selector)})`;
|
|
|
|
case 'uncheck':
|
|
|
|
return `uncheck(${quote(action.selector)})`;
|
|
|
|
case 'fill':
|
|
|
|
return `fill(${quote(action.selector)}, ${quote(action.text)})`;
|
|
|
|
case 'setInputFiles':
|
|
|
|
return `setInputFiles(${quote(action.selector)}, ${formatPath(action.files.length === 1 ? action.files[0] : action.files)})`;
|
|
|
|
case 'press': {
|
|
|
|
const modifiers = toModifiers(action.modifiers);
|
|
|
|
const shortcut = [...modifiers, action.key].join('+');
|
|
|
|
return `press(${quote(action.selector)}, ${quote(shortcut)})`;
|
|
|
|
}
|
|
|
|
case 'navigate':
|
|
|
|
return `navigate(${quote(action.url)})`;
|
|
|
|
case 'select':
|
|
|
|
return `selectOption(${quote(action.selector)}, ${formatSelectOption(action.options.length > 1 ? action.options : action.options[0])})`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
generateHeader(options: LanguageGeneratorOptions): string {
|
|
|
|
const formatter = new JavaScriptFormatter();
|
|
|
|
formatter.add(`
|
|
|
|
import com.microsoft.playwright.*;
|
|
|
|
import com.microsoft.playwright.options.*;
|
2021-05-21 16:17:25 -07:00
|
|
|
import java.util.*;
|
2021-03-03 14:32:09 -08:00
|
|
|
|
|
|
|
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)});`);
|
|
|
|
return formatter.format();
|
|
|
|
}
|
|
|
|
|
|
|
|
generateFooter(saveStorage: string | undefined): string {
|
2021-05-21 16:17:25 -07:00
|
|
|
const storageStateLine = saveStorage ? `\n context.storageState(new BrowserContext.StorageStateOptions().setPath(${quote(saveStorage)}));\n` : '';
|
|
|
|
return `${storageStateLine} }
|
2021-03-03 14:32:09 -08:00
|
|
|
}
|
|
|
|
}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatPath(files: string | string[]): string {
|
|
|
|
if (Array.isArray(files)) {
|
|
|
|
if (files.length === 0)
|
|
|
|
return 'new Path[0]';
|
|
|
|
return `new Path[] {${files.map(s => 'Paths.get(' + quote(s) + ')').join(', ')}}`;
|
|
|
|
}
|
|
|
|
return `Paths.get(${quote(files)})`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatSelectOption(options: string | string[]): string {
|
|
|
|
if (Array.isArray(options)) {
|
|
|
|
if (options.length === 0)
|
|
|
|
return 'new String[0]';
|
|
|
|
return `new String[] {${options.map(s => quote(s)).join(', ')}}`;
|
|
|
|
}
|
|
|
|
return quote(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatLaunchOptions(options: any): string {
|
|
|
|
const lines = [];
|
|
|
|
if (!Object.keys(options).length)
|
|
|
|
return '';
|
|
|
|
lines.push('new BrowserType.LaunchOptions()');
|
|
|
|
if (typeof options.headless === 'boolean')
|
2021-03-05 14:05:48 -08:00
|
|
|
lines.push(` .setHeadless(false)`);
|
2021-03-15 08:07:57 -07:00
|
|
|
if (options.channel)
|
|
|
|
lines.push(` .setChannel("${options.channel}")`);
|
2021-03-03 14:32:09 -08:00
|
|
|
return lines.join('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatContextOptions(contextOptions: BrowserContextOptions, deviceName: string | undefined): string {
|
|
|
|
const lines = [];
|
|
|
|
if (!Object.keys(contextOptions).length && !deviceName)
|
|
|
|
return '';
|
|
|
|
const device = deviceName ? deviceDescriptors[deviceName] : {};
|
|
|
|
const options: BrowserContextOptions = { ...device, ...contextOptions };
|
|
|
|
lines.push('new Browser.NewContextOptions()');
|
2021-05-21 16:17:25 -07:00
|
|
|
if (options.acceptDownloads)
|
|
|
|
lines.push(` .setAcceptDownloads(true)`);
|
|
|
|
if (options.bypassCSP)
|
|
|
|
lines.push(` .setBypassCSP(true)`);
|
2021-03-03 14:32:09 -08:00
|
|
|
if (options.colorScheme)
|
2021-03-05 14:05:48 -08:00
|
|
|
lines.push(` .setColorScheme(ColorScheme.${options.colorScheme.toUpperCase()})`);
|
2021-05-21 16:17:25 -07:00
|
|
|
if (options.deviceScaleFactor)
|
|
|
|
lines.push(` .setDeviceScaleFactor(${options.deviceScaleFactor})`);
|
2021-03-03 14:32:09 -08:00
|
|
|
if (options.geolocation)
|
2021-03-05 14:05:48 -08:00
|
|
|
lines.push(` .setGeolocation(${options.geolocation.latitude}, ${options.geolocation.longitude})`);
|
2021-05-21 16:17:25 -07:00
|
|
|
if (options.hasTouch)
|
|
|
|
lines.push(` .setHasTouch(${options.hasTouch})`);
|
|
|
|
if (options.isMobile)
|
|
|
|
lines.push(` .setIsMobile(${options.isMobile})`);
|
2021-03-03 14:32:09 -08:00
|
|
|
if (options.locale)
|
2021-03-05 14:05:48 -08:00
|
|
|
lines.push(` .setLocale("${options.locale}")`);
|
2021-03-03 14:32:09 -08:00
|
|
|
if (options.proxy)
|
2021-03-05 14:05:48 -08:00
|
|
|
lines.push(` .setProxy(new Proxy("${options.proxy.server}"))`);
|
2021-05-21 16:17:25 -07:00
|
|
|
if (options.storageState)
|
|
|
|
lines.push(` .setStorageStatePath(Paths.get(${quote(options.storageState as string)}))`);
|
2021-03-03 14:32:09 -08:00
|
|
|
if (options.timezoneId)
|
2021-03-05 14:05:48 -08:00
|
|
|
lines.push(` .setTimezoneId("${options.timezoneId}")`);
|
2021-03-03 14:32:09 -08:00
|
|
|
if (options.userAgent)
|
2021-03-05 14:05:48 -08:00
|
|
|
lines.push(` .setUserAgent("${options.userAgent}")`);
|
2021-03-03 14:32:09 -08:00
|
|
|
if (options.viewport)
|
2021-03-05 14:05:48 -08:00
|
|
|
lines.push(` .setViewportSize(${options.viewport.width}, ${options.viewport.height})`);
|
2021-05-21 16:17:25 -07:00
|
|
|
return lines.join('\n');
|
|
|
|
}
|
2021-03-03 14:32:09 -08:00
|
|
|
|
2021-05-21 16:17:25 -07:00
|
|
|
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()`);
|
2021-03-03 14:32:09 -08:00
|
|
|
return lines.join('\n');
|
|
|
|
}
|
|
|
|
|
2021-08-23 12:22:19 -04:00
|
|
|
function quote(text: string) {
|
|
|
|
return escapeWithQuotes(text, '\"');
|
2021-03-03 14:32:09 -08:00
|
|
|
}
|