How to use PageDispatchEventTests class of Microsoft.Playwright.Tests package

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.PageDispatchEventTests

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

PageDispatchEventTests.cs

Source: PageDispatchEventTests.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    ///<playwright-file>dispatchevent.spec.ts</playwright-file>
32    public class PageDispatchEventTests : PageTestEx
33    {
34        [PlaywrightTest("page-dispatchevent.spec.ts", "should dispatch click event")]
35        public async Task ShouldDispatchClickEvent()
36        {
37            await Page.GotoAsync(Server.Prefix + "/input/button.html");
38            await Page.DispatchEventAsync("button", "click");
39            Assert.AreEqual("Clicked", await Page.EvaluateAsync<string>("() => result"));
40        }
41
42        [PlaywrightTest("page-dispatchevent.spec.ts", "should dispatch click event properties")]
43        public async Task ShouldDispatchClickEventProperties()
44        {
45            await Page.GotoAsync(Server.Prefix + "/input/button.html");
46            await Page.DispatchEventAsync("button", "click");
47            Assert.True(await Page.EvaluateAsync<bool>("() => bubbles"));
48            Assert.True(await Page.EvaluateAsync<bool>("() => cancelable"));
49            Assert.True(await Page.EvaluateAsync<bool>("() => composed"));
50        }
51
52        [PlaywrightTest("page-dispatchevent.spec.ts", "should dispatch click svg")]
53        public async Task ShouldDispatchClickSvg()
54        {
55            await Page.SetContentAsync(@"
56            <svg height=""100"" width=""100"">
57                <circle onclick=""javascript:window.__CLICKED=42"" cx=""50"" cy=""50"" r=""40"" stroke=""black"" stroke-width = ""3"" fill=""red"" />
58            </svg>");
59
60            await Page.DispatchEventAsync("circle", "click");
61            Assert.AreEqual(42, await Page.EvaluateAsync<int>("() => window.__CLICKED"));
62        }
63
64        [PlaywrightTest("page-dispatchevent.spec.ts", "should dispatch click on a span with an inline element inside")]
65        public async Task ShouldDispatchClickOnASpanWithAnInlineElementInside()
66        {
67            await Page.SetContentAsync(@"
68              <style>
69                  span::before {
70                    content: 'q';
71                  }
72              </style>
73              <span onclick='javascript:window.CLICKED=42'></span>");
74
75            await Page.DispatchEventAsync("span", "click");
76            Assert.AreEqual(42, await Page.EvaluateAsync<int>("() => window.CLICKED"));
77        }
78
79        [PlaywrightTest("page-dispatchevent.spec.ts", "should dispatch click after navigation")]
80        public async Task ShouldDispatchClickAfterNavigation()
81        {
82            await Page.GotoAsync(Server.Prefix + "/input/button.html");
83            await Page.DispatchEventAsync("button", "click");
84            await Page.GotoAsync(Server.Prefix + "/input/button.html");
85            await Page.DispatchEventAsync("button", "click");
86            Assert.AreEqual("Clicked", await Page.EvaluateAsync<string>("() => result"));
87        }
88
89        [PlaywrightTest("page-dispatchevent.spec.ts", "should dispatch click after a cross origin navigation")]
90        public async Task ShouldDispatchClickAfterACrossOriginNavigation()
91        {
92            await Page.GotoAsync(Server.Prefix + "/input/button.html");
93            await Page.DispatchEventAsync("button", "click");
94            await Page.GotoAsync(Server.CrossProcessPrefix + "/input/button.html");
95            await Page.DispatchEventAsync("button", "click");
96            Assert.AreEqual("Clicked", await Page.EvaluateAsync<string>("() => result"));
97        }
98
99        [PlaywrightTest("page-dispatchevent.spec.ts", "should not fail when element is blocked on hover")]
100        public async Task ShouldNotFailWhenElementIsBlockedOnHover()
101        {
102            await Page.SetContentAsync(@"
103              <style>
104                container { display: block; position: relative; width: 200px; height: 50px; }
105                div, button { position: absolute; left: 0; top: 0; bottom: 0; right: 0; }
106                div { pointer-events: none; }
107                container:hover div { pointer-events: auto; background: red; }
108            </style>
109            <container>
110                <button onclick=""window.clicked = true"">Click me</button>
111                <div></div>
112            </container>");
113
114            await Page.DispatchEventAsync("button", "click");
115            Assert.True(await Page.EvaluateAsync<bool>("() => window.clicked"));
116        }
117
118        [PlaywrightTest("page-dispatchevent.spec.ts", "should dispatch click when node is added in shadow dom")]
119        public async Task ShouldDispatchClickWhenNodeIsAddedInShadowDom()
120        {
121            await Page.GotoAsync(Server.EmptyPage);
122            var watchdog = Page.DispatchEventAsync("span", "click");
123
124            await Page.EvaluateAsync(@"() => {
125              const div = document.createElement('div');
126              div.attachShadow({mode: 'open'});
127              document.body.appendChild(div);
128            }");
129            await Page.EvaluateAsync("() => new Promise(f => setTimeout(f, 100))");
130
131            await Page.EvaluateAsync(@"() => {
132              const span = document.createElement('span');
133              span.textContent = 'Hello from shadow';
134              span.addEventListener('click', () => window.clicked = true);
135              document.querySelector('div').shadowRoot.appendChild(span);
136            }");
137
138            await watchdog;
139            Assert.True(await Page.EvaluateAsync<bool>("() => window.clicked"));
140        }
141
142        [PlaywrightTest("page-dispatchevent.spec.ts", "should be atomic")]
143        public async Task ShouldBeAtomic()
144        {
145            const string createDummySelector = @"({
146                create(root, target) {},
147                query(root, selector) {
148                    const result = root.querySelector(selector);
149                    if (result)
150                    Promise.resolve().then(() => result.onclick = '');
151                    return result;
152                },
153                queryAll(root, selector) {
154                    const result = Array.from(root.querySelectorAll(selector));
155                    for (const e of result)
156                    Promise.resolve().then(() => e.onclick = null);
157                    return result;
158                }
159            })";
160
161            await TestUtils.RegisterEngineAsync(Playwright, "page-dispatchevent", createDummySelector);
162            await Page.SetContentAsync("<div onclick=\"window._clicked = true\">Hello</div>");
163            await Page.DispatchEventAsync("page-dispatchevent=div", "click");
164            Assert.True(await Page.EvaluateAsync<bool>("() => window['_clicked']"));
165        }
166
167        [PlaywrightTest("page-dispatchevent.spec.ts", "Page.dispatchEvent(drag)", "should dispatch drag drop events")]
168        [Skip(SkipAttribute.Targets.Webkit)]
169        public async Task ShouldDispatchDragDropEvents()
170        {
171            await Page.GotoAsync(Server.Prefix + "/drag-n-drop.html");
172            var dataTransfer = await Page.EvaluateHandleAsync("() => new DataTransfer()");
173            await Page.DispatchEventAsync("#source", "dragstart", new { dataTransfer });
174            await Page.DispatchEventAsync("#target", "drop", new { dataTransfer });
175
176            var source = await Page.QuerySelectorAsync("#source");
177            var target = await Page.QuerySelectorAsync("#target");
178            Assert.True(await Page.EvaluateAsync<bool>(@"() => {
179                return source.parentElement === target;
180            }", new { source, target }));
181        }
182
183        [PlaywrightTest("page-dispatchevent.spec.ts", "Page.dispatchEvent(drag)", "should dispatch drag drop events")]
184        [Skip(SkipAttribute.Targets.Webkit)]
185        public async Task ElementHandleShouldDispatchDragDropEvents()
186        {
187            await Page.GotoAsync(Server.Prefix + "/drag-n-drop.html");
188            var dataTransfer = await Page.EvaluateHandleAsync("() => new DataTransfer()");
189            var source = await Page.QuerySelectorAsync("#source");
190            await source.DispatchEventAsync("dragstart", new { dataTransfer });
191            var target = await Page.QuerySelectorAsync("#target");
192            await target.DispatchEventAsync("drop", new { dataTransfer });
193
194            Assert.True(await Page.EvaluateAsync<bool>(@"() => {
195                return source.parentElement === target;
196            }", new { source, target }));
197        }
198
199        [PlaywrightTest("page-dispatchevent.spec.ts", "should dispatch click event")]
200        public async Task ElementHandleShouldDispatchClickEvent()
201        {
202            await Page.GotoAsync(Server.Prefix + "/input/button.html");
203            var button = await Page.QuerySelectorAsync("button");
204            await button.DispatchEventAsync("click");
205            Assert.AreEqual("Clicked", await Page.EvaluateAsync<string>("() => result"));
206        }
207    }
208}
209
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)