我正试图用Playwright录制一个HAR文件。
根据文档,您所需要做的就是在BrowserContextOptions
中传递RecordHarPath
,并在BrowserContext
上调用CloseAsync
。
对于Firefox,这不会创建任何HAR文件。有了Chromium,它挂在CloseAsync
上,永远不会完成。
例如
static async Task Main()
{
var playwright = await Playwright.CreateAsync();
var currentDir = Directory.GetCurrentDirectory();
Console.WriteLine("Trying Firefox");
var firefox = await playwright.Firefox.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false});
await TryWith(firefox, Path.Combine(currentDir, "firefoxHar.har"));
Console.WriteLine("Trying Chromium");
var chromium = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });
await TryWith(chromium, Path.Combine(currentDir, "chromium.har"));
}
static async Task TryWith(IBrowser browser, string harPath)
{
var context = await browser.NewContextAsync(new BrowserNewContextOptions()
{
RecordHarPath = harPath
});
var page = await context.NewPageAsync();
await page.GotoAsync("https://www.microsoft.com");
var link = await page.QuerySelectorAsync("a#uhf-shopping-cart");
await link.ClickAsync();
Console.WriteLine("Closing Context");
await context.CloseAsync();
Console.WriteLine("Closing Browser");
await browser.CloseAsync();
}
我最终提出了一个错误,其他人似乎也有同样的问题
https://github.com/microsoft/playwright-dotnet/issues/1841