How to use ShouldFocusPopupsByDefault method of Microsoft.Playwright.Tests.EmulationFocusTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.EmulationFocusTests.ShouldFocusPopupsByDefault

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

EmulationFocusTests.cs

Source: EmulationFocusTests.cs Github

copy
1/*
2 * MIT License
3 *
4 * Copyright (c) Microsoft Corporation.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25using System.Threading.Tasks;
26using Microsoft.Playwright.NUnit;
27using NUnit.Framework;
28
29namespace Microsoft.Playwright.Tests
30{
31    public class EmulationFocusTests : PageTestEx
32    {
33        [PlaywrightTest("emulation-focus.spec.ts", "should think that it is focused by default")]
34        public async Task ShouldThinkThatItIsFocusedByDefault()
35        {
36            Assert.True(await Page.EvaluateAsync<bool>("document.hasFocus()"));
37        }
38
39        [PlaywrightTest("emulation-focus.spec.ts", "should think that all pages are focused")]
40        public async Task ShouldThinkThatAllPagesAreFocused()
41        {
42            var page2 = await Page.Context.NewPageAsync();
43            Assert.True(await Page.EvaluateAsync<bool>("document.hasFocus()"));
44            Assert.True(await page2.EvaluateAsync<bool>("document.hasFocus()"));
45        }
46
47        [PlaywrightTest("emulation-focus.spec.ts", "should focus popups by default")]
48        public async Task ShouldFocusPopupsByDefault()
49        {
50            await Page.GotoAsync(Server.EmptyPage);
51            var popupTask = Page.WaitForPopupAsync();
52
53            await TaskUtils.WhenAll(
54                popupTask,
55                Page.EvaluateAsync("url => window.open(url)", Server.EmptyPage));
56
57            var popup = popupTask.Result;
58
59            Assert.True(await Page.EvaluateAsync<bool>("document.hasFocus()"));
60            Assert.True(await popup.EvaluateAsync<bool>("document.hasFocus()"));
61        }
62
63        [PlaywrightTest("emulation-focus.spec.ts", "should provide target for keyboard events")]
64        public async Task ShouldProvideTargetForKeyboardEvents()
65        {
66            var page2 = await Page.Context.NewPageAsync();
67
68            await TaskUtils.WhenAll(
69                Page.GotoAsync(Server.Prefix + "/input/textarea.html"),
70                page2.GotoAsync(Server.Prefix + "/input/textarea.html"));
71
72            await TaskUtils.WhenAll(
73                Page.FocusAsync("input"),
74                page2.FocusAsync("input"));
75
76            string text = "first";
77            string text2 = "second";
78
79            await TaskUtils.WhenAll(
80                Page.Keyboard.TypeAsync(text),
81                page2.Keyboard.TypeAsync(text2));
82
83            var results = await TaskUtils.WhenAll(
84                Page.EvaluateAsync<string>("result"),
85                page2.EvaluateAsync<string>("result"));
86
87            Assert.AreEqual(text, results.Item1);
88            Assert.AreEqual(text2, results.Item2);
89        }
90
91        [PlaywrightTest("emulation-focus.spec.ts", "should not affect mouse event target page")]
92        public async Task ShouldNotAffectMouseEventTargetPage()
93        {
94            var page2 = await Page.Context.NewPageAsync();
95            string clickCounter = @"function clickCounter() {
96              document.onclick = () => window.clickCount  = (window.clickCount || 0) + 1;
97            }";
98
99            await TaskUtils.WhenAll(
100                Page.EvaluateAsync(clickCounter),
101                page2.EvaluateAsync(clickCounter),
102                Page.FocusAsync("body"),
103                page2.FocusAsync("body"));
104
105            await TaskUtils.WhenAll(
106                Page.Mouse.ClickAsync(1, 1),
107                page2.Mouse.ClickAsync(1, 1));
108
109            var counters = await TaskUtils.WhenAll(
110                Page.EvaluateAsync<int>("window.clickCount"),
111                page2.EvaluateAsync<int>("window.clickCount"));
112
113            Assert.AreEqual(1, counters.Item1);
114            Assert.AreEqual(1, counters.Item2);
115        }
116
117        [PlaywrightTest("emulation-focus.spec.ts", "should change document.activeElement")]
118        public async Task ShouldChangeDocumentActiveElement()
119        {
120            var page2 = await Page.Context.NewPageAsync();
121
122            await TaskUtils.WhenAll(
123                Page.GotoAsync(Server.Prefix + "/input/textarea.html"),
124                page2.GotoAsync(Server.Prefix + "/input/textarea.html"));
125
126            await TaskUtils.WhenAll(
127                Page.FocusAsync("input"),
128                page2.FocusAsync("textArea"));
129
130            var results = await TaskUtils.WhenAll(
131                Page.EvaluateAsync<string>("document.activeElement.tagName"),
132                page2.EvaluateAsync<string>("document.activeElement.tagName"));
133
134            Assert.AreEqual("INPUT", results.Item1);
135            Assert.AreEqual("TEXTAREA", results.Item2);
136        }
137
138        [PlaywrightTest("emulation-focus.spec.ts", "should not affect screenshots")]
139        [Ignore("We need screenshot features first")]
140        public void ShouldNotAffectScreenshots()
141        {
142        }
143
144        [PlaywrightTest("emulation-focus.spec.ts", "should change focused iframe")]
145        public async Task ShouldChangeFocusedIframe()
146        {
147            await Page.GotoAsync(Server.EmptyPage);
148
149            var (frame1, frame2) = await TaskUtils.WhenAll(
150                FrameUtils.AttachFrameAsync(Page, "frame1", Server.Prefix + "/input/textarea.html"),
151                FrameUtils.AttachFrameAsync(Page, "frame2", Server.Prefix + "/input/textarea.html"));
152
153            string logger = @"function logger() {
154              self._events = [];
155              const element = document.querySelector('input');
156              element.onfocus = element.onblur = (e) => self._events.push(e.type);
157            }";
158
159            await TaskUtils.WhenAll(
160                frame1.EvaluateAsync(logger),
161                frame2.EvaluateAsync(logger));
162
163            var focused = await TaskUtils.WhenAll(
164                frame1.EvaluateAsync<bool>("document.hasFocus()"),
165                frame2.EvaluateAsync<bool>("document.hasFocus()"));
166
167            Assert.False(focused.Item1);
168            Assert.False(focused.Item2);
169
170            await frame1.FocusAsync("input");
171            var events = await TaskUtils.WhenAll(
172                frame1.EvaluateAsync<string[]>("self._events"),
173                frame2.EvaluateAsync<string[]>("self._events"));
174
175            Assert.AreEqual(new[] { "focus" }, events.Item1);
176            Assert.IsEmpty(events.Item2);
177
178            focused = await TaskUtils.WhenAll(
179                frame1.EvaluateAsync<bool>("document.hasFocus()"),
180                frame2.EvaluateAsync<bool>("document.hasFocus()"));
181
182            Assert.True(focused.Item1);
183            Assert.False(focused.Item2);
184
185            await frame2.FocusAsync("input");
186            events = await TaskUtils.WhenAll(
187                frame1.EvaluateAsync<string[]>("self._events"),
188                frame2.EvaluateAsync<string[]>("self._events"));
189
190            Assert.AreEqual(new[] { "focus", "blur" }, events.Item1);
191            Assert.AreEqual(new[] { "focus" }, events.Item2);
192
193            focused = await TaskUtils.WhenAll(
194                frame1.EvaluateAsync<bool>("document.hasFocus()"),
195                frame2.EvaluateAsync<bool>("document.hasFocus()"));
196
197            Assert.False(focused.Item1);
198            Assert.True(focused.Item2);
199        }
200    }
201}
202
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 ShouldFocusPopupsByDefault code on LambdaTest Cloud Grid

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