一、新建项目TestProject3
二、准备swagger
三、开始录制
打开PowerShell 7 (x64)
cd D:\xxx\xxx\VS2022Projects\TestProject3\TestProject3\bin\Debug\net8.0
pwsh playwright.ps1 codegen --target=csharp -b chromium localhost:5252/swagger/index.html
#支持的语言
javascript, test, python, python-async和csharp
我们这里选择C#
把生成成的代码贴进项目,需要简单调整一下如下:
using Microsoft.Playwright;
namespace TestProject3
{
[TestClass]
public class UnitTest1 : PageTest
{
[TestMethod]
public async Task HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage()
{
await using var browser = await Playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
Headless = false,
});
var context = await browser.NewContextAsync();
var page = await context.NewPageAsync();
await page.GotoAsync("http://localhost:5252/swagger/index.html");
await page.GetByRole(AriaRole.Button, new PageGetByRoleOptions() { NameString = "post /api/hellos" }).ClickAsync();
await page.GetByRole(AriaRole.Button, new() { NameString = "Try it out" }).ClickAsync();
await page.GetByText(new Regex("\\{\\s+\"id\":\\s+0,\\s+\"name\":\\s+\"string\",\\s+\"cloudFileName\":\\s+\"string\"\\s+\\}")).ClickAsync();
await page.GetByText(new Regex("\\{\\s+\"id\":\\s+0,\\s+\"name\":\\s+\"string\",\\s+\"cloudFileName\":\\s+\"string\"\\s+\\}")).ClickAsync();
await page.GetByText(new Regex("\\{\\s+\"id\":\\s+0,\\s+\"name\":\\s+\"string\",\\s+\"cloudFileName\":\\s+\"string\"\\s+\\}")).ClickAsync();
await page.GetByText(new Regex("\\{\\s+\"id\":\\s+0,\\s+\"name\":\\s+\"string\",\\s+\"cloudFileName\":\\s+\"string\"\\s+\\}")).PressAsync("ArrowUp");
await page.GetByText(new Regex("\\{\\s+\"id\":\\s+0,\\s+\"name\":\\s+\"string\",\\s+\"cloudFileName\":\\s+\"string\"\\s+\\}")).PressAsync("ArrowUp");
await page.GetByText(new Regex("\\{\\s+\"id\":\\s+0,\\s+\"name\":\\s+\"string\",\\s+\"cloudFileName\":\\s+\"string\"\\s+\\}")).PressAsync("ArrowUp");
await page.GetByText(new Regex("\\{\\s+\"id\":\\s+0,\\s+\"name\":\\s+\"string\",\\s+\"cloudFileName\":\\s+\"string\"\\s+\\}")).FillAsync("{\n \"name\": \"string\",\n \"cloudFileName\": \"string\"\n}");
await page.GetByRole(AriaRole.Button, new() { NameString = "Execute" }).ClickAsync();
}
}
}