How to use InitializeAsync method of PuppeteerSharp.Tests.BrowserContextTests.DefaultBrowserContextTests class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.BrowserContextTests.DefaultBrowserContextTests.InitializeAsync

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

DefaultBrowserContextTests.cs

Source: DefaultBrowserContextTests.cs Github

copy
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using Xunit;
7using Xunit.Abstractions;
8
9namespace PuppeteerSharp.Tests.BrowserContextTests
10{
11    [Collection(TestConstants.TestFixtureCollectionName)]
12    public class DefaultBrowserContextTests : PuppeteerPageBaseTest
13    {
14        public DefaultBrowserContextTests(ITestOutputHelper output) : base(output)
15        {
16        }
17
18        public override async Task InitializeAsync()
19        {
20            await base.InitializeAsync();
21
22            Context = Browser.DefaultContext;
23            Page = await Context.NewPageAsync();
24        }
25
26        [Fact]
27        public async Task PageGetCookiesAsyncShouldWork()
28        {
29            await Page.GoToAsync(TestConstants.EmptyPage);
30
31            await Page.EvaluateExpressionAsync("document.cookie = 'username=John Doe'");
32            var cookie = (await Page.GetCookiesAsync()).FirstOrDefault();
33            Assert.Equal("username", cookie.Name);
34            Assert.Equal("John Doe", cookie.Value);
35            Assert.Equal("localhost", cookie.Domain);
36            Assert.Equal("/", cookie.Path);
37            Assert.Equal(-1, cookie.Expires);
38            Assert.Equal(16, cookie.Size);
39            Assert.False(cookie.HttpOnly);
40            Assert.False(cookie.Secure);
41            Assert.True(cookie.Session);
42        }
43
44        [Fact]
45        public async Task PageSetCookiesAsyncShouldWork()
46        {
47            await Page.GoToAsync(TestConstants.EmptyPage);
48
49            await Page.SetCookieAsync(new CookieParam
50            {
51                Name = "username",
52                Value = "John Doe"
53            });
54
55            var cookie = (await Page.GetCookiesAsync()).FirstOrDefault();
56            Assert.Equal("username", cookie.Name);
57            Assert.Equal("John Doe", cookie.Value);
58            Assert.Equal("localhost", cookie.Domain);
59            Assert.Equal("/", cookie.Path);
60            Assert.Equal(-1, cookie.Expires);
61            Assert.Equal(16, cookie.Size);
62            Assert.False(cookie.HttpOnly);
63            Assert.False(cookie.Secure);
64            Assert.True(cookie.Session);
65        }
66
67        [Fact]
68        public async Task PageDeleteCookieAsyncShouldWork()
69        {
70            await Page.GoToAsync(TestConstants.EmptyPage);
71
72            await Page.SetCookieAsync(
73                new CookieParam
74                {
75                    Name = "cookie1",
76                    Value = "1"
77                },
78                new CookieParam
79                {
80                    Name = "cookie2",
81                    Value = "2"
82                });
83
84            Assert.Equal("cookie1=1; cookie2=2", await Page.EvaluateExpressionAsync<string>("document.cookie"));
85            await Page.DeleteCookieAsync(new CookieParam
86            {
87                Name = "cookie2"
88            });
89            Assert.Equal("cookie1=1", await Page.EvaluateExpressionAsync<string>("document.cookie"));
90
91            var cookie = (await Page.GetCookiesAsync()).FirstOrDefault();
92            Assert.Equal("cookie1", cookie.Name);
93            Assert.Equal("1", cookie.Value);
94            Assert.Equal("localhost", cookie.Domain);
95            Assert.Equal("/", cookie.Path);
96            Assert.Equal(-1, cookie.Expires);
97            Assert.Equal(8, cookie.Size);
98            Assert.False(cookie.HttpOnly);
99            Assert.False(cookie.Secure);
100            Assert.True(cookie.Session);
101        }
102    }
103}
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

Trigger InitializeAsync code on LambdaTest Cloud Grid

Execute automation tests with InitializeAsync on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
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)