How to use QuerySelectorAsync method of PuppeteerSharp.Frame class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Frame.QuerySelectorAsync

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

AuthIntegrationTests.cs

Source: AuthIntegrationTests.cs Github

copy
1using System;
2using System.Linq;
3using System.Threading.Tasks;
4
5using FluentAssertions;
6
7using Microsoft.EntityFrameworkCore;
8using Microsoft.Extensions.DependencyInjection;
9
10using NUnit.Framework;
11
12using PuppeteerSharp;
13
14namespace Brighid.Identity.Auth
15{
16    [TestFixture]
17    [Category("Integration")]
18    public class AuthIntegrationTests
19    {
20        private const string Email = "[email protected]";
21        private const string Password = "Password123!";
22
23        [Test]
24        [Auto]
25        public async Task LoginRedirect(
26            AppFactory app,
27            Browser browser
28        )
29        {
30            var page = await browser.NewPageAsync();
31            await page.GoToAsync(app.RootUri.ToString());
32
33            // We should be redirected to login
34            new Uri(page.Url).AbsolutePath.Should().Be("/login");
35        }
36
37        [Test]
38        [Auto]
39        public async Task SignupThenLogin(
40            AppFactory app,
41            Browser browser
42        )
43        {
44            var page = await browser.NewPageAsync();
45            await page.GoToAsync($"{app.RootUri}signup");
46            {
47                var emailField = await page.MainFrame.QuerySelectorAsync("#Email");
48                await emailField.TypeAsync(Email);
49
50                var passwordField = await page.MainFrame.QuerySelectorAsync("#Password");
51                await passwordField.TypeAsync(Password);
52
53                var confirmPasswordField = await page.MainFrame.QuerySelectorAsync("#ConfirmPassword");
54                await confirmPasswordField.TypeAsync(Password);
55
56                var submitButton = await page.MainFrame.QuerySelectorAsync("input[type=submit]");
57                await submitButton.ClickAsync();
58
59                await page.MainFrame.WaitForNavigationAsync();
60            }
61
62            {
63                using var scope = app.Services.CreateScope();
64                var dbContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
65                var query = from user in dbContext.Users.AsQueryable() where user.Email == Email select user;
66                var exists = await query.AnyAsync();
67
68                exists.Should().Be(true);
69            }
70
71            {
72                var cookies = await page.GetCookiesAsync();
73                await page.DeleteCookieAsync(cookies);
74                await page.GoToAsync($"{app.RootUri}login", waitUntil: WaitUntilNavigation.DOMContentLoaded);
75            }
76
77            {
78                var emailField = await page.MainFrame.QuerySelectorAsync("#Email");
79                await emailField.TypeAsync(Email);
80
81                var passwordField = await page.MainFrame.QuerySelectorAsync("#Password");
82                await passwordField.TypeAsync(Password);
83
84                var submitButton = await page.MainFrame.QuerySelectorAsync("input[type=submit]");
85                await submitButton.ClickAsync();
86
87                await page.MainFrame.WaitForNavigationAsync();
88
89                var url = new Uri(page.Url);
90                url.AbsolutePath.Should().Be("/");
91            }
92        }
93    }
94}
95
Full Screen

PuppeteerExtensions.cs

Source: PuppeteerExtensions.cs Github

copy
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
5using PuppeteerSharp;
6
7namespace SpotifyBot.PuppeteerPrelude
8{
9    public static class PuppeteerExtensions
10    {
11        public static async Task UseProxyAuth(this Page page, string credentials)
12        {
13            var authStr = $"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials))}";
14            await page.SetExtraHttpHeadersAsync(new Dictionary<string, string>
15            {
16                ["Proxy-Authorization"] = authStr
17            });
18        }
19
20        public static async Task TypeAsync(this Frame frame, string selector, string text)
21        {
22            await (await frame.QuerySelectorAsync(selector)).TypeAsync(text);
23        }
24
25        public static async Task ClickAsync(this Frame frame, string selector)
26        {
27            await (await frame.QuerySelectorAsync(selector)).ClickAsync();
28        }
29
30        public static async Task WaitForTruth(this Page page, string script, WaitForFunctionOptions opts = null)
31        {
32            var jsHandle = await page.WaitForExpressionAsync(script, opts);
33            await jsHandle.DisposeAsync();
34        }
35
36        public static async Task WaitForDocumentInteractiveState(this Page page, int? timeout = null)
37        {
38            await page.WaitForTruth("document.readyState === 'interactive' || document.readyState === 'complete'", new WaitForFunctionOptions { Timeout = timeout ?? page.Browser.DefaultWaitForTimeout });
39        }
40    }
41}
42
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)