Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.BrowserContextAddCookiesTests.AssertEqual
BrowserContextAddCookiesTests.cs
Source:BrowserContextAddCookiesTests.cs  
...66                Secure = c.Secure,67                Value = c.Value68            }));69            var newCookies = await Context.CookiesAsync();70            AssertEqual(cookies, newCookies);71        }72        [PlaywrightTest("browsercontext-add-cookies.spec.ts", "should send cookie header")]73        public async Task ShouldSendCookieHeader()74        {75            string cookie = string.Empty;76            Server.SetRoute("/empty.html", context =>77            {78                cookie = string.Join(";", context.Request.Cookies.Select(c => $"{c.Key}={c.Value}"));79                return Task.CompletedTask;80            });81            await Context.AddCookiesAsync(new[]82            {83                new Cookie84                {85                    Name = "cookie",86                    Value = "value",87                    Url = Server.EmptyPage88                }89            });90            await Context.NewPageAsync();91            await Page.GotoAsync(Server.EmptyPage);92            Assert.AreEqual("cookie=value", cookie);93        }94        [PlaywrightTest("browsercontext-add-cookies.spec.ts", "should isolate cookies in browser contexts")]95        public async Task ShouldIsolateCookiesInBrowserContexts()96        {97            await using var anotherContext = await Browser.NewContextAsync();98            await Context.AddCookiesAsync(new[]99            {100                new Cookie101                {102                    Name = "isolatecookie",103                    Value = "page1value",104                    Url = Server.EmptyPage105                }106            });107            await anotherContext.AddCookiesAsync(new[]108            {109                new Cookie110                {111                    Name = "isolatecookie",112                    Value = "page2value",113                    Url = Server.EmptyPage114                }115            });116            var cookies1 = await Context.CookiesAsync();117            var cookies2 = await anotherContext.CookiesAsync();118            Assert.That(cookies1, Has.Count.EqualTo(1));119            Assert.That(cookies2, Has.Count.EqualTo(1));120            Assert.AreEqual("isolatecookie", cookies1.ElementAt(0).Name);121            Assert.AreEqual("page1value", cookies1.ElementAt(0).Value);122            Assert.AreEqual("isolatecookie", cookies2.ElementAt(0).Name);123            Assert.AreEqual("page2value", cookies2.ElementAt(0).Value);124        }125        [PlaywrightTest("browsercontext-add-cookies.spec.ts", "should isolate session cookies")]126        public async Task ShouldIsolateSessionCookies()127        {128            Server.SetRoute("/setcookie.html", context =>129            {130                context.Response.Cookies.Append("session", "value");131                return Task.CompletedTask;132            });133            var page = await Context.NewPageAsync();134            await page.GotoAsync(Server.Prefix + "/setcookie.html");135            page = await Context.NewPageAsync();136            await page.GotoAsync(Server.EmptyPage);137            await using var context2 = await Browser.NewContextAsync();138            page = await context2.NewPageAsync();139            await page.GotoAsync(Server.EmptyPage);140            var cookies = await context2.CookiesAsync();141            Assert.That(cookies, Is.Empty);142        }143        [PlaywrightTest("browsercontext-add-cookies.spec.ts", "should isolate persistent cookies")]144        public async Task ShouldIsolatePersistentCookies()145        {146            Server.SetRoute("/setcookie.html", context =>147            {148                context.Response.Cookies.Append("persistent", "persistent-value", new() { MaxAge = TimeSpan.FromSeconds(3600) });149                return Task.CompletedTask;150            });151            var page = await Context.NewPageAsync();152            await page.GotoAsync(Server.Prefix + "/setcookie.html");153            await using var context2 = await Browser.NewContextAsync();154            var (page1, page2) = await TaskUtils.WhenAll(Context.NewPageAsync(), context2.NewPageAsync());155            await TaskUtils.WhenAll(156                page1.GotoAsync(Server.EmptyPage),157                page2.GotoAsync(Server.EmptyPage));158            var (cookies1, cookies2) = await TaskUtils.WhenAll(Context.CookiesAsync(), context2.CookiesAsync());159            Assert.That(cookies1, Has.Count.EqualTo(1));160            Assert.AreEqual("persistent", cookies1[0].Name);161            Assert.AreEqual("persistent-value", cookies1[0].Value);162            Assert.That(cookies2, Is.Empty);163        }164        [PlaywrightTest("browsercontext-add-cookies.spec.ts", "should isolate send cookie header")]165        public async Task ShouldIsolateSendCookieHeader()166        {167            string cookie = string.Empty;168            Server.SetRoute("/empty.html", context =>169            {170                cookie = string.Join(";", context.Request.Cookies.Select(c => $"{c.Key}={c.Value}"));171                return Task.CompletedTask;172            });173            await Context.AddCookiesAsync(new[]174            {175                new Cookie176                {177                    Name = "sendcookie",178                    Value = "value",179                    Url = Server.EmptyPage180                }181            });182            var page = await Context.NewPageAsync();183            await page.GotoAsync(Server.Prefix + "/empty.html");184            Assert.AreEqual("sendcookie=value", cookie);185            var context = await Browser.NewContextAsync();186            page = await context.NewPageAsync();187            await page.GotoAsync(Server.EmptyPage);188            Assert.That(cookie, Is.Null.Or.Empty);189        }190        [PlaywrightTest("browsercontext-add-cookies.spec.ts", "should isolate cookies between launches")]191        public async Task ShouldIsolateCookiesBetweenLaunches()192        {193            await using (await Playwright[TestConstants.BrowserName].LaunchAsync())194            {195                await Browser.NewContextAsync();196                await Context.AddCookiesAsync(new[]197                {198                    new Cookie199                    {200                        Name = "cookie-in-context-1",201                        Value = "value",202                        Expires = DateTimeOffset.Now.ToUnixTimeSeconds() + 10000,203                        Url = Server.EmptyPage204                    }205                });206            }207            await using (await Playwright[TestConstants.BrowserName].LaunchAsync())208            {209                var context1 = await Browser.NewContextAsync();210                var cookies = await context1.CookiesAsync();211                Assert.That(cookies, Is.Empty);212            }213        }214        [PlaywrightTest("browsercontext-add-cookies.spec.ts", "should set multiple cookies")]215        public async Task ShouldSetMultipleCookies()216        {217            await Page.GotoAsync(Server.EmptyPage);218            await Context.AddCookiesAsync(new[]219            {220                new Cookie221                {222                    Url = Server.EmptyPage,223                    Name = "multiple-1",224                    Value = "123456"225                },226                new Cookie227                {228                    Url = Server.EmptyPage,229                    Name = "multiple-2",230                    Value = "bar"231                },232            });233            CollectionAssert.AreEqual(234                new[]235                {236                    "multiple-1=123456",237                    "multiple-2=bar"238                },239                await Page.EvaluateAsync<string[]>(@"() => {240                    const cookies = document.cookie.split(';');241                    return cookies.map(cookie => cookie.trim()).sort();242                }")243            );244        }245        [PlaywrightTest("browsercontext-add-cookies.spec.ts", "should have |expires| set to |-1| for session cookies")]246        public async Task ShouldHaveExpiresSetToMinus1ForSessionCookies()247        {248            await Context.AddCookiesAsync(new[]249            {250                new Cookie251                {252                    Url = Server.EmptyPage,253                    Name = "expires",254                    Value = "123456"255                }256            });257            var cookies = await Context.CookiesAsync();258            Assert.AreEqual(-1, cookies.ElementAt(0).Expires);259        }260        [PlaywrightTest("browsercontext-add-cookies.spec.ts", "should set cookie with reasonable defaults")]261        public async Task ShouldSetCookieWithReasonableDefaults()262        {263            await Context.AddCookiesAsync(new[]264            {265                new Cookie266                {267                    Url = Server.EmptyPage,268                    Name = "defaults",269                    Value = "123456"270                }271            });272            var cookies = await Context.CookiesAsync();273            Assert.That(cookies, Has.Count.EqualTo(1));274            var cookie = cookies[0];275            Assert.AreEqual("defaults", cookie.Name);276            Assert.AreEqual("123456", cookie.Value);277            Assert.AreEqual("localhost", cookie.Domain);278            Assert.AreEqual("/", cookie.Path);279            Assert.AreEqual(-1, cookie.Expires);280            Assert.IsFalse(cookie.HttpOnly);281            Assert.IsFalse(cookie.Secure);282            Assert.AreEqual(TestConstants.IsChromium ? SameSiteAttribute.Lax : SameSiteAttribute.None, cookie.SameSite);283        }284        [PlaywrightTest("browsercontext-add-cookies.spec.ts", "should set a cookie with a path")]285        public async Task ShouldSetACookieWithAPath()286        {287            await Page.GotoAsync(Server.Prefix + "/grid.html");288            await Context.AddCookiesAsync(new[]289            {290                new Cookie291                {292                    Domain = "localhost",293                    Path = "/grid.html",294                    Name = "gridcookie",295                    Value = "GRID",296                    SameSite = SameSiteAttribute.Lax,297                }298            });299            var cookies = await Context.CookiesAsync();300            Assert.That(cookies, Has.Count.EqualTo(1));301            var cookie = cookies[0];302            Assert.AreEqual("gridcookie", cookie.Name);303            Assert.AreEqual("GRID", cookie.Value);304            Assert.AreEqual("localhost", cookie.Domain);305            Assert.AreEqual("/grid.html", cookie.Path);306            Assert.AreEqual(cookie.Expires, -1);307            Assert.IsFalse(cookie.HttpOnly);308            Assert.IsFalse(cookie.Secure);309            Assert.AreEqual(TestConstants.IsWebKit && TestConstants.IsWindows ? SameSiteAttribute.None : SameSiteAttribute.Lax, cookie.SameSite);310            Assert.AreEqual("gridcookie=GRID", await Page.EvaluateAsync<string>("document.cookie"));311            await Page.GotoAsync(Server.EmptyPage);312            Assert.That(await Page.EvaluateAsync<string>("document.cookie"), Is.Empty);313            await Page.GotoAsync(Server.Prefix + "/grid.html");314            Assert.AreEqual("gridcookie=GRID", await Page.EvaluateAsync<string>("document.cookie"));315        }316        [PlaywrightTest("browsercontext-add-cookies.spec.ts", "should not set a cookie with blank page URL")]317        public async Task ShouldNotSetACookieWithBlankPageURL()318        {319            await Page.GotoAsync(TestConstants.AboutBlank);320            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(()321                => Context.AddCookiesAsync(new[]322                {323                        new Cookie324                        {325                            Url = Server.EmptyPage,326                            Name = "example-cookie",327                            Value = "best"328                        },329                        new Cookie330                        {331                            Url = "about:blank",332                            Name = "example-cookie-blank",333                            Value = "best"334                        },335                }));336            Assert.AreEqual("Blank page can not have cookie \"example-cookie-blank\"", exception.Message);337        }338        [PlaywrightTest("browsercontext-add-cookies.spec.ts", "should not set a cookie on a data URL page")]339        public async Task ShouldNotSetACookieOnADataURLPage()340        {341            await Page.GotoAsync("data:,Hello%2C%20World!");342            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(()343                => Context.AddCookiesAsync(new[]344                {345                        new Cookie346                        {347                            Url = "data:,Hello%2C%20World!",348                            Name = "example-cookie",349                            Value = "best"350                        }351                }));352            Assert.AreEqual("Data URL page can not have cookie \"example-cookie\"", exception.Message);353        }354        [PlaywrightTest("browsercontext-add-cookies.spec.ts", "should default to setting secure cookie for HTTPS websites")]355        public async Task ShouldDefaultToSettingSecureCookieForHttpsWebsites()356        {357            await Page.GotoAsync(Server.EmptyPage);358            const string secureUrl = "https://example.com";359            await Context.AddCookiesAsync(new[]360            {361                new Cookie362                {363                    Url = secureUrl,364                    Name = "foo",365                    Value = "bar"366                }367            });368            var cookies = await Context.CookiesAsync(new[] { secureUrl });369            Assert.That(cookies, Has.Count.EqualTo(1));370            var cookie = cookies[0];371            Assert.IsTrue(cookie.Secure);372        }373        [PlaywrightTest("browsercontext-add-cookies.spec.ts", "should be able to set unsecure cookie for HTTP website")]374        public async Task ShouldBeAbleToSetUnsecureCookieForHttpWebSite()375        {376            await Page.GotoAsync(Server.EmptyPage);377            string SecureUrl = "http://example.com";378            await Context.AddCookiesAsync(new[]379            {380                new Cookie381                {382                    Url = SecureUrl,383                    Name = "foo",384                    Value = "bar"385                }386            });387            var cookies = await Context.CookiesAsync(new[] { SecureUrl });388            Assert.That(cookies, Has.Count.EqualTo(1));389            var cookie = cookies[0];390            Assert.IsFalse(cookie.Secure);391        }392        [PlaywrightTest("browsercontext-add-cookies.spec.ts", "should set a cookie on a different domain")]393        public async Task ShouldSetACookieOnADifferentDomain()394        {395            await Page.GotoAsync(Server.Prefix + "/grid.html");396            await Context.AddCookiesAsync(new[]397            {398                new Cookie399                {400                    Name = "example-cookie",401                    Value = "best",402                    Url = "https://www.example.com",403                    SameSite = SameSiteAttribute.Lax,404                }405            });406            Assert.AreEqual(string.Empty, await Page.EvaluateAsync<string>("document.cookie"));407            var cookies = await Context.CookiesAsync(new[] { "https://www.example.com" });408            Assert.That(cookies, Has.Count.EqualTo(1));409            var cookie = cookies[0];410            Assert.AreEqual("example-cookie", cookie.Name);411            Assert.AreEqual("best", cookie.Value);412            Assert.AreEqual("www.example.com", cookie.Domain);413            Assert.AreEqual("/", cookie.Path);414            Assert.AreEqual(cookie.Expires, -1);415            Assert.IsFalse(cookie.HttpOnly);416            Assert.IsTrue(cookie.Secure);417            Assert.AreEqual(TestConstants.IsWebKit && TestConstants.IsWindows ? SameSiteAttribute.None : SameSiteAttribute.Lax, cookie.SameSite);418        }419        [PlaywrightTest("browsercontext-add-cookies.spec.ts", "should set cookies for a frame")]420        public async Task ShouldSetCookiesForAFrame()421        {422            await Page.GotoAsync(Server.EmptyPage);423            await Context.AddCookiesAsync(new[]424            {425                new Cookie426                {427                    Url = Server.Prefix,428                    Name = "frame-cookie",429                    Value = "value"430                }431            });432            await Page.EvaluateAsync(@"src => {433                    let fulfill;434                    const promise = new Promise(x => fulfill = x);435                    const iframe = document.createElement('iframe');436                    document.body.appendChild(iframe);437                    iframe.onload = fulfill;438                    iframe.src = src;439                    return promise;440                }", Server.Prefix + "/grid.html");441            Assert.AreEqual("frame-cookie=value", await Page.FirstChildFrame().EvaluateAsync<string>("document.cookie"));442        }443        [PlaywrightTest("browsercontext-add-cookies.spec.ts", "should(not) block third party cookies")]444        public async Task ShouldNotBlockThirdPartyCookies()445        {446            await Page.GotoAsync(Server.EmptyPage);447            await Page.EvaluateAsync(@"src => {448                  let fulfill;449                  const promise = new Promise(x => fulfill = x);450                  const iframe = document.createElement('iframe');451                  document.body.appendChild(iframe);452                  iframe.onload = fulfill;453                  iframe.src = src;454                  return promise;455                }", Server.CrossProcessPrefix + "/grid.html");456            await Page.FirstChildFrame().EvaluateAsync<string>("document.cookie = 'username=John Doe'");457            await Page.WaitForTimeoutAsync(2000);458            bool allowsThirdParty = TestConstants.IsFirefox;459            var cookies = await Context.CookiesAsync(new[] { Server.CrossProcessPrefix + "/grid.html" });460            if (allowsThirdParty)461            {462                Assert.That(cookies, Has.Count.EqualTo(1));463                var cookie = cookies[0];464                Assert.AreEqual("127.0.0.1", cookie.Domain);465                Assert.AreEqual(cookie.Expires, -1);466                Assert.IsFalse(cookie.HttpOnly);467                Assert.AreEqual("username", cookie.Name);468                Assert.AreEqual("/", cookie.Path);469                Assert.AreEqual(SameSiteAttribute.None, cookie.SameSite);470                Assert.IsFalse(cookie.Secure);471                Assert.AreEqual("John Doe", cookie.Value);472            }473            else474            {475                Assert.That(cookies, Is.Empty);476            }477        }478        static void AssertEqual(IEnumerable<BrowserContextCookiesResult> ea, IEnumerable<BrowserContextCookiesResult> eb)479        {480            var aa = ea.ToList();481            var bb = eb.ToList();482            Assert.AreEqual(aa.Count, bb.Count);483            for (int i = 0; i < aa.Count; ++i)484            {485                var a = aa[i];486                var b = bb[i];487                Assert.AreEqual(a.Name, b.Name);488                Assert.AreEqual(a.Value, b.Value);489                Assert.AreEqual(a.Domain, b.Domain);490                Assert.AreEqual(a.Path, b.Path);491                Assert.AreEqual(a.Expires, b.Expires);492                Assert.AreEqual(a.HttpOnly, b.HttpOnly);...AssertEqual
Using AI Code Generation
1var browserContextAddCookiesTests = new Microsoft.Playwright.Tests.BrowserContextAddCookiesTests();2browserContextAddCookiesTests.AssertEqual();3var browserContextAddInitScriptTests = new Microsoft.Playwright.Tests.BrowserContextAddInitScriptTests();4browserContextAddInitScriptTests.AssertEqual();5var browserContextAddInitScriptTests = new Microsoft.Playwright.Tests.BrowserContextAddInitScriptTests();6browserContextAddInitScriptTests.AssertEqual();7var browserContextAddInitScriptTests = new Microsoft.Playwright.Tests.BrowserContextAddInitScriptTests();8browserContextAddInitScriptTests.AssertEqual();9var browserContextAddInitScriptTests = new Microsoft.Playwright.Tests.BrowserContextAddInitScriptTests();10browserContextAddInitScriptTests.AssertEqual();11var browserContextAddScriptTagTests = new Microsoft.Playwright.Tests.BrowserContextAddScriptTagTests();12browserContextAddScriptTagTests.AssertEqual();13var browserContextAddScriptTagTests = new Microsoft.Playwright.Tests.BrowserContextAddScriptTagTests();14browserContextAddScriptTagTests.AssertEqual();15var browserContextAddScriptTagTests = new Microsoft.Playwright.Tests.BrowserContextAddScriptTagTests();16browserContextAddScriptTagTests.AssertEqual();17var browserContextAddScriptTagTests = new Microsoft.Playwright.Tests.BrowserContextAddScriptTagTests();18browserContextAddScriptTagTests.AssertEqual();AssertEqual
Using AI Code Generation
1Microsoft.Playwright.Tests.BrowserContextAddCookiesTests.AssertEqual(new Cookie("name", "value"), new Cookie("name", "value"));2Microsoft.Playwright.Tests.BrowserContextAddCookiesTests.AssertEqual(new Cookie("name", "value"), new Cookie("name", "value"));3Microsoft.Playwright.Tests.BrowserContextAddCookiesTests.AssertEqual(new Cookie("name", "value"), new Cookie("name", "value"));4Microsoft.Playwright.Tests.BrowserContextAddCookiesTests.AssertEqual(new Cookie("name", "value"), new Cookie("name", "value"));5Microsoft.Playwright.Tests.BrowserContextAddCookiesTests.AssertEqual(new Cookie("name", "value"), new Cookie("name", "value"));6Microsoft.Playwright.Tests.BrowserContextAddCookiesTests.AssertEqual(new Cookie("name", "value"), new Cookie("name", "value"));7Microsoft.Playwright.Tests.BrowserContextAddCookiesTests.AssertEqual(new Cookie("name", "value"), new Cookie("name", "value"));8Microsoft.Playwright.Tests.BrowserContextAddCookiesTests.AssertEqual(new Cookie("name", "value"), new Cookie("name", "value"));9Microsoft.Playwright.Tests.BrowserContextAddCookiesTests.AssertEqual(new Cookie("name", "value"), new Cookie("name", "value"));AssertEqual
Using AI Code Generation
1var actual = 5;2var expected = 5;3AssertEqual(actual, expected);4var actual = 5;5var expected = 5;6AssertEqual(actual, expected);7var actual = 5;8var expected = 5;9AssertEqual(actual, expected);10var actual = 5;11var expected = 5;12AssertEqual(actual, expected);13var actual = 5;14var expected = 5;15AssertEqual(actual, expected);16var actual = 5;17var expected = 5;18AssertEqual(actual, expected);19var actual = 5;20var expected = 5;21AssertEqual(actual, expected);22var actual = 5;23var expected = 5;24AssertEqual(actual, expected);25var actual = 5;26var expected = 5;27AssertEqual(actual, expected);28var actual = 5;29var expected = 5;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!!
