How to use WheelEventInternal method of PuppeteerSharp.Tests.MouseTests.MouseTests class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.MouseTests.MouseTests.WheelEventInternal

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

MouseTests.cs

Source: MouseTests.cs Github

copy
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using PuppeteerSharp.Input;
5using PuppeteerSharp.Tests.Attributes;
6using PuppeteerSharp.Xunit;
7using Xunit;
8using Xunit.Abstractions;
9
10namespace PuppeteerSharp.Tests.MouseTests
11{
12    [Collection(TestConstants.TestFixtureCollectionName)]
13    public class MouseTests : PuppeteerPageBaseTest
14    {
15        private const string Dimensions = @"function dimensions() {
16            const rect = document.querySelector('textarea').getBoundingClientRect();
17            return {
18                x: rect.left,
19                y: rect.top,
20                width: rect.width,
21                height: rect.height
22            };
23        }";
24
25        public MouseTests(ITestOutputHelper output) : base(output)
26        {
27        }
28
29        [PuppeteerTest("mouse.spec.ts", "Mouse", "should click the document")]
30        [PuppeteerFact]
31        public async Task ShouldClickTheDocument()
32        {
33            await Page.EvaluateFunctionAsync(@"() => {
34                globalThis.clickPromise = new Promise((resolve) => {
35                    document.addEventListener('click', (event) => {
36                    resolve({
37                        type: event.type,
38                        detail: event.detail,
39                        clientX: event.clientX,
40                        clientY: event.clientY,
41                        isTrusted: event.isTrusted,
42                        button: event.button,
43                    });
44                    });
45                });
46            }");
47            await Page.Mouse.ClickAsync(50, 60);
48            var e = await Page.EvaluateFunctionAsync<MouseEvent>("() => globalThis.clickPromise");
49
50            Assert.Equal("click", e.Type);
51            Assert.Equal(1, e.Detail);
52            Assert.Equal(50, e.ClientX);
53            Assert.Equal(60, e.ClientY);
54            Assert.True(e.IsTrusted);
55            Assert.Equal(0, e.Button);
56        }
57
58        [PuppeteerTest("mouse.spec.ts", "Mouse", "should resize the textarea")]
59        [SkipBrowserFact(skipFirefox: true)]
60        public async Task ShouldResizeTheTextarea()
61        {
62            await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");
63            var dimensions = await Page.EvaluateFunctionAsync<Dimensions>(Dimensions);
64            var mouse = Page.Mouse;
65            await mouse.MoveAsync(dimensions.X + dimensions.Width - 4, dimensions.Y + dimensions.Height - 4);
66            await mouse.DownAsync();
67            await mouse.MoveAsync(dimensions.X + dimensions.Width + 100, dimensions.Y + dimensions.Height + 100);
68            await mouse.UpAsync();
69            var newDimensions = await Page.EvaluateFunctionAsync<Dimensions>(Dimensions);
70            Assert.Equal(Math.Round(dimensions.Width + 104, MidpointRounding.AwayFromZero), newDimensions.Width);
71            Assert.Equal(Math.Round(dimensions.Height + 104, MidpointRounding.AwayFromZero), newDimensions.Height);
72        }
73
74        [PuppeteerTest("mouse.spec.ts", "Mouse", "should select the text with mouse")]
75        [SkipBrowserFact(skipFirefox: true)]
76        public async Task ShouldSelectTheTextWithMouse()
77        {
78            await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");
79            await Page.FocusAsync("textarea");
80            const string text = "This is the text that we are going to try to select. Let's see how it goes.";
81            await Page.Keyboard.TypeAsync(text);
82            // Firefox needs an extra frame here after typing or it will fail to set the scrollTop
83            await Page.EvaluateExpressionAsync("new Promise(requestAnimationFrame)");
84            await Page.EvaluateExpressionAsync("document.querySelector('textarea').scrollTop = 0");
85            var dimensions = await Page.EvaluateFunctionAsync<Dimensions>(Dimensions);
86            await Page.Mouse.MoveAsync(dimensions.X + 2, dimensions.Y + 2);
87            await Page.Mouse.DownAsync();
88            await Page.Mouse.MoveAsync(100, 100);
89            await Page.Mouse.UpAsync();
90            Assert.Equal(text, await Page.EvaluateFunctionAsync<string>(@"() => {
91                const textarea = document.querySelector('textarea');
92                return textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
93            }"));
94        }
95
96        [PuppeteerTest("mouse.spec.ts", "Mouse", "should trigger hover state")]
97        [SkipBrowserFact(skipFirefox: true)]
98        public async Task ShouldTriggerHoverState()
99        {
100            await Page.GoToAsync(TestConstants.ServerUrl + "/input/scrollable.html");
101            await Page.HoverAsync("#button-6");
102            Assert.Equal("button-6", await Page.EvaluateExpressionAsync<string>("document.querySelector('button:hover').id"));
103            await Page.HoverAsync("#button-2");
104            Assert.Equal("button-2", await Page.EvaluateExpressionAsync<string>("document.querySelector('button:hover').id"));
105            await Page.HoverAsync("#button-91");
106            Assert.Equal("button-91", await Page.EvaluateExpressionAsync<string>("document.querySelector('button:hover').id"));
107        }
108
109        [PuppeteerTest("mouse.spec.ts", "Mouse", "should trigger hover state with removed window.Node")]
110        [SkipBrowserFact(skipFirefox: true)]
111        public async Task ShouldTriggerHoverStateWithRemovedWindowNode()
112        {
113            await Page.GoToAsync(TestConstants.ServerUrl + "/input/scrollable.html");
114            await Page.EvaluateExpressionAsync("delete window.Node");
115            await Page.HoverAsync("#button-6");
116            Assert.Equal("button-6", await Page.EvaluateExpressionAsync("document.querySelector('button:hover').id"));
117        }
118
119        [PuppeteerTest("mouse.spec.ts", "Mouse", "should set modifier keys on click")]
120        [SkipBrowserFact(skipFirefox: true)]
121        public async Task ShouldSetModifierKeysOnClick()
122        {
123            await Page.GoToAsync(TestConstants.ServerUrl + "/input/scrollable.html");
124            await Page.EvaluateExpressionAsync("document.querySelector('#button-3').addEventListener('mousedown', e => window.lastEvent = e, true)");
125            var modifiers = new Dictionary<string, string> { ["Shift"] = "shiftKey", ["Control"] = "ctrlKey", ["Alt"] = "altKey", ["Meta"] = "metaKey" };
126            foreach (var modifier in modifiers)
127            {
128                await Page.Keyboard.DownAsync(modifier.Key);
129                await Page.ClickAsync("#button-3");
130                if (!(await Page.EvaluateFunctionAsync<bool>("mod => window.lastEvent[mod]", modifier.Value)))
131                {
132                    Assert.True(false, $"{modifier.Value} should be true");
133                }
134
135                await Page.Keyboard.UpAsync(modifier.Key);
136            }
137            await Page.ClickAsync("#button-3");
138            foreach (var modifier in modifiers)
139            {
140                if (await Page.EvaluateFunctionAsync<bool>("mod => window.lastEvent[mod]", modifier.Value))
141                {
142                    Assert.False(true, $"{modifiers.Values} should be false");
143                }
144            }
145        }
146
147        [PuppeteerTest("mouse.spec.ts", "Mouse", "should send mouse wheel events")]
148        [SkipBrowserFact(skipFirefox: true)]
149        public async Task ShouldSendMouseWheelEvents()
150        {
151            await Page.GoToAsync(TestConstants.ServerUrl + "/input/wheel.html");
152            var elem = await Page.QuerySelectorAsync("div");
153            var boundingBoxBefore = await elem.BoundingBoxAsync();
154            Assert.Equal(115, boundingBoxBefore.Width);
155            Assert.Equal(115, boundingBoxBefore.Height);
156
157            await Page.Mouse.MoveAsync(
158                boundingBoxBefore.X + (boundingBoxBefore.Width / 2),
159                boundingBoxBefore.Y + (boundingBoxBefore.Height / 2)
160            );
161
162            await Page.Mouse.WheelAsync(0, -100);
163            var boundingBoxAfter = await elem.BoundingBoxAsync();
164            Assert.Equal(230, boundingBoxAfter.Width);
165            Assert.Equal(230, boundingBoxAfter.Height);
166        }
167
168        [PuppeteerTest("mouse.spec.ts", "Mouse", "should tween mouse movement")]
169        [SkipBrowserFact(skipFirefox: true)]
170        public async Task ShouldTweenMouseMovement()
171        {
172            await Page.Mouse.MoveAsync(100, 100);
173            await Page.EvaluateExpressionAsync(@"{
174                window.result = [];
175                document.addEventListener('mousemove', event => {
176                    window.result.push([event.clientX, event.clientY]);
177                });
178            }");
179            await Page.Mouse.MoveAsync(200, 300, new MoveOptions { Steps = 5 });
180            Assert.Equal(new[] {
181                new[]{ 120, 140 },
182                new[]{ 140, 180 },
183                new[]{ 160, 220 },
184                new[]{ 180, 260 },
185                new[]{ 200, 300 }
186            }, await Page.EvaluateExpressionAsync<int[][]>("result"));
187        }
188
189        [PuppeteerTest("mouse.spec.ts", "Mouse", "should work with mobile viewports and cross process navigations")]
190        [SkipBrowserFact(skipFirefox: true)]
191        public async Task ShouldWorkWithMobileViewportsAndCrossProcessNavigations()
192        {
193            await Page.GoToAsync(TestConstants.EmptyPage);
194            await Page.SetViewportAsync(new ViewPortOptions
195            {
196                Width = 360,
197                Height = 640,
198                IsMobile = true
199            });
200            await Page.GoToAsync(TestConstants.CrossProcessUrl + "/mobile.html");
201            await Page.EvaluateFunctionAsync(@"() => {
202                document.addEventListener('click', event => {
203                    window.result = { x: event.clientX, y: event.clientY };
204                });
205            }");
206
207            await Page.Mouse.ClickAsync(30, 40);
208
209            Assert.Equal(new DomPointInternal()
210            {
211                X = 30,
212                Y = 40
213            }, await Page.EvaluateExpressionAsync<DomPointInternal>("result"));
214        }
215
216        internal struct WheelEventInternal
217        {
218            public WheelEventInternal(decimal deltaX, decimal deltaY)
219            {
220                DeltaX = deltaX;
221                DeltaY = deltaY;
222            }
223
224            public decimal DeltaX { get; set; }
225
226            public decimal DeltaY { get; set; }
227
228            public override string ToString() => $"({DeltaX}, {DeltaY})";
229        }
230
231        internal struct DomPointInternal
232        {
233            public decimal X { get; set; }
234
235            public decimal Y { get; set; }
236
237            public override string ToString() => $"({X}, {Y})";
238
239            public void Scroll(decimal deltaX, decimal deltaY)
240            {
241                X = Math.Max(0, X + deltaX);
242                Y = Math.Max(0, Y + deltaY);
243            }
244        }
245
246        internal struct MouseEvent
247        {
248            public string Type { get; set; }
249
250            public int Detail { get; set; }
251
252            public int ClientX { get; set; }
253
254            public int ClientY { get; set; }
255
256            public bool IsTrusted { get; set; }
257
258            public int Button { get; set; }
259        }
260    }
261}
262
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 WheelEventInternal code on LambdaTest Cloud Grid

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