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';
|
2021-04-24 03:34:52 +02:00
|
|
|
import deviceDescriptors from '../../deviceDescriptors';
|
2020-12-28 23:50:12 +01:00
|
|
|
|
|
|
|
|
export class CSharpLanguageGenerator implements LanguageGenerator {
|
2021-02-17 03:13:26 +01:00
|
|
|
id = 'csharp';
|
|
|
|
|
fileName = '<csharp>';
|
|
|
|
|
highlighter = 'csharp';
|
2020-12-28 23:50:12 +01:00
|
|
|
|
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 CSharpFormatter(0);
|
|
|
|
|
formatter.newLine();
|
|
|
|
|
formatter.add('// ' + actionTitle(action));
|
|
|
|
|
|
|
|
|
|
if (action.name === 'openPage') {
|
|
|
|
|
formatter.add(`var ${pageAlias} = await context.NewPageAsync();`);
|
|
|
|
|
if (action.url && action.url !== 'about:blank' && action.url !== 'chrome://newtab/')
|
2021-05-13 20:57:02 +02:00
|
|
|
formatter.add(`await ${pageAlias}.GotoAsync(${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}.GetFrame(name: ${quote(actionInContext.frameName)})` :
|
|
|
|
|
`${pageAlias}.GetFrame(url: ${quote(actionInContext.frameUrl)})`);
|
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(` void ${pageAlias}_Dialog${signals.dialog.dialogAlias}_EventHandler(object sender, DialogEventArgs e)
|
2020-12-28 23:50:12 +01:00
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Dialog message: {e.Dialog.Message}");
|
|
|
|
|
e.Dialog.DismissAsync();
|
2021-02-17 03:13:26 +01:00
|
|
|
${pageAlias}.Dialog -= ${pageAlias}_Dialog${signals.dialog.dialogAlias}_EventHandler;
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
2021-02-17 03:13:26 +01:00
|
|
|
${pageAlias}.Dialog += ${pageAlias}_Dialog${signals.dialog.dialogAlias}_EventHandler;`);
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-17 03:13:26 +01:00
|
|
|
const emitTaskWhenAll = signals.waitForNavigation || signals.popup || signals.download;
|
2020-12-28 23:50:12 +01:00
|
|
|
if (emitTaskWhenAll) {
|
2021-02-17 03:13:26 +01:00
|
|
|
if (signals.popup)
|
|
|
|
|
formatter.add(`var ${signals.popup.popupAlias}Task = ${pageAlias}.WaitForEventAsync(PageEvent.Popup)`);
|
|
|
|
|
else if (signals.download)
|
2020-12-28 23:50:12 +01:00
|
|
|
formatter.add(`var downloadTask = ${pageAlias}.WaitForEventAsync(PageEvent.Download);`);
|
|
|
|
|
|
|
|
|
|
formatter.add(`await Task.WhenAll(`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Popup signals.
|
2021-02-17 03:13:26 +01:00
|
|
|
if (signals.popup)
|
|
|
|
|
formatter.add(`${signals.popup.popupAlias}Task,`);
|
2020-12-28 23:50:12 +01:00
|
|
|
|
|
|
|
|
// Navigation signal.
|
2021-02-17 03:13:26 +01:00
|
|
|
if (signals.waitForNavigation)
|
|
|
|
|
formatter.add(`${pageAlias}.WaitForNavigationAsync(/*${quote(signals.waitForNavigation.url)}*/),`);
|
2020-12-28 23:50:12 +01:00
|
|
|
|
|
|
|
|
// Download signals.
|
2021-02-17 03:13:26 +01:00
|
|
|
if (signals.download)
|
2020-12-28 23:50:12 +01:00
|
|
|
formatter.add(`downloadTask,`);
|
|
|
|
|
|
2021-02-17 03:13:26 +01:00
|
|
|
const prefix = (signals.popup || signals.waitForNavigation || signals.download) ? '' : 'await ';
|
2020-12-28 23:50:12 +01:00
|
|
|
const actionCall = this._generateActionCall(action);
|
|
|
|
|
const suffix = emitTaskWhenAll ? ');' : ';';
|
|
|
|
|
formatter.add(`${prefix}${subject}.${actionCall}${suffix}`);
|
|
|
|
|
|
2021-02-17 03:13:26 +01:00
|
|
|
if (signals.assertNavigation)
|
|
|
|
|
formatter.add(` // Assert.Equal(${quote(signals.assertNavigation.url)}, ${pageAlias}.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 'CloseAsync()';
|
|
|
|
|
case 'click': {
|
|
|
|
|
let method = 'ClickAsync';
|
|
|
|
|
if (action.clickCount === 2)
|
|
|
|
|
method = 'DblClickAsync';
|
|
|
|
|
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, false);
|
|
|
|
|
return `${method}(${quote(action.selector)}${optionsString})`;
|
|
|
|
|
}
|
|
|
|
|
case 'check':
|
|
|
|
|
return `CheckAsync(${quote(action.selector)})`;
|
|
|
|
|
case 'uncheck':
|
|
|
|
|
return `UncheckAsync(${quote(action.selector)})`;
|
|
|
|
|
case 'fill':
|
|
|
|
|
return `FillAsync(${quote(action.selector)}, ${quote(action.text)})`;
|
|
|
|
|
case 'setInputFiles':
|
|
|
|
|
return `SetInputFilesAsync(${quote(action.selector)}, ${formatObject(action.files.length === 1 ? action.files[0] : action.files)})`;
|
|
|
|
|
case 'press': {
|
|
|
|
|
const modifiers = toModifiers(action.modifiers);
|
|
|
|
|
const shortcut = [...modifiers, action.key].join('+');
|
|
|
|
|
return `PressAsync(${quote(action.selector)}, ${quote(shortcut)})`;
|
|
|
|
|
}
|
|
|
|
|
case 'navigate':
|
2021-05-13 20:57:02 +02:00
|
|
|
return `GotoAsync(${quote(action.url)})`;
|
2020-12-28 23:50:12 +01:00
|
|
|
case 'select':
|
|
|
|
|
return `SelectOptionAsync(${quote(action.selector)}, ${formatObject(action.options.length > 1 ? action.options : action.options[0])})`;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-17 03:13:26 +01:00
|
|
|
generateHeader(options: LanguageGeneratorOptions): string {
|
2020-12-28 23:50:12 +01:00
|
|
|
const formatter = new CSharpFormatter(0);
|
|
|
|
|
formatter.add(`
|
|
|
|
|
await Playwright.InstallAsync();
|
|
|
|
|
using var playwright = await Playwright.CreateAsync();
|
2021-03-15 16:07:57 +01:00
|
|
|
await using var browser = await playwright.${toPascal(options.browserName)}.LaunchAsync(${formatArgs(options.launchOptions)}
|
|
|
|
|
);
|
2021-02-17 03:13:26 +01:00
|
|
|
var context = await browser.NewContextAsync(${formatContextOptions(options.contextOptions, options.deviceName)});`);
|
2020-12-28 23:50:12 +01:00
|
|
|
return formatter.format();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generateFooter(saveStorage: string | undefined): string {
|
2021-01-13 21:52:03 +01:00
|
|
|
const storageStateLine = saveStorage ? `\nawait context.StorageStateAsync(path: "${saveStorage}");` : '';
|
2021-02-17 03:13:26 +01:00
|
|
|
return `\n// ---------------------${storageStateLine}`;
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatValue(value: any): string {
|
|
|
|
|
if (value === false)
|
|
|
|
|
return 'false';
|
|
|
|
|
if (value === true)
|
|
|
|
|
return 'true';
|
|
|
|
|
if (value === undefined)
|
|
|
|
|
return 'null';
|
|
|
|
|
if (Array.isArray(value))
|
|
|
|
|
return `new [] {${value.map(formatValue).join(', ')}}`;
|
|
|
|
|
if (typeof value === 'string')
|
|
|
|
|
return quote(value);
|
|
|
|
|
return String(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatOptions(value: any, hasArguments: boolean, isInitializing: boolean): string {
|
|
|
|
|
const keys = Object.keys(value);
|
|
|
|
|
if (!keys.length)
|
|
|
|
|
return '';
|
|
|
|
|
return (hasArguments ? ', ' : '') + keys.map(key => `${key}${isInitializing ? ': ' : ' = '}${formatValue(value[key])}`).join(', ');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatArgs(value: any, indent = ' '): string {
|
|
|
|
|
if (typeof value === 'string')
|
|
|
|
|
return quote(value);
|
|
|
|
|
if (Array.isArray(value))
|
|
|
|
|
return `new [] {${value.map(o => formatObject(o)).join(', ')}}`;
|
|
|
|
|
if (typeof value === 'object') {
|
|
|
|
|
const keys = Object.keys(value);
|
|
|
|
|
if (!keys.length)
|
|
|
|
|
return '';
|
|
|
|
|
const tokens: string[] = [];
|
|
|
|
|
for (const key of keys)
|
|
|
|
|
tokens.push(`${keys.length !== 1 ? indent : ''}${key}: ${formatObject(value[key], indent, key)}`);
|
2021-03-15 16:07:57 +01:00
|
|
|
return `\n${indent}${tokens.join(`,\n${indent}`)}`;
|
2020-12-28 23:50:12 +01:00
|
|
|
}
|
|
|
|
|
return String(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatObject(value: any, indent = ' ', name = ''): string {
|
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
|
if (name === 'permissions' || name === 'colorScheme')
|
|
|
|
|
return `${getClassName(name)}.${toPascal(value)}`;
|
|
|
|
|
return quote(value);
|
|
|
|
|
}
|
|
|
|
|
if (Array.isArray(value))
|
|
|
|
|
return `new[] { ${value.map(o => formatObject(o, indent, name)).join(', ')} }`;
|
|
|
|
|
if (typeof value === 'object') {
|
|
|
|
|
const keys = Object.keys(value);
|
|
|
|
|
if (!keys.length)
|
|
|
|
|
return '';
|
|
|
|
|
const tokens: string[] = [];
|
|
|
|
|
for (const key of keys)
|
|
|
|
|
tokens.push(`${toPascal(key)} = ${formatObject(value[key], indent, key)},`);
|
|
|
|
|
if (name)
|
|
|
|
|
return `new ${getClassName(name)}\n{\n${indent}${tokens.join(`\n${indent}`)}\n${indent}}`;
|
|
|
|
|
return `{\n${indent}${tokens.join(`\n${indent}`)}\n${indent}}`;
|
|
|
|
|
}
|
|
|
|
|
if (name === 'latitude' || name === 'longitude')
|
|
|
|
|
return String(value) + 'm';
|
|
|
|
|
|
|
|
|
|
return String(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getClassName(value: string): string {
|
|
|
|
|
switch (value) {
|
|
|
|
|
case 'viewport': return 'ViewportSize';
|
|
|
|
|
case 'proxy': return 'ProxySettings';
|
|
|
|
|
case 'permissions': return 'ContextPermission';
|
|
|
|
|
default: return toPascal(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function toPascal(value: string): string {
|
|
|
|
|
return value[0].toUpperCase() + value.slice(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 formatArgs(options);
|
2021-01-25 04:21:19 +01:00
|
|
|
const serializedObject = formatObject(sanitizeDeviceOptions(device, options), ' ');
|
2020-12-28 23:50:12 +01:00
|
|
|
// When there are no additional context options, we still want to spread the device inside.
|
|
|
|
|
|
|
|
|
|
if (!serializedObject)
|
|
|
|
|
return `playwright.Devices["${deviceName}"]`;
|
|
|
|
|
let result = `new BrowserContextOptions(playwright.Devices["${deviceName}"])`;
|
|
|
|
|
|
|
|
|
|
if (serializedObject) {
|
|
|
|
|
const lines = serializedObject.split('\n');
|
|
|
|
|
result = `${result} \n${lines.join('\n')}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CSharpFormatter {
|
|
|
|
|
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 = '';
|
|
|
|
|
let previousLine = '';
|
|
|
|
|
return this._lines.map((line: string) => {
|
|
|
|
|
if (line === '')
|
|
|
|
|
return line;
|
2021-03-15 16:07:57 +01:00
|
|
|
if (line.startsWith('}') || line.startsWith(']') || line.includes('});') || line === ');')
|
2020-12-28 23:50:12 +01:00
|
|
|
spaces = spaces.substring(this._baseIndent.length);
|
|
|
|
|
|
|
|
|
|
const extraSpaces = /^(for|while|if).*\(.*\)$/.test(previousLine) ? this._baseIndent : '';
|
|
|
|
|
previousLine = line;
|
|
|
|
|
|
|
|
|
|
line = spaces + extraSpaces + line;
|
|
|
|
|
if (line.endsWith('{') || line.endsWith('[') || line.endsWith('('))
|
|
|
|
|
spaces += this._baseIndent;
|
|
|
|
|
if (line.endsWith('});'))
|
|
|
|
|
spaces = spaces.substring(this._baseIndent.length);
|
|
|
|
|
|
|
|
|
|
return this._baseOffset + line;
|
|
|
|
|
}).join('\n');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function quote(text: string) {
|
|
|
|
|
return `"${text.replace(/["]/g, '\\"')}"`;
|
|
|
|
|
}
|