2020-12-28 23:50:12 +01: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.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-02-17 03:13:26 +01:00
|
|
|
import type { BrowserContextOptions } from '../../../..';
|
|
|
|
|
import { LanguageGenerator, LanguageGeneratorOptions, sanitizeDeviceOptions, toSignalMap } from './language';
|
2021-01-25 04:21:19 +01:00
|
|
|
import { ActionInContext } from './codeGenerator';
|
2021-02-17 03:13:26 +01:00
|
|
|
import { actionTitle, Action } from './recorderActions';
|
2021-01-25 04:21:19 +01:00
|
|
|
import { MouseClickOptions, toModifiers } from './utils';
|
|
|
|
|
import deviceDescriptors = require('../../deviceDescriptors');
|
2020-12-28 23:50:12 +01:00
|
|
|
|
|
|
|
|
export class PythonLanguageGenerator implements LanguageGenerator {
|
2021-02-17 03:13:26 +01:00
|
|
|
id = 'python';
|
|
|
|
|
fileName = '<python>';
|
|
|
|
|
highlighter = 'python';
|
|
|
|
|
|
2020-12-28 23:50:12 +01:00
|
|
|
private _awaitPrefix: '' | 'await ';
|
|
|
|
|
private _asyncPrefix: '' | 'async ';
|
|
|
|
|
private _isAsync: boolean;
|
|
|
|
|
|
|
|
|
|
constructor(isAsync: boolean) {
|
2021-02-17 03:13:26 +01:00
|
|
|
this.id = isAsync ? 'python-async' : 'python';
|
|
|
|
|
this.fileName = isAsync ? '<async python>' : '<python>';
|
2020-12-28 23:50:12 +01:00
|
|
|
this._isAsync = isAsync;
|
|
|
|
|
this._awaitPrefix = isAsync ? 'await ' : '';
|
|
|
|
|
this._asyncPrefix = isAsync ? 'async ' : '';
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-17 03:13:26 +01:00
|
|
|
generateAction(actionInContext: ActionInContext): string {
|
2021-01-27 22:19:36 +01:00
|
|
|
const { action, pageAlias } = actionInContext;
|
2020-12-28 23:50:12 +01:00
|
|
|
const formatter = new PythonFormatter(4);
|
|
|
|
|
formatter.newLine();
|
|
|
|
|
formatter.add('# ' + actionTitle(action));
|
|
|
|
|
|
|
|
|
|
if (action.name === 'openPage') {
|
2021-01-13 21:52:03 +01:00
|
|
|
formatter.add(`${pageAlias} = ${this._awaitPrefix}context.new_page()`);
|
2020-12-28 23:50:12 +01:00
|
|
|
if (action.url && action.url !== 'about:blank' && action.url !== 'chrome://newtab/')
|
2021-04-21 16:59:38 +02:00
|
|
|
formatter.add(`${this._awaitPrefix}${pageAlias}.goto(${quote(action.url)})`);
|
2020-12-28 23:50:12 +01:00
|
|
|
return formatter.format();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 22:19:36 +01:00
|
|
|
const subject = actionInContext.isMainFrame ? pageAlias :
|
|
|
|
|
(actionInContext.frameName ?
|
|
|
|
|
`${pageAlias}.frame(${formatOptions({ name: actionInContext.frameName }, false)})` :
|
|
|
|
|
`${pageAlias}.frame(${formatOptions({ url: actionInContext.frameUrl }, false)})`);
|
2020-12-28 23:50:12 +01:00
|
|
|
|
2021-02-17 03:13:26 +01:00
|
|
|
const signals = toSignalMap(action);
|
2020-12-28 23:50:12 +01:00
|
|
|
|
2021-02-17 03:13:26 +01:00
|
|
|
if (signals.dialog)
|
|
|
|
|
formatter.add(` ${pageAlias}.once("dialog", lambda dialog: dialog.dismiss())`);
|
2020-12-28 23:50:12 +01:00
|
|
|
|
|
|
|
|
const actionCall = this._generateActionCall(action);
|
|
|
|
|
let code = `${this._awaitPrefix}${subject}.${actionCall}`;
|
|
|
|
|
|
2021-02-17 03:13:26 +01:00
|
|
|
if (signals.popup) {
|
2020-12-28 23:50:12 +01:00
|
|
|
code = `${this._asyncPrefix}with ${pageAlias}.expect_popup() as popup_info {
|
|
|
|
|
${code}
|
|
|
|
|
}
|
2021-02-17 03:13:26 +01:00
|
|
|
${signals.popup.popupAlias} = ${this._awaitPrefix}popup_info.value`;
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-17 03:13:26 +01:00
|
|
|
if (signals.download) {
|
2020-12-28 23:50:12 +01:00
|
|
|
code = `${this._asyncPrefix}with ${pageAlias}.expect_download() as download_info {
|
|
|
|
|
${code}
|
|
|
|
|
}
|
2021-02-17 03:13:26 +01:00
|
|
|
download = ${this._awaitPrefix}download_info.value`;
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-17 03:13:26 +01:00
|
|
|
if (signals.waitForNavigation) {
|
2020-12-28 23:50:12 +01:00
|
|
|
code = `
|
2021-02-17 03:13:26 +01:00
|
|
|
# ${this._asyncPrefix}with ${pageAlias}.expect_navigation(url=${quote(signals.waitForNavigation.url)}):
|
2020-12-28 23:50:12 +01:00
|
|
|
${this._asyncPrefix}with ${pageAlias}.expect_navigation() {
|
|
|
|
|
${code}
|
|
|
|
|
}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
formatter.add(code);
|
|
|
|
|
|
2021-02-17 03:13:26 +01:00
|
|
|
if (signals.assertNavigation)
|
|
|
|
|
formatter.add(` # assert ${pageAlias}.url == ${quote(signals.assertNavigation.url)}`);
|
2020-12-28 23:50:12 +01:00
|
|
|
return formatter.format();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _generateActionCall(action: Action): string {
|
|
|
|
|
switch (action.name) {
|
|
|
|
|
case 'openPage':
|
|
|
|
|
throw Error('Not reached');
|
|
|
|
|
case 'closePage':
|
|
|
|
|
return 'close()';
|
|
|
|
|
case 'click': {
|
|
|
|
|
let method = 'click';
|
|
|
|
|
if (action.clickCount === 2)
|
|
|
|
|
method = 'dblclick';
|
|
|
|
|
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 optionsString = formatOptions(options, true);
|
|
|
|
|
return `${method}(${quote(action.selector)}${optionsString})`;
|
|
|
|
|
}
|
|
|
|
|
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':
|
2021-02-17 03:13:26 +01:00
|
|
|
return `set_input_files(${quote(action.selector)}, ${formatValue(action.files.length === 1 ? action.files[0] : action.files)})`;
|
2020-12-28 23:50:12 +01:00
|
|
|
case 'press': {
|
|
|
|
|
const modifiers = toModifiers(action.modifiers);
|
|
|
|
|
const shortcut = [...modifiers, action.key].join('+');
|
|
|
|
|
return `press(${quote(action.selector)}, ${quote(shortcut)})`;
|
|
|
|
|
}
|
|
|
|
|
case 'navigate':
|
|
|
|
|
return `goto(${quote(action.url)})`;
|
|
|
|
|
case 'select':
|
2021-02-17 03:13:26 +01:00
|
|
|
return `select_option(${quote(action.selector)}, ${formatValue(action.options.length === 1 ? action.options[0] : action.options)})`;
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-17 03:13:26 +01:00
|
|
|
generateHeader(options: LanguageGeneratorOptions): string {
|
2020-12-28 23:50:12 +01:00
|
|
|
const formatter = new PythonFormatter();
|
|
|
|
|
if (this._isAsync) {
|
|
|
|
|
formatter.add(`
|
|
|
|
|
import asyncio
|
2021-01-12 02:04:24 +01:00
|
|
|
from playwright.async_api import async_playwright
|
2020-12-28 23:50:12 +01:00
|
|
|
|
|
|
|
|
async def run(playwright) {
|
2021-02-17 03:13:26 +01:00
|
|
|
browser = await playwright.${options.browserName}.launch(${formatOptions(options.launchOptions, false)})
|
|
|
|
|
context = await browser.new_context(${formatContextOptions(options.contextOptions, options.deviceName)})`);
|
2020-12-28 23:50:12 +01:00
|
|
|
} else {
|
|
|
|
|
formatter.add(`
|
2021-01-12 02:04:24 +01:00
|
|
|
from playwright.sync_api import sync_playwright
|
2020-12-28 23:50:12 +01:00
|
|
|
|
|
|
|
|
def run(playwright) {
|
2021-02-17 03:13:26 +01:00
|
|
|
browser = playwright.${options.browserName}.launch(${formatOptions(options.launchOptions, false)})
|
|
|
|
|
context = browser.new_context(${formatContextOptions(options.contextOptions, options.deviceName)})`);
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
|
|
|
|
return formatter.format();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generateFooter(saveStorage: string | undefined): string {
|
|
|
|
|
if (this._isAsync) {
|
2021-01-13 21:52:03 +01:00
|
|
|
const storageStateLine = saveStorage ? `\n await context.storage_state(path="${saveStorage}")` : '';
|
2021-02-17 03:13:26 +01:00
|
|
|
return `\n # ---------------------${storageStateLine}
|
2020-12-28 23:50:12 +01:00
|
|
|
await context.close()
|
|
|
|
|
await browser.close()
|
|
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
|
async with async_playwright() as playwright:
|
|
|
|
|
await run(playwright)
|
|
|
|
|
asyncio.run(main())`;
|
|
|
|
|
} else {
|
2021-01-13 21:52:03 +01:00
|
|
|
const storageStateLine = saveStorage ? `\n context.storage_state(path="${saveStorage}")` : '';
|
2021-02-17 03:13:26 +01:00
|
|
|
return `\n # ---------------------${storageStateLine}
|
2020-12-28 23:50:12 +01:00
|
|
|
context.close()
|
|
|
|
|
browser.close()
|
|
|
|
|
|
|
|
|
|
with sync_playwright() as playwright:
|
|
|
|
|
run(playwright)`;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatValue(value: any): string {
|
|
|
|
|
if (value === false)
|
|
|
|
|
return 'False';
|
|
|
|
|
if (value === true)
|
|
|
|
|
return 'True';
|
|
|
|
|
if (value === undefined)
|
|
|
|
|
return 'None';
|
|
|
|
|
if (Array.isArray(value))
|
|
|
|
|
return `[${value.map(formatValue).join(', ')}]`;
|
|
|
|
|
if (typeof value === 'string')
|
|
|
|
|
return quote(value);
|
|
|
|
|
return String(value);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 21:52:03 +01:00
|
|
|
function toSnakeCase(name: string): string {
|
|
|
|
|
const toSnakeCaseRegex = /((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))/g;
|
|
|
|
|
return name.replace(toSnakeCaseRegex, `_$1`).toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 23:50:12 +01:00
|
|
|
function formatOptions(value: any, hasArguments: boolean): string {
|
|
|
|
|
const keys = Object.keys(value);
|
|
|
|
|
if (!keys.length)
|
|
|
|
|
return '';
|
2021-01-13 21:52:03 +01:00
|
|
|
return (hasArguments ? ', ' : '') + keys.map(key => `${toSnakeCase(key)}=${formatValue(value[key])}`).join(', ');
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatContextOptions(options: BrowserContextOptions, deviceName: string | undefined): string {
|
2021-01-25 04:21:19 +01:00
|
|
|
const device = deviceName && deviceDescriptors[deviceName];
|
2020-12-28 23:50:12 +01:00
|
|
|
if (!device)
|
|
|
|
|
return formatOptions(options, false);
|
2021-01-25 04:21:19 +01:00
|
|
|
return `**playwright.devices["${deviceName}"]` + formatOptions(sanitizeDeviceOptions(device, options), true);
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class PythonFormatter {
|
|
|
|
|
private _baseIndent: string;
|
|
|
|
|
private _baseOffset: string;
|
|
|
|
|
private _lines: string[] = [];
|
|
|
|
|
|
|
|
|
|
constructor(offset = 0) {
|
|
|
|
|
this._baseIndent = ' '.repeat(4);
|
|
|
|
|
this._baseOffset = ' '.repeat(offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prepend(text: string) {
|
|
|
|
|
this._lines = text.trim().split('\n').map(line => line.trim()).concat(this._lines);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
add(text: string) {
|
|
|
|
|
this._lines.push(...text.trim().split('\n').map(line => line.trim()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newLine() {
|
|
|
|
|
this._lines.push('');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
format(): string {
|
|
|
|
|
let spaces = '';
|
|
|
|
|
const lines: string[] = [];
|
|
|
|
|
this._lines.forEach((line: string) => {
|
|
|
|
|
if (line === '')
|
|
|
|
|
return lines.push(line);
|
|
|
|
|
if (line === '}') {
|
|
|
|
|
spaces = spaces.substring(this._baseIndent.length);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
line = spaces + line;
|
|
|
|
|
if (line.endsWith('{')) {
|
|
|
|
|
spaces += this._baseIndent;
|
|
|
|
|
line = line.substring(0, line.length - 1).trimEnd() + ':';
|
|
|
|
|
}
|
|
|
|
|
return lines.push(this._baseOffset + line);
|
|
|
|
|
});
|
|
|
|
|
return lines.join('\n');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function quote(text: string, char: string = '\"') {
|
|
|
|
|
if (char === '\'')
|
|
|
|
|
return char + text.replace(/[']/g, '\\\'') + char;
|
|
|
|
|
if (char === '"')
|
|
|
|
|
return char + text.replace(/["]/g, '\\"') + char;
|
|
|
|
|
if (char === '`')
|
|
|
|
|
return char + text.replace(/[`]/g, '\\`') + char;
|
|
|
|
|
throw new Error('Invalid escape char');
|
|
|
|
|
}
|