Best Playwright-dotnet code snippet using Microsoft.Playwright.Core.Frame.WaitForLoadStateAsync
Page.cs
Source:Page.cs
...605 public Task UnrouteAsync(Regex urlString, Action<IRoute> handler)606 => UnrouteAsync(urlString, null, handler);607 public Task UnrouteAsync(Func<string, bool> urlFunc, Action<IRoute> handler)608 => UnrouteAsync(null, urlFunc, handler);609 public Task WaitForLoadStateAsync(LoadState? state = default, PageWaitForLoadStateOptions options = default)610 => MainFrame.WaitForLoadStateAsync(state, new() { Timeout = options?.Timeout });611 public Task SetViewportSizeAsync(int width, int height)612 {613 ViewportSize = new() { Width = width, Height = height };614 return _channel.SetViewportSizeAsync(ViewportSize);615 }616 public Task SetCheckedAsync(string selector, bool checkedState, PageSetCheckedOptions options = null)617 => checkedState ?618 MainFrame.CheckAsync(selector, new()619 {620 Position = options?.Position,621 Force = options?.Force,622 NoWaitAfter = options?.NoWaitAfter,623 Strict = options?.Strict,624 Timeout = options?.Timeout,...
Frame.cs
Source:Frame.cs
...121 noWaitAfter: options?.NoWaitAfter,122 strict: options?.Strict,123 force: options?.Force,124 timeout: options?.Timeout).ConfigureAwait(false)).ToList().AsReadOnly();125 public async Task WaitForLoadStateAsync(LoadState? state = default, FrameWaitForLoadStateOptions options = default)126 {127 Task<WaitUntilState> task;128 Waiter waiter = null;129 WaitUntilState loadState = Microsoft.Playwright.WaitUntilState.Load;130 switch (state)131 {132 case Microsoft.Playwright.LoadState.Load:133 loadState = Microsoft.Playwright.WaitUntilState.Load;134 break;135 case Microsoft.Playwright.LoadState.DOMContentLoaded:136 loadState = Microsoft.Playwright.WaitUntilState.DOMContentLoaded;137 break;138 case Microsoft.Playwright.LoadState.NetworkIdle:139 loadState = Microsoft.Playwright.WaitUntilState.NetworkIdle;140 break;141 }142 try143 {144 lock (_loadStates)145 {146 if (_loadStates.Contains(loadState))147 {148 return;149 }150 waiter = SetupNavigationWaiter("frame.WaitForLoadStateAsync", options?.Timeout);151 task = waiter.WaitForEventAsync<WaitUntilState>(this, "LoadState", s =>152 {153 waiter.Log($" \"{s}\" event fired");154 return s == loadState;155 });156 }157 await task.ConfigureAwait(false);158 }159 finally160 {161 waiter?.Dispose();162 }163 }164 public async Task<IResponse> WaitForNavigationAsync(FrameWaitForNavigationOptions options = default)165 {166 WaitUntilState waitUntil2 = options?.WaitUntil ?? WaitUntilState.Load;167 using var waiter = SetupNavigationWaiter("frame.WaitForNavigationAsync", options?.Timeout);168 string toUrl = !string.IsNullOrEmpty(options?.UrlString) ? $" to \"{options?.UrlString}\"" : string.Empty;169 waiter.Log($"waiting for navigation{toUrl} until \"{waitUntil2}\"");170 var navigatedEventTask = waiter.WaitForEventAsync<FrameNavigatedEventArgs>(171 this,172 "Navigated",173 e =>174 {175 // Any failed navigation results in a rejection.176 if (e.Error != null)177 {178 return true;179 }180 waiter.Log($" navigated to \"{e.Url}\"");181 return UrlMatches(e.Url, options?.UrlString, options?.UrlRegex, options?.UrlFunc);182 });183 var navigatedEvent = await navigatedEventTask.ConfigureAwait(false);184 if (navigatedEvent.Error != null)185 {186 var ex = new PlaywrightException(navigatedEvent.Error);187 await waiter.WaitForPromiseAsync(Task.FromException<object>(ex)).ConfigureAwait(false);188 }189 if (!_loadStates.Select(s => s.ToValueString()).Contains(waitUntil2.ToValueString()))190 {191 await waiter.WaitForEventAsync<WaitUntilState>(192 this,193 "LoadState",194 e =>195 {196 waiter.Log($" \"{e}\" event fired");197 return e.ToValueString() == waitUntil2.ToValueString();198 }).ConfigureAwait(false);199 }200 var request = navigatedEvent.NewDocument?.Request?.Object;201 var response = request != null202 ? await waiter.WaitForPromiseAsync(request.FinalRequest.ResponseAsync()).ConfigureAwait(false)203 : null;204 return response;205 }206 public async Task<IResponse> RunAndWaitForNavigationAsync(Func<Task> action, FrameRunAndWaitForNavigationOptions options = default)207 {208 var result = WaitForNavigationAsync(new()209 {210 UrlString = options?.UrlString,211 UrlRegex = options?.UrlRegex,212 UrlFunc = options?.UrlFunc,213 WaitUntil = options?.WaitUntil,214 Timeout = options?.Timeout,215 });216 if (action != null)217 {218 await WrapApiBoundaryAsync(() => Task.WhenAll(result, action())).ConfigureAwait(false);219 }220 return await result.ConfigureAwait(false);221 }222 public Task TapAsync(string selector, FrameTapOptions options = default)223 => _channel.TapAsync(224 selector,225 modifiers: options?.Modifiers,226 position: options?.Position,227 timeout: options?.Timeout,228 force: options?.Force,229 noWaitAfter: options?.NoWaitAfter,230 trial: options?.Trial,231 strict: options?.Strict);232 internal Task<int> QueryCountAsync(string selector)233 => _channel.QueryCountAsync(selector);234 public Task<string> ContentAsync() => _channel.ContentAsync();235 public Task FocusAsync(string selector, FrameFocusOptions options = default)236 => _channel.FocusAsync(selector, options?.Timeout, options?.Strict);237 public Task TypeAsync(string selector, string text, FrameTypeOptions options = default)238 => _channel.TypeAsync(239 selector,240 text,241 delay: options?.Delay,242 timeout: options?.Timeout,243 noWaitAfter: options?.NoWaitAfter,244 strict: options?.Strict);245 public Task<string> GetAttributeAsync(string selector, string name, FrameGetAttributeOptions options = default)246 => _channel.GetAttributeAsync(selector, name, options?.Timeout, options?.Strict);247 public Task<string> InnerHTMLAsync(string selector, FrameInnerHTMLOptions options = default)248 => _channel.InnerHTMLAsync(selector, options?.Timeout, options?.Strict);249 public Task<string> InnerTextAsync(string selector, FrameInnerTextOptions options = default)250 => _channel.InnerTextAsync(selector, options?.Timeout, options?.Strict);251 public Task<string> TextContentAsync(string selector, FrameTextContentOptions options = default)252 => _channel.TextContentAsync(selector, options?.Timeout, options?.Strict);253 public Task HoverAsync(string selector, FrameHoverOptions options = default)254 => _channel.HoverAsync(255 selector,256 position: options?.Position,257 modifiers: options?.Modifiers,258 force: options?.Force,259 timeout: options?.Timeout,260 trial: options?.Trial,261 strict: options?.Strict);262 public Task PressAsync(string selector, string key, FramePressOptions options = default)263 => _channel.PressAsync(264 selector,265 key,266 delay: options?.Delay,267 timeout: options?.Timeout,268 noWaitAfter: options?.NoWaitAfter,269 strict: options?.Strict);270 public Task DispatchEventAsync(string selector, string type, object eventInit = default, FrameDispatchEventOptions options = default)271 => _channel.DispatchEventAsync(272 selector,273 type,274 ScriptsHelper.SerializedArgument(eventInit),275 options?.Timeout,276 options?.Strict);277 public Task FillAsync(string selector, string value, FrameFillOptions options = default)278 => _channel.FillAsync(selector, value, force: options?.Force, timeout: options?.Timeout, noWaitAfter: options?.NoWaitAfter, options?.Strict);279 public async Task<IElementHandle> AddScriptTagAsync(FrameAddScriptTagOptions options = default)280 {281 var content = options?.Content;282 if (!string.IsNullOrEmpty(options?.Path))283 {284 content = File.ReadAllText(options.Path);285 content += "//# sourceURL=" + options.Path.Replace("\n", string.Empty);286 }287 return (await _channel.AddScriptTagAsync(options?.Url, options?.Path, content, options?.Type).ConfigureAwait(false)).Object;288 }289 public async Task<IElementHandle> AddStyleTagAsync(FrameAddStyleTagOptions options = default)290 {291 var content = options?.Content;292 if (!string.IsNullOrEmpty(options?.Path))293 {294 content = File.ReadAllText(options.Path);295 content += "//# sourceURL=" + options.Path.Replace("\n", string.Empty);296 }297 return (await _channel.AddStyleTagAsync(options?.Url, options?.Path, content).ConfigureAwait(false)).Object;298 }299 public Task SetInputFilesAsync(string selector, string files, FrameSetInputFilesOptions options = default)300 => SetInputFilesAsync(selector, new[] { files }, options);301 public async Task SetInputFilesAsync(string selector, IEnumerable<string> files, FrameSetInputFilesOptions options = default)302 {303 var converted = await SetInputFilesHelpers.ConvertInputFilesAsync(files, (BrowserContext)Page.Context).ConfigureAwait(false);304 if (converted.Files != null)305 {306 await _channel.SetInputFilesAsync(selector, converted.Files, options?.NoWaitAfter, options?.Timeout, options?.Strict).ConfigureAwait(false);307 }308 else309 {310 await _channel.SetInputFilePathsAsync(selector, converted?.LocalPaths, converted?.Streams, options?.NoWaitAfter, options?.Timeout, options?.Strict).ConfigureAwait(false);311 }312 }313 public Task SetInputFilesAsync(string selector, FilePayload files, FrameSetInputFilesOptions options = default)314 => SetInputFilesAsync(selector, new[] { files }, options);315 public async Task SetInputFilesAsync(string selector, IEnumerable<FilePayload> files, FrameSetInputFilesOptions options = default)316 {317 var converted = SetInputFilesHelpers.ConvertInputFiles(files);318 await _channel.SetInputFilesAsync(selector, converted.Files, noWaitAfter: options?.NoWaitAfter, timeout: options?.Timeout, options?.Strict).ConfigureAwait(false);319 }320 public Task ClickAsync(string selector, FrameClickOptions options = default)321 => _channel.ClickAsync(322 selector,323 delay: options?.Delay,324 button: options?.Button,325 clickCount: options?.ClickCount,326 modifiers: options?.Modifiers,327 position: options?.Position,328 timeout: options?.Timeout,329 force: options?.Force,330 noWaitAfter: options?.NoWaitAfter,331 trial: options?.Trial,332 strict: options?.Strict);333 public Task DblClickAsync(string selector, FrameDblClickOptions options = default)334 => _channel.DblClickAsync(335 selector,336 delay: options?.Delay,337 button: options?.Button,338 position: options?.Position,339 modifiers: options?.Modifiers,340 timeout: options?.Timeout,341 force: options?.Force,342 noWaitAfter: options?.NoWaitAfter,343 trial: options?.Trial,344 strict: options?.Strict);345 public Task CheckAsync(string selector, FrameCheckOptions options = default)346 => _channel.CheckAsync(347 selector,348 position: options?.Position,349 timeout: options?.Timeout,350 force: options?.Force,351 noWaitAfter: options?.NoWaitAfter,352 trial: options?.Trial,353 strict: options?.Strict);354 public Task UncheckAsync(string selector, FrameUncheckOptions options = default)355 => _channel.UncheckAsync(356 selector,357 position: options?.Position,358 timeout: options?.Timeout,359 force: options?.Force,360 noWaitAfter: options?.NoWaitAfter,361 trial: options?.Trial,362 strict: options?.Strict);363 public Task SetCheckedAsync(string selector, bool checkedState, FrameSetCheckedOptions options = null)364 => checkedState ?365 _channel.CheckAsync(366 selector,367 position: options?.Position,368 timeout: options?.Timeout,369 force: options?.Force,370 noWaitAfter: options?.NoWaitAfter,371 trial: options?.Trial,372 strict: options?.Strict)373 : _channel.UncheckAsync(374 selector,375 position: options?.Position,376 timeout: options?.Timeout,377 force: options?.Force,378 noWaitAfter: options?.NoWaitAfter,379 trial: options?.Trial,380 strict: options?.Strict);381 public Task SetContentAsync(string html, FrameSetContentOptions options = default)382 => _channel.SetContentAsync(html, timeout: options?.Timeout, waitUntil: options?.WaitUntil);383 public Task<string> InputValueAsync(string selector, FrameInputValueOptions options = null)384 => _channel.InputValueAsync(selector, options?.Timeout, options?.Strict);385 public async Task<IElementHandle> QuerySelectorAsync(string selector)386 => (await _channel.QuerySelectorAsync(selector).ConfigureAwait(false))?.Object;387 public async Task<IReadOnlyList<IElementHandle>> QuerySelectorAllAsync(string selector)388 => (await _channel.QuerySelectorAllAsync(selector).ConfigureAwait(false)).Select(c => ((ElementHandleChannel)c).Object).ToList().AsReadOnly();389 public async Task<IJSHandle> WaitForFunctionAsync(string expression, object arg = default, FrameWaitForFunctionOptions options = default)390 => (await _channel.WaitForFunctionAsync(391 expression: expression,392 arg: ScriptsHelper.SerializedArgument(arg),393 timeout: options?.Timeout,394 polling: options?.PollingInterval).ConfigureAwait(false)).Object;395 public async Task<IElementHandle> WaitForSelectorAsync(string selector, FrameWaitForSelectorOptions options = default)396 => (await _channel.WaitForSelectorAsync(397 selector: selector,398 state: options?.State,399 timeout: options?.Timeout,400 strict: options?.Strict,401 omitReturnValue: false).ConfigureAwait(false))?.Object;402 public async Task<IElementHandle> LocatorWaitForAsync(string selector, LocatorWaitForOptions options = default)403 => (await _channel.WaitForSelectorAsync(404 selector: selector,405 state: options?.State,406 timeout: options?.Timeout,407 strict: true,408 omitReturnValue: true).ConfigureAwait(false))?.Object;409 public async Task<IJSHandle> EvaluateHandleAsync(string script, object args = null)410 => (await _channel.EvaluateExpressionHandleAsync(411 script,412 arg: ScriptsHelper.SerializedArgument(args)).ConfigureAwait(false))?.Object;413 public async Task<JsonElement?> EvaluateAsync(string script, object arg = null)414 => ScriptsHelper.ParseEvaluateResult<JsonElement?>(await _channel.EvaluateExpressionAsync(415 script,416 arg: ScriptsHelper.SerializedArgument(arg)).ConfigureAwait(false));417 public async Task<T> EvaluateAsync<T>(string script, object arg = null)418 => ScriptsHelper.ParseEvaluateResult<T>(await _channel.EvaluateExpressionAsync(419 script,420 arg: ScriptsHelper.SerializedArgument(arg)).ConfigureAwait(false));421 public async Task<JsonElement?> EvalOnSelectorAsync(string selector, string script, object arg = null)422 => ScriptsHelper.ParseEvaluateResult<JsonElement?>(await _channel.EvalOnSelectorAsync(423 selector: selector,424 script,425 arg: ScriptsHelper.SerializedArgument(arg),426 strict: null).ConfigureAwait(false));427 public async Task<T> EvalOnSelectorAsync<T>(string selector, string script, object arg = null)428 => ScriptsHelper.ParseEvaluateResult<T>(await _channel.EvalOnSelectorAsync(429 selector: selector,430 script,431 arg: ScriptsHelper.SerializedArgument(arg),432 strict: null).ConfigureAwait(false));433 public async Task<T> EvalOnSelectorAsync<T>(string selector, string expression, object arg = null, FrameEvalOnSelectorOptions options = null)434 => ScriptsHelper.ParseEvaluateResult<T>(await _channel.EvalOnSelectorAsync(435 selector: selector,436 expression,437 arg: ScriptsHelper.SerializedArgument(arg),438 strict: options?.Strict).ConfigureAwait(false));439 public async Task<JsonElement?> EvalOnSelectorAllAsync(string selector, string script, object arg = null)440 => ScriptsHelper.ParseEvaluateResult<JsonElement?>(await _channel.EvalOnSelectorAllAsync(441 selector: selector,442 script,443 arg: ScriptsHelper.SerializedArgument(arg)).ConfigureAwait(false));444 public async Task<T> EvalOnSelectorAllAsync<T>(string selector, string script, object arg = null)445 => ScriptsHelper.ParseEvaluateResult<T>(await _channel.EvalOnSelectorAllAsync(446 selector: selector,447 script,448 arg: ScriptsHelper.SerializedArgument(arg)).ConfigureAwait(false));449 public ILocator Locator(string selector, FrameLocatorOptions options = null) => new Locator(this, selector, new() { HasTextRegex = options?.HasTextRegex, HasTextString = options?.HasTextString, Has = options?.Has });450 public async Task<IElementHandle> QuerySelectorAsync(string selector, FrameQuerySelectorOptions options = null)451 => (await _channel.QuerySelectorAsync(selector, options?.Strict).ConfigureAwait(false))?.Object;452 public async Task<IResponse> GotoAsync(string url, FrameGotoOptions options = default)453 => (await _channel.GotoAsync(454 url,455 timeout: options?.Timeout,456 waitUntil: options?.WaitUntil,457 referer: options?.Referer).ConfigureAwait(false))?.Object;458 public Task<bool> IsCheckedAsync(string selector, FrameIsCheckedOptions options = default)459 => _channel.IsCheckedAsync(selector, timeout: options?.Timeout, options?.Strict);460 public Task<bool> IsDisabledAsync(string selector, FrameIsDisabledOptions options = default)461 => _channel.IsDisabledAsync(selector, timeout: options?.Timeout, options?.Strict);462 public Task<bool> IsEditableAsync(string selector, FrameIsEditableOptions options = default)463 => _channel.IsEditableAsync(selector, timeout: options?.Timeout, options?.Strict);464 public Task<bool> IsEnabledAsync(string selector, FrameIsEnabledOptions options = default)465 => _channel.IsEnabledAsync(selector, timeout: options?.Timeout, options?.Strict);466#pragma warning disable CS0612 // Type or member is obsolete467 public Task<bool> IsHiddenAsync(string selector, FrameIsHiddenOptions options = default)468 => _channel.IsHiddenAsync(selector, timeout: options?.Timeout, options?.Strict);469 public Task<bool> IsVisibleAsync(string selector, FrameIsVisibleOptions options = default)470 => _channel.IsVisibleAsync(selector, timeout: options?.Timeout, options?.Strict);471#pragma warning restore CS0612 // Type or member is obsolete472 public Task WaitForURLAsync(string url, FrameWaitForURLOptions options = default)473 => WaitForURLAsync(url, null, null, options);474 public Task WaitForURLAsync(Regex url, FrameWaitForURLOptions options = default)475 => WaitForURLAsync(null, url, null, options);476 public Task WaitForURLAsync(Func<string, bool> url, FrameWaitForURLOptions options = default)477 => WaitForURLAsync(null, null, url, options);478 public Task DragAndDropAsync(string source, string target, FrameDragAndDropOptions options = null)479 => _channel.DragAndDropAsync(source, target, options?.Force, options?.NoWaitAfter, options?.Timeout, options?.Trial, options?.Strict);480 internal Task<FrameExpectResult> ExpectAsync(string selector, string expression, FrameExpectOptions options = null) =>481 _channel.ExpectAsync(selector, expression, expressionArg: options?.ExpressionArg, expectedText: options?.ExpectedText, expectedNumber: options?.ExpectedNumber, expectedValue: options?.ExpectedValue, useInnerText: options?.UseInnerText, isNot: options?.IsNot, timeout: options?.Timeout);482 private Task WaitForURLAsync(string urlString, Regex urlRegex, Func<string, bool> urlFunc, FrameWaitForURLOptions options = default)483 {484 if (UrlMatches(Url, urlString, urlRegex, urlFunc))485 {486 return WaitForLoadStateAsync(ToLoadState(options?.WaitUntil), new() { Timeout = options?.Timeout });487 }488 return WaitForNavigationAsync(489 new()490 {491 UrlString = urlString,492 UrlRegex = urlRegex,493 UrlFunc = urlFunc,494 Timeout = options?.Timeout,495 WaitUntil = options?.WaitUntil,496 });497 }498 private LoadState? ToLoadState(WaitUntilState? waitUntilState)499 {500 if (waitUntilState == null)...
FrameChannel.cs
Source:FrameChannel.cs
...202 ["waitUntil"] = waitUntil,203 };204 return Connection.SendMessageToServerAsync<ResponseChannel>(Guid, "waitForNavigation", param);205 }206 internal Task WaitForLoadStateAsync(LoadState? state, float? timeout)207 {208 var param = new Dictionary<string, object>209 {210 ["timeout"] = timeout,211 ["state"] = state,212 };213 return Connection.SendMessageToServerAsync(214 Guid,215 "waitForLoadState",216 param);217 }218 internal async Task<int> QueryCountAsync(string selector)219 {220 var args = new Dictionary<string, object>...
PageAutoWaitingBasicTests.cs
Source:PageAutoWaitingBasicTests.cs
...199 });200 await Page.SetContentAsync($"<a href=\"{Server.EmptyPage}\">empty.html</a>");201 var clickLoaded = new TaskCompletionSource<bool>();202 await TaskUtils.WhenAll(203 Page.ClickAsync("a").ContinueWith(_ => Page.WaitForLoadStateAsync(LoadState.Load).ContinueWith(_ =>204 {205 messages.Add("clickload");206 clickLoaded.TrySetResult(true);207 })),208 clickLoaded.Task,209 Page.WaitForNavigationAsync(new() { WaitUntil = WaitUntilState.DOMContentLoaded }).ContinueWith(_ => messages.Add("domcontentloaded")));210 Assert.AreEqual("route|domcontentloaded|clickload", string.Join("|", messages));211 }212 [PlaywrightTest("page-autowaiting-basic.spec.ts", "should work with goto following click")]213 public async Task ShouldWorkWithGotoFollowingClick()214 {215 var messages = new List<string>();216 Server.SetRoute("/empty.html", context =>217 {...
PageNetworkIdleTests.cs
Source:PageNetworkIdleTests.cs
...122 var popupTask = Page.WaitForPopupAsync();123 await Task.WhenAll(124 Page.WaitForPopupAsync(),125 Page.ClickAsync("#box" + i));126 await popupTask.Result.WaitForLoadStateAsync(LoadState.NetworkIdle);127 }128 }129 private async Task NetworkIdleTestAsync(IFrame frame, Func<Task> action = default, bool isSetContent = false)130 {131 var lastResponseFinished = new Stopwatch();132 var responses = new ConcurrentDictionary<string, TaskCompletionSource<bool>>();133 var fetches = new Dictionary<string, TaskCompletionSource<bool>>();134 async Task RequestDelegate(HttpContext context)135 {136 var taskCompletion = new TaskCompletionSource<bool>();137 responses[context.Request.Path] = taskCompletion;138 fetches[context.Request.Path].TrySetResult(true);139 await taskCompletion.Task;140 lastResponseFinished.Restart();...
PageWaitForLoadStateTests.cs
Source:PageWaitForLoadStateTests.cs
...48 }49 });50 var navigationTask = Page.GotoAsync(Server.Prefix + "/one-style.html");51 await waitForRequestTask;52 var waitLoadTask = Page.WaitForLoadStateAsync();53 responseTask.TrySetResult(true);54 await waitLoadTask;55 await navigationTask;56 }57 [PlaywrightTest("page-wait-for-load-state.ts", "should respect timeout")]58 public async Task ShouldRespectTimeout()59 {60 Server.SetRoute("/one-style.css", _ => Task.Delay(Timeout.Infinite));61 await Page.GotoAsync(Server.Prefix + "/one-style.html", new() { WaitUntil = WaitUntilState.DOMContentLoaded });62 var exception = await PlaywrightAssert.ThrowsAsync<TimeoutException>(() => Page.WaitForLoadStateAsync(LoadState.Load, new() { Timeout = 1 }));63 StringAssert.Contains("Timeout 1ms exceeded", exception.Message);64 }65 [PlaywrightTest("page-wait-for-load-state.ts", "should resolve immediately if loaded")]66 public async Task ShouldResolveImmediatelyIfLoaded()67 {68 await Page.GotoAsync(Server.Prefix + "/one-style.html");69 await Page.WaitForLoadStateAsync();70 }71 [PlaywrightTest("page-wait-for-load-state.ts", "should resolve immediately if load state matches")]72 public async Task ShouldResolveImmediatelyIfLoadStateMatches()73 {74 var responseTask = new TaskCompletionSource<bool>();75 var waitForRequestTask = Server.WaitForRequest("/one-style.css");76 Server.SetRoute("/one-style.css", async (ctx) =>77 {78 if (await responseTask.Task)79 {80 ctx.Response.StatusCode = 404;81 await ctx.Response.WriteAsync("File not found");82 }83 });84 var navigationTask = Page.GotoAsync(Server.Prefix + "/one-style.html");85 await waitForRequestTask;86 await Page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);87 responseTask.TrySetResult(true);88 await navigationTask;89 }90 [PlaywrightTest("page-wait-for-load-state.ts", "should work with pages that have loaded before being connected to")]91 [Skip(SkipAttribute.Targets.Firefox, SkipAttribute.Targets.Webkit)]92 public async Task ShouldWorkWithPagesThatHaveLoadedBeforeBeingConnectedTo()93 {94 await Page.GotoAsync(Server.EmptyPage);95 await Page.EvaluateAsync(@"async () => {96 const child = window.open(document.location.href);97 while (child.document.readyState !== 'complete' || child.document.location.href === 'about:blank')98 await new Promise(f => setTimeout(f, 100));99 }");100 var pages = Context.Pages;101 Assert.AreEqual(2, pages.Count);102 // order is not guaranteed103 var mainPage = pages.FirstOrDefault(p => ReferenceEquals(Page, p));104 var connectedPage = pages.Single(p => !ReferenceEquals(Page, p));105 Assert.NotNull(mainPage);106 Assert.AreEqual(Server.EmptyPage, mainPage.Url);107 Assert.AreEqual(Server.EmptyPage, connectedPage.Url);108 await connectedPage.WaitForLoadStateAsync();109 Assert.AreEqual(Server.EmptyPage, connectedPage.Url);110 }111 [PlaywrightTest("page-wait-for-load-state.spec.ts", "should work for frame")]112 public async Task ShouldWorkForFrame()113 {114 await Page.GotoAsync(Server.Prefix + "/frames/one-frame.html");115 var frame = Page.Frames.ElementAt(1);116 TaskCompletionSource<bool> requestTask = new TaskCompletionSource<bool>();117 TaskCompletionSource<bool> routeReachedTask = new TaskCompletionSource<bool>();118 await Page.RouteAsync(Server.Prefix + "/one-style.css", async (route) =>119 {120 routeReachedTask.TrySetResult(true);121 await requestTask.Task;122 await route.ContinueAsync();123 });124 await frame.GotoAsync(Server.Prefix + "/one-style.html", new() { WaitUntil = WaitUntilState.DOMContentLoaded });125 await routeReachedTask.Task;126 var loadTask = frame.WaitForLoadStateAsync();127 await Page.EvaluateAsync("1");128 Assert.False(loadTask.IsCompleted);129 requestTask.TrySetResult(true);130 await loadTask;131 }132 }133}...
WaitForLoadStateAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using Microsoft.Playwright;4{5 {6 static async Task Main(string[] args)7 {8 using var playwright = await Playwright.CreateAsync();9 var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions10 {11 });12 var context = await browser.NewContextAsync();13 var page = await context.NewPageAsync();14 await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);15 await page.ScreenshotAsync("screenshot.png");16 await browser.CloseAsync();17 }18 }19}20using System;21using System.Threading.Tasks;22using Microsoft.Playwright;23{24 {25 static async Task Main(string[] args)26 {27 using var playwright = await Playwright.CreateAsync();28 var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions29 {30 });31 var context = await browser.NewContextAsync();32 var page = await context.NewPageAsync();33 await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded, 10000);34 await page.ScreenshotAsync("screenshot.png");35 await browser.CloseAsync();36 }37 }38}
WaitForLoadStateAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using Microsoft.Playwright;4{5 {6 static async Task Main(string[] args)7 {8 using var playwright = await Playwright.CreateAsync();9 await using var browser = await playwright.Firefox.LaunchAsync();10 var page = await browser.NewPageAsync();11 await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);12 }13 }14}15using System;16using System.Threading.Tasks;17using Microsoft.Playwright;18{19 {20 static async Task Main(string[] args)21 {22 using var playwright = await Playwright.CreateAsync();23 await using var browser = await playwright.Firefox.LaunchAsync();24 var page = await browser.NewPageAsync();25 await page.WaitForNavigationAsync();26 }27 }28}29using System;30using System.Threading.Tasks;31using Microsoft.Playwright;32{33 {34 static async Task Main(string[] args)35 {36 using var playwright = await Playwright.CreateAsync();37 await using var browser = await playwright.Firefox.LaunchAsync();38 var page = await browser.NewPageAsync();39 var request = await page.WaitForRequestAsync("**/*");40 }41 }42}43using System;44using System.Threading.Tasks;45using Microsoft.Playwright;46{47 {48 static async Task Main(string[] args)49 {50 using var playwright = await Playwright.CreateAsync();51 await using var browser = await playwright.Firefox.LaunchAsync();52 var page = await browser.NewPageAsync();53 var response = await page.WaitForResponseAsync("**/*");54 }55 }56}
WaitForLoadStateAsync
Using AI Code Generation
1using Microsoft.Playwright;2using System.Threading.Tasks;3{4 {5 static async Task Main(string[] args)6 {7 using var playwright = await Playwright.CreateAsync();8 await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });9 var page = await browser.NewPageAsync();10 await page.ClickAsync("input[name=q]");11 await page.FillAsync("input[name=q]", "Hello World");12 await page.PressAsync("input[name=q]", "Enter");13 await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);14 await page.ScreenshotAsync("screenshot.png");15 }16 }17}
WaitForLoadStateAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using Microsoft.Playwright;4using Microsoft.Playwright.Core;5{6 {7 static async Task Main(string[] args)8 {9 using var playwright = await Playwright.CreateAsync();10 await using var browser = await playwright.Chromium.LaunchAsync(new LaunchOptions11 {12 });13 var page = await browser.NewPageAsync();14 await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);15 await page.WaitForLoadStateAsync();16 await page.WaitForLoadStateAsync(LoadState.NetworkIdle);17 await page.WaitForTimeoutAsync(5000);18 await browser.CloseAsync();19 }20 }21}
WaitForLoadStateAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using Microsoft.Playwright;4using Microsoft.Playwright.Core;5{6 {7 static async Task Main(string[] args)8 {9 using var playwright = await Playwright.CreateAsync();10 var browser = await playwright.Chromium.LaunchAsync();11 var context = await browser.NewContextAsync();12 var page = await context.NewPageAsync();13 await page.WaitForLoadStateAsync(LoadState.Networkidle);14 await page.WaitForSelectorAsync("text=Sign in");15 await page.ClickAsync("text=
WaitForLoadStateAsync
Using AI Code Generation
1var frame = await page.MainFrame();2await frame.WaitForLoadStateAsync(LoadState.DOMContentLoaded);3var frame = await page.MainFrame();4await frame.WaitForLoadStateAsync(LoadState.Load);5var frame = await page.MainFrame();6await frame.WaitForLoadStateAsync(LoadState.NetworkIdle);7var frame = await page.MainFrame();8await frame.WaitForLoadStateAsync(LoadState.DOMContentLoaded, LoadState.Load);9var frame = await page.MainFrame();10await frame.WaitForLoadStateAsync(LoadState.DOMContentLoaded, LoadState.Load, LoadState.NetworkIdle);11var frame = await page.MainFrame();12await frame.WaitForLoadStateAsync(LoadState.DOMContentLoaded, LoadState.NetworkIdle);13var frame = await page.MainFrame();14await frame.WaitForLoadStateAsync(LoadState.Load, LoadState.NetworkIdle);15var frame = await page.MainFrame();16await frame.WaitForLoadStateAsync(LoadState.DOMContentLoaded, LoadState.Load, LoadState.NetworkIdle, TimeSpan.FromSeconds(2));17var frame = await page.MainFrame();18await frame.WaitForLoadStateAsync(LoadState.DOMContentLoaded, LoadState.Load, LoadState.NetworkIdle, TimeSpan
WaitForLoadStateAsync
Using AI Code Generation
1using System.Threading.Tasks;2using Microsoft.Playwright;3using Microsoft.Playwright.Core;4{5 {6 static async Task Main(string[] args)7 {8 using var playwright = await Playwright.CreateAsync();9 await using var browser = await playwright.Chromium.LaunchAsync();10 var context = await browser.NewContextAsync();11 var page = await context.NewPageAsync();12 await page.ClickAsync("text=Sign in");13 await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);14 await page.ScreenshotAsync("2.png");15 }16 }17}
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!