How to use ShouldNotOverrideViewportSizeWhenPassedNull method of Microsoft.Playwright.Tests.HeadfulTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.HeadfulTests.ShouldNotOverrideViewportSizeWhenPassedNull

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

HeadfulTests.cs

Source: HeadfulTests.cs Github

copy
1/*
2 * MIT License
3 *
4 * Copyright (c) 2020 Darío Kondratiuk
5 * Modifications copyright (c) Microsoft Corporation.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26using System.Linq;
27using System.Threading.Tasks;
28using Microsoft.Playwright.NUnit;
29using NUnit.Framework;
30
31namespace Microsoft.Playwright.Tests
32{
33    ///<playwright-file>headful.spec.ts</playwright-file>
34
35    public class HeadfulTests : PlaywrightTestEx
36    {
37        [PlaywrightTest("headful.spec.ts", "should have default url when launching browser")]
38        public async Task ShouldHaveDefaultUrlWhenLaunchingBrowser()
39        {
40            using var tempDir = new TempDirectory();
41            await using var browserContext = await LaunchPersistentHeaded(tempDir.Path);
42
43            string[] pages = browserContext.Pages.Select(page => page.Url).ToArray();
44            Assert.AreEqual(new[] { "about:blank" }, pages);
45        }
46
47        [PlaywrightTest("headful.spec.ts", "headless should be able to read cookies written by headful")]
48        [Ignore("Flaky")]
49        public async Task HeadlessShouldBeAbleToReadCookiesWrittenByHeadful()
50        {
51            using var userDataDir = new TempDirectory();
52
53            // Write a cookie in headful chrome            
54            await using var headfulContext = await LaunchPersistentHeaded(userDataDir.Path);
55            var headfulPage = await headfulContext.NewPageAsync();
56            await headfulPage.GotoAsync(Server.EmptyPage);
57            await headfulPage.EvaluateAsync("() => document.cookie = 'foo=true; expires=Fri, 31 Dec 9999 23:59:59 GMT'");
58            await headfulContext.CloseAsync();
59
60            var headlessContext = await LaunchPersistentHeaded(userDataDir.Path);
61            var headlessPage = await headlessContext.NewPageAsync();
62            await headlessPage.GotoAsync(Server.EmptyPage);
63            string cookie = await headlessPage.EvaluateAsync<string>("() => document.cookie");
64            await headlessContext.CloseAsync();
65
66            Assert.AreEqual("foo=true", cookie);
67        }
68
69        [PlaywrightTest("headful.spec.ts", "should close browser with beforeunload page")]
70        public async Task ShouldCloseBrowserWithBeforeunloadPage()
71        {
72            using var userDataDir = new TempDirectory();
73            await using var browserContext = await LaunchPersistentHeaded(userDataDir.Path);
74            var page = await browserContext.NewPageAsync();
75
76            await page.GotoAsync(Server.Prefix + "/beforeunload.html");
77            // We have to interact with a page so that 'beforeunload' handlers fire.
78            await page.ClickAsync("body");
79        }
80
81        [PlaywrightTest("headful.spec.ts", "should not crash when creating second context")]
82        public async Task ShouldNotCrashWhenCreatingSecondContext()
83        {
84            await using var browser = await LaunchHeaded();
85
86            await using (var browserContext = await browser.NewContextAsync())
87            {
88                await browserContext.NewPageAsync();
89            }
90
91            await using (var browserContext = await browser.NewContextAsync())
92            {
93                await browserContext.NewPageAsync();
94            }
95        }
96
97        [PlaywrightTest("headful.spec.ts", "should click background tab")]
98        public async Task ShouldClickBackgroundTab()
99        {
100            await using var browser = await LaunchHeaded();
101            var page = await browser.NewPageAsync();
102            await page.SetContentAsync($"<button>Hello</button><a target=_blank href=\"{Server.EmptyPage}\">empty.html</a>");
103            await page.ClickAsync("a");
104            await page.ClickAsync("button");
105        }
106
107        [PlaywrightTest("headful.spec.ts", "should close browser after context menu was triggered")]
108        public async Task ShouldCloseBrowserAfterContextMenuWasTriggered()
109        {
110            await using var browser = await LaunchHeaded();
111            var page = await browser.NewPageAsync();
112            await page.GotoAsync(Server.Prefix + "/grid.html");
113            await page.ClickAsync("body", new() { Button = MouseButton.Right });
114        }
115
116        [PlaywrightTest("headful.spec.ts", "should(not) block third party cookies")]
117        public async Task ShouldNotBlockThirdPartyCookies()
118        {
119            await using var browser = await LaunchHeaded();
120            var page = await browser.NewPageAsync();
121            await page.GotoAsync(Server.EmptyPage);
122
123            await page.EvaluateAsync(@"src => {
124                let fulfill;
125                const promise = new Promise(x => fulfill = x);
126                const iframe = document.createElement('iframe');
127                document.body.appendChild(iframe);
128                iframe.onload = fulfill;
129                iframe.src = src;
130                return promise;
131            }", Server.CrossProcessPrefix + "/grid.html");
132
133            string documentCookie = await page.Frames.ElementAt(1).EvaluateAsync<string>(@"() => {
134                document.cookie = 'username=John Doe';
135                return document.cookie;
136            }");
137
138            await page.WaitForTimeoutAsync(2000);
139            bool allowsThirdParty = TestConstants.IsFirefox;
140            Assert.AreEqual(allowsThirdParty ? "username=John Doe" : string.Empty, documentCookie);
141            var cookies = await page.Context.CookiesAsync(new[] { Server.CrossProcessPrefix + "/grid.html" });
142
143            if (allowsThirdParty)
144            {
145                Assert.That(cookies, Has.Count.EqualTo(1));
146                var cookie = cookies.First();
147                Assert.AreEqual("127.0.0.1", cookie.Domain);
148                Assert.AreEqual(cookie.Expires, -1);
149                Assert.False(cookie.HttpOnly);
150                Assert.AreEqual("username", cookie.Name);
151                Assert.AreEqual("/", cookie.Path);
152                Assert.AreEqual(SameSiteAttribute.None, cookie.SameSite);
153                Assert.False(cookie.Secure);
154                Assert.AreEqual("John Doe", cookie.Value);
155            }
156            else
157            {
158                Assert.IsEmpty(cookies);
159            }
160        }
161
162        [PlaywrightTest("headful.spec.ts", "should not override viewport size when passed null")]
163        [Skip(SkipAttribute.Targets.Webkit)]
164        public async Task ShouldNotOverrideViewportSizeWhenPassedNull()
165        {
166            await using var browser = await LaunchHeaded();
167            var context = await browser.NewContextAsync(new() { ViewportSize = ViewportSize.NoViewport });
168            var page = await context.NewPageAsync();
169            await page.GotoAsync(Server.EmptyPage);
170            var popupTask = page.WaitForPopupAsync();
171
172            await TaskUtils.WhenAll(
173                popupTask,
174                page.EvaluateAsync(@"() => {
175                    const win = window.open(window.location.href, 'Title', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=300,top=0,left=0');
176                    win.resizeTo(500, 450);
177                }"));
178
179            var popup = popupTask.Result;
180            await popup.WaitForLoadStateAsync();
181            await popup.WaitForFunctionAsync("() => window.outerWidth === 500 && window.outerHeight === 450");
182        }
183
184        [PlaywrightTest("headful.spec.ts", "Page.bringToFront should work")]
185        public async Task PageBringToFrontShouldWork()
186        {
187            await using var browser = await LaunchHeaded();
188            var context = await browser.NewContextAsync(new() { ViewportSize = ViewportSize.NoViewport });
189            var page1 = await context.NewPageAsync();
190            await page1.SetContentAsync("Page1");
191            var page2 = await context.NewPageAsync();
192            await page2.SetContentAsync("Page2");
193
194            await page1.BringToFrontAsync();
195            Assert.AreEqual("visible", await page1.EvaluateAsync<string>("document.visibilityState"));
196            Assert.AreEqual("visible", await page2.EvaluateAsync<string>("document.visibilityState"));
197
198            await page2.BringToFrontAsync();
199            Assert.AreEqual("visible", await page1.EvaluateAsync<string>("document.visibilityState"));
200            Assert.AreEqual("visible", await page2.EvaluateAsync<string>("document.visibilityState"));
201        }
202
203        private Task<IBrowserContext> LaunchPersistentHeaded(string path)
204        {
205            return BrowserType.LaunchPersistentContextAsync(path, new() { Headless = false });
206        }
207
208        private Task<IBrowser> LaunchHeaded()
209        {
210            return BrowserType.LaunchAsync(new() { Headless = false });
211        }
212    }
213}
214
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 ShouldNotOverrideViewportSizeWhenPassedNull code on LambdaTest Cloud Grid

Execute automation tests with ShouldNotOverrideViewportSizeWhenPassedNull 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)