chore: make csharp library codegen more csharp like (#28663)

This commit is contained in:
Max Schmitt 2023-12-15 10:24:26 -08:00 committed by GitHub
parent 9c845365f7
commit 44c3ad5ceb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 158 additions and 166 deletions

View File

@ -53,7 +53,7 @@ export class CSharpLanguageGenerator implements LanguageGenerator {
generateAction(actionInContext: ActionInContext): string { generateAction(actionInContext: ActionInContext): string {
const action = this._generateActionInner(actionInContext); const action = this._generateActionInner(actionInContext);
if (action) if (action)
return action + '\n'; return action;
return ''; return '';
} }
@ -64,7 +64,7 @@ export class CSharpLanguageGenerator implements LanguageGenerator {
let pageAlias = actionInContext.frame.pageAlias; let pageAlias = actionInContext.frame.pageAlias;
if (this._mode !== 'library') if (this._mode !== 'library')
pageAlias = pageAlias.replace('page', 'Page'); pageAlias = pageAlias.replace('page', 'Page');
const formatter = new CSharpFormatter(8); const formatter = new CSharpFormatter(this._mode === 'library' ? 0 : 8);
if (action.name === 'openPage') { if (action.name === 'openPage') {
formatter.add(`var ${pageAlias} = await context.NewPageAsync();`); formatter.add(`var ${pageAlias} = await context.NewPageAsync();`);
@ -184,10 +184,6 @@ export class CSharpLanguageGenerator implements LanguageGenerator {
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
class Program
{
public static async Task Main()
{
using var playwright = await Playwright.CreateAsync(); using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.${toPascal(options.browserName)}.LaunchAsync(${formatObject(options.launchOptions, ' ', 'BrowserTypeLaunchOptions')}); await using var browser = await playwright.${toPascal(options.browserName)}.LaunchAsync(${formatObject(options.launchOptions, ' ', 'BrowserTypeLaunchOptions')});
var context = await browser.NewContextAsync(${formatContextOptions(options.contextOptions, options.deviceName)});`); var context = await browser.NewContextAsync(${formatContextOptions(options.contextOptions, options.deviceName)});`);
@ -220,9 +216,11 @@ export class CSharpLanguageGenerator implements LanguageGenerator {
} }
generateFooter(saveStorage: string | undefined): string { generateFooter(saveStorage: string | undefined): string {
const storageStateLine = saveStorage ? `\n await context.StorageStateAsync(new BrowserContextStorageStateOptions\n {\n Path = ${quote(saveStorage)}\n });\n` : ''; const offset = this._mode === 'library' ? '' : ' ';
return `${storageStateLine} } let storageStateLine = saveStorage ? `\n${offset}await context.StorageStateAsync(new BrowserContextStorageStateOptions\n${offset}{\n${offset} Path = ${quote(saveStorage)}\n${offset}});\n` : '';
}\n`; if (this._mode !== 'library')
storageStateLine += ` }\n}\n`;
return storageStateLine;
} }
} }

View File

@ -33,10 +33,6 @@ test('should print the correct imports and context options', async ({ browserNam
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
class Program
{
public static async Task Main()
{
using var playwright = await Playwright.CreateAsync(); using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions await using var browser = await playwright.${capitalize(browserName)}.LaunchAsync(new BrowserTypeLaunchOptions
{ {
@ -231,7 +227,6 @@ public class Tests : PageTest
public async Task MyTest() public async Task MyTest()
{ {
await Page.GotoAsync("${emptyHTML}"); await Page.GotoAsync("${emptyHTML}");
} }
}`; }`;
expect(cli.text()).toContain(expected); expect(cli.text()).toContain(expected);
@ -259,7 +254,6 @@ public class Tests : PageTest
public async Task MyTest() public async Task MyTest()
{ {
await Page.GotoAsync("${emptyHTML}"); await Page.GotoAsync("${emptyHTML}");
} }
}`; }`;
expect(cli.text()).toContain(expected); expect(cli.text()).toContain(expected);