Add WaitForNetworkIdle and Timeout

This commit is contained in:
Haiping Chen 2024-06-26 07:48:15 -05:00
parent 9c620b1cae
commit 5e10023abb
3 changed files with 20 additions and 6 deletions

View file

@ -12,6 +12,9 @@ public class PageActionArgs
public string Url { get; set; } = null!;
public bool OpenNewTab { get; set; } = false;
public bool WaitForNetworkIdle = true;
public float? Timeout { get; set; }
/// <summary>
/// On page data fetched
/// </summary>

View file

@ -49,6 +49,11 @@ public class PlaywrightInstance : IDisposable
Headless = true,
#endif
Channel = "chrome",
ViewportSize = new ViewportSize
{
Width = 1600,
Height = 900
},
IgnoreDefaultArgs =
[
"--enable-automation",
@ -71,10 +76,6 @@ public class PlaywrightInstance : IDisposable
Serilog.Log.Information($"Page is closed: {e.Url}");
};
Serilog.Log.Information($"New page is created: {page.Url}");
if (!page.IsClosed)
{
await page.SetViewportSizeAsync(1600, 900);
}
/*page.Response += async (sender, e) =>
{

View file

@ -29,9 +29,19 @@ public partial class PlaywrightWebDriver
var page = args.OpenNewTab ? await _instance.NewPage(message.ContextId, fetched: args.OnDataFetched) :
_instance.GetPage(message.ContextId);
var response = await page.GotoAsync(args.Url);
var response = await page.GotoAsync(args.Url, new PageGotoOptions
{
Timeout = args.Timeout
});
await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
await page.WaitForLoadStateAsync(LoadState.NetworkIdle);
if (args.WaitForNetworkIdle)
{
await page.WaitForLoadStateAsync(LoadState.NetworkIdle, new PageWaitForLoadStateOptions
{
Timeout = args.Timeout
});
}
if (response.Status == 200)
{