How to use MoveOptions class of PuppeteerSharp.Input package

Best Puppeteer-sharp code snippet using PuppeteerSharp.Input.MoveOptions

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

Mouse.cs

Source: Mouse.cs Github

copy
1using System.Threading.Tasks;
2using PuppeteerSharp.Messaging;
3
4namespace PuppeteerSharp.Input
5{
6    /// <summary>
7    /// Provides methods to interact with the mouse
8    /// </summary>
9    public class Mouse
10    {
11        private readonly CDPSession _client;
12        private readonly Keyboard _keyboard;
13
14        private decimal _x = 0;
15        private decimal _y = 0;
16        private MouseButton _button = MouseButton.None;
17
18        /// <summary>
19        /// Initializes a new instance of the <see cref="Mouse"/> class.
20        /// </summary>
21        /// <param name="client">The client</param>
22        /// <param name="keyboard">The keyboard</param>
23        public Mouse(CDPSession client, Keyboard keyboard)
24        {
25            _client = client;
26            _keyboard = keyboard;
27        }
28
29        /// <summary>
30        /// Dispatches a <c>mousemove</c> event.
31        /// </summary>
32        /// <param name="x"></param>
33        /// <param name="y"></param>
34        /// <param name="options"></param>
35        /// <returns>Task</returns>
36        public async Task MoveAsync(decimal x, decimal y, MoveOptions options = null)
37        {
38            options = options ?? new MoveOptions();
39
40            decimal fromX = _x;
41            decimal fromY = _y;
42            _x = x;
43            _y = y;
44            int steps = options.Steps;
45
46            for (var i = 1; i <= steps; i++)
47            {
48                await _client.SendAsync("Input.dispatchMouseEvent", new InputDispatchMouseEventRequest
49                {
50                    Type = MouseEventType.MouseMoved,
51                    Button = _button,
52                    X = fromX + ((_x - fromX) * ((decimal)i / steps)),
53                    Y = fromY + ((_y - fromY) * ((decimal)i / steps)),
54                    Modifiers = _keyboard.Modifiers
55                }).ConfigureAwait(false);
56            }
57        }
58
59        /// <summary>
60        /// Shortcut for <see cref="MoveAsync(decimal, decimal, MoveOptions)"/>, <see cref="DownAsync(ClickOptions)"/> and <see cref="UpAsync(ClickOptions)"/>
61        /// </summary>
62        /// <param name="x"></param>
63        /// <param name="y"></param>
64        /// <param name="options"></param>
65        /// <returns>Task</returns>
66        public async Task ClickAsync(decimal x, decimal y, ClickOptions options = null)
67        {
68            options = options ?? new ClickOptions();
69
70            if (options.Delay > 0)
71            {
72                await Task.WhenAll(
73                    MoveAsync(x, y),
74                    DownAsync(options)
75                ).ConfigureAwait(false);
76
77                await Task.Delay(options.Delay).ConfigureAwait(false);
78                await UpAsync(options).ConfigureAwait(false);
79            }
80            else
81            {
82                await Task.WhenAll(
83                   MoveAsync(x, y),
84                   DownAsync(options),
85                   UpAsync(options)
86               ).ConfigureAwait(false);
87            }
88        }
89
90        /// <summary>
91        /// Dispatches a <c>mousedown</c> event.
92        /// </summary>
93        /// <param name="options"></param>
94        /// <returns>Task</returns>
95        public Task DownAsync(ClickOptions options = null)
96        {
97            options = options ?? new ClickOptions();
98
99            _button = options.Button;
100
101            return _client.SendAsync("Input.dispatchMouseEvent", new InputDispatchMouseEventRequest
102            {
103                Type = MouseEventType.MousePressed,
104                Button = _button,
105                X = _x,
106                Y = _y,
107                Modifiers = _keyboard.Modifiers,
108                ClickCount = options.ClickCount
109            });
110        }
111
112        /// <summary>
113        /// Dispatches a <c>mouseup</c> event.
114        /// </summary>
115        /// <param name="options"></param>
116        /// <returns>Task</returns>
117        public Task UpAsync(ClickOptions options = null)
118        {
119            options = options ?? new ClickOptions();
120
121            _button = MouseButton.None;
122
123            return _client.SendAsync("Input.dispatchMouseEvent", new InputDispatchMouseEventRequest
124            {
125                Type = MouseEventType.MouseReleased,
126                Button = options.Button,
127                X = _x,
128                Y = _y,
129                Modifiers = _keyboard.Modifiers,
130                ClickCount = options.ClickCount
131            });
132        }
133
134        /// <summary>
135        /// Dispatches a <c>wheel</c> event.
136        /// </summary>
137        /// <param name="deltaX">Delta X.</param>
138        /// <param name="deltaY">Delta Y.</param>
139        /// <returns>Task</returns>
140        public Task WheelAsync(decimal deltaX, decimal deltaY)
141            => _client.SendAsync(
142                "Input.dispatchMouseEvent",
143                new InputDispatchMouseEventRequest
144                {
145                    Type = MouseEventType.MouseWheel,
146                    DeltaX = deltaX,
147                    DeltaY = deltaY
148                });
149    }
150}
Full Screen

MoveOptions.cs

Source: MoveOptions.cs Github

copy
1namespace PuppeteerSharp.Input
2{
3    /// <summary>
4    /// options to use <see cref="Mouse.MoveAsync(decimal, decimal, MoveOptions)"/>
5    /// </summary>
6    public class MoveOptions
7    {
8        /// <summary>
9        /// Sends intermediate <c>mousemove</c> events. Defaults to 1
10        /// </summary>
11        public int Steps { get; set; } = 1;
12    }
13}
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

Most used methods in MoveOptions

    No methods found for this class ­čś×

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)