How to use WaitForSelectorAsync method of Microsoft.Playwright.Core.Frame class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Core.Frame.WaitForSelectorAsync

Run Playwright-dotnet automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

BlazorServerTemplateTest.cs

Source: BlazorServerTemplateTest.cs Github

copy
1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3
4using System;
5using System.Collections.Generic;
6using System.Linq;
7using System.Net;
8using System.Threading.Tasks;
9using Microsoft.AspNetCore.BrowserTesting;
10using Microsoft.AspNetCore.Testing;
11using Microsoft.Playwright;
12using ProjectTemplates.Tests.Infrastructure;
13using Templates.Test.Helpers;
14using Xunit;
15using Xunit.Abstractions;
16
17namespace BlazorTemplates.Tests;
18
19public class BlazorServerTemplateTest : BlazorTemplateTest
20{
21    public BlazorServerTemplateTest(ProjectFactoryFixture projectFactory)
22        : base(projectFactory)
23    {
24    }
25
26    public override string ProjectType { get; } = "blazorserver";
27
28    [Theory(Skip = "https://github.com/dotnet/aspnetcore/issues/30761")]
29    [InlineData(BrowserKind.Chromium)]
30    public async Task BlazorServerTemplateWorks_NoAuth(BrowserKind browserKind)
31    {
32        var project = await CreateBuildPublishAsync("blazorservernoauth" + browserKind);
33
34        await using var browser = BrowserManager.IsAvailable(browserKind) ?
35            await BrowserManager.GetBrowserInstance(browserKind, BrowserContextInfo) :
36            null;
37
38        using (var aspNetProcess = project.StartBuiltProjectAsync())
39        {
40            Assert.False(
41                aspNetProcess.Process.HasExited,
42                ErrorMessages.GetFailedProcessMessageOrEmpty("Run built project", project, aspNetProcess.Process));
43
44            await aspNetProcess.AssertStatusCode("/", HttpStatusCode.OK, "text/html");
45
46            if (BrowserManager.IsAvailable(browserKind))
47            {
48                var page = await browser.NewPageAsync();
49                await aspNetProcess.VisitInBrowserAsync(page);
50                await TestBasicNavigation(page);
51                await page.CloseAsync();
52            }
53            else
54            {
55                EnsureBrowserAvailable(browserKind);
56            }
57        }
58
59        using (var aspNetProcess = project.StartPublishedProjectAsync())
60        {
61            Assert.False(
62                aspNetProcess.Process.HasExited,
63                ErrorMessages.GetFailedProcessMessageOrEmpty("Run published project", project, aspNetProcess.Process));
64
65            await aspNetProcess.AssertStatusCode("/", HttpStatusCode.OK, "text/html");
66            if (BrowserManager.IsAvailable(browserKind))
67            {
68                var page = await browser.NewPageAsync();
69                await aspNetProcess.VisitInBrowserAsync(page);
70                await TestBasicNavigation(page);
71                await page.CloseAsync();
72            }
73            else
74            {
75                EnsureBrowserAvailable(browserKind);
76            }
77        }
78    }
79
80    public static IEnumerable<object[]> BlazorServerTemplateWorks_IndividualAuthData =>
81            BrowserManager.WithBrowsers(new[] { BrowserKind.Chromium }, true, false);
82
83    [Theory(Skip = "https://github.com/dotnet/aspnetcore/issues/30882")]
84    [MemberData(nameof(BlazorServerTemplateWorks_IndividualAuthData))]
85    [SkipOnHelix("https://github.com/dotnet/aspnetcore/issues/30825", Queues = "All.OSX")]
86    public async Task BlazorServerTemplateWorks_IndividualAuth(BrowserKind browserKind, bool useLocalDB)
87    {
88        var project = await CreateBuildPublishAsync("blazorserverindividual" + browserKind + (useLocalDB ? "uld" : ""));
89
90        var browser = !BrowserManager.IsAvailable(browserKind) ?
91            null :
92            await BrowserManager.GetBrowserInstance(browserKind, BrowserContextInfo);
93
94        using (var aspNetProcess = project.StartBuiltProjectAsync())
95        {
96            Assert.False(
97                aspNetProcess.Process.HasExited,
98                ErrorMessages.GetFailedProcessMessageOrEmpty("Run built project", project, aspNetProcess.Process));
99
100            await aspNetProcess.AssertStatusCode("/", HttpStatusCode.OK, "text/html");
101            if (BrowserManager.IsAvailable(browserKind))
102            {
103                var page = await browser.NewPageAsync();
104                await aspNetProcess.VisitInBrowserAsync(page);
105                await TestBasicNavigation(page);
106                await page.CloseAsync();
107            }
108            else
109            {
110                EnsureBrowserAvailable(browserKind);
111            }
112        }
113
114        using (var aspNetProcess = project.StartPublishedProjectAsync())
115        {
116            Assert.False(
117                aspNetProcess.Process.HasExited,
118                ErrorMessages.GetFailedProcessMessageOrEmpty("Run published project", project, aspNetProcess.Process));
119
120            await aspNetProcess.AssertStatusCode("/", HttpStatusCode.OK, "text/html");
121            if (BrowserManager.IsAvailable(browserKind))
122            {
123                var page = await browser.NewPageAsync();
124                await aspNetProcess.VisitInBrowserAsync(page);
125                await TestBasicNavigation(page);
126                await page.CloseAsync();
127            }
128            else
129            {
130                EnsureBrowserAvailable(browserKind);
131            }
132        }
133    }
134
135    private async Task TestBasicNavigation(IPage page)
136    {
137        var socket = await page.WaitForWebSocketAsync();
138
139        var framesReceived = 0;
140        var framesSent = 0;
141
142        void FrameReceived(object sender, IWebSocketFrame frame) { framesReceived++; }
143        void FrameSent(object sender, IWebSocketFrame frame) { framesSent++; }
144
145        socket.FrameReceived += FrameReceived;
146        socket.FrameSent += FrameSent;
147
148        // Receive render batch
149        await page.WaitForWebSocketAsync(new() { Predicate = (s) => framesReceived == 1 });
150        await page.WaitForWebSocketAsync(new() { Predicate = (s) => framesSent == 1 });
151
152        // JS interop call to intercept navigation
153        await page.WaitForWebSocketAsync(new() { Predicate = (s) => framesReceived == 2 });
154        await page.WaitForWebSocketAsync(new() { Predicate = (s) => framesSent == 2 });
155
156        socket.FrameReceived -= FrameReceived;
157        socket.FrameSent -= FrameSent;
158
159        await page.WaitForSelectorAsync("nav");
160        // <title> element gets project ID injected into it during template execution
161        Assert.Equal("Index", (await page.TitleAsync()).Trim());
162
163        // Initially displays the home page
164        await page.WaitForSelectorAsync("h1 >> text=Hello, world!");
165
166        // Can navigate to the counter page
167        await page.ClickAsync("a[href=counter] >> text=Counter");
168        await page.WaitForSelectorAsync("h1+p >> text=Current count: 0");
169
170        // Clicking the counter button works
171        await page.ClickAsync("p+button >> text=Click me");
172        await page.WaitForSelectorAsync("h1+p >> text=Current count: 1");
173
174        // Can navigate to the 'fetch data' page
175        await page.ClickAsync("a[href=fetchdata] >> text=Fetch data");
176        await page.WaitForSelectorAsync("h1 >> text=Weather forecast");
177
178        // Asynchronously loads and displays the table of weather forecasts
179        await page.WaitForSelectorAsync("table>tbody>tr");
180        Assert.Equal(5, await page.Locator("p+table>tbody>tr").CountAsync());
181    }
182
183    [Theory(Skip = "https://github.com/dotnet/aspnetcore/issues/30882")]
184    [InlineData("IndividualB2C", null)]
185    [InlineData("IndividualB2C", new string[] { "--called-api-url \"https://graph.microsoft.com\"", "--called-api-scopes user.readwrite" })]
186    [InlineData("SingleOrg", null)]
187    [InlineData("SingleOrg", new string[] { "--called-api-url \"https://graph.microsoft.com\"", "--called-api-scopes user.readwrite" })]
188    [InlineData("SingleOrg", new string[] { "--calls-graph" })]
189    public Task BlazorServerTemplate_IdentityWeb_BuildAndPublish(string auth, string[] args)
190        => CreateBuildPublishAsync("blazorserveridweb" + Guid.NewGuid().ToString().Substring(0, 10).ToLowerInvariant(), auth, args);
191}
192
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)