How to use KeyboardPressOptions method of Microsoft.Playwright.KeyboardPressOptions class

Best Playwright-dotnet code snippet using Microsoft.Playwright.KeyboardPressOptions.KeyboardPressOptions

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

KeyboardPressOptions.cs

Source: KeyboardPressOptions.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;
26using System.Collections.Generic;
27using System.ComponentModel.DataAnnotations;
28using System.Drawing;
29using System.Globalization;
30using System.IO;
31using System.Runtime.Serialization;
32using System.Text.Json;
33using System.Text.Json.Serialization;
34using System.Text.RegularExpressions;
35using System.Threading;
36using System.Threading.Tasks;
37
38#nullable enable
39
40namespace Microsoft.Playwright
41{
42    public class KeyboardPressOptions
43    {
44        public KeyboardPressOptions() { }
45
46        public KeyboardPressOptions(KeyboardPressOptions clone)
47        {
48            if (clone == null)
49            {
50                return;
51            }
52
53            Delay = clone.Delay;
54        }
55
56        /// <summary>
57        /// <para>
58        /// Time to wait between <c>keydown</c> and <c>keyup</c> in milliseconds. Defaults to
59        /// 0.
60        /// </para>
61        /// </summary>
62        [JsonPropertyName("delay")]
63        public float? Delay { get; set; }
64    }
65}
66
67#nullable disable
68
Full Screen

PlaywrightHook.cs

Source: PlaywrightHook.cs Github

copy
1using BrowserHook;
2using Microsoft.Playwright;
3using System;
4using System.Text.RegularExpressions;
5using System.Threading.Tasks;
6
7namespace PlaywrightHook
8{
9    public class PlaywrightHook : IBrowserHook, IAsyncDisposable
10    {
11        IPlaywright _playwright;
12        IBrowserContext _browser;
13        IPage _page;
14        public async Task Initialize()
15        {
16            _playwright = await Playwright.CreateAsync();
17            _browser = await _playwright.Chromium.LaunchPersistentContextAsync("browserContext", new BrowserTypeLaunchPersistentContextOptions {  Headless = false});
18            _page = await _browser.NewPageAsync();
19        }
20
21        public async Task NavigateTo(string url) => 
22            await _page.GotoAsync(url);
23
24        public async Task WaitForElement(string xpath, bool continueOnTimeout = false, int timeoutMilliseconds = 30000)
25        {
26            try
27            {
28                await _page.WaitForSelectorAsync(xpath, new PageWaitForSelectorOptions { Timeout = timeoutMilliseconds });
29            }
30            catch (System.TimeoutException) {
31                if (!continueOnTimeout) throw;
32            }
33        }
34           
35        public async Task WaitForPage(string url) => 
36            await _page.WaitForURLAsync(url, new PageWaitForURLOptions { Timeout = 0, WaitUntil = WaitUntilState.NetworkIdle  });
37
38        public async Task WaitForPage(Regex regex) =>
39            await _page.WaitForURLAsync(regex, new PageWaitForURLOptions { Timeout = 0, WaitUntil = WaitUntilState.NetworkIdle });
40
41        public async Task<bool> IsElementPresent(string xpath) => 
42            (await _page.QuerySelectorAsync(xpath, new PageQuerySelectorOptions { })) != null;
43
44        public async Task<string> GetInnerText(string xpath) =>
45            await _page.InnerTextAsync(xpath, new PageInnerTextOptions {  });
46
47        public async ValueTask DisposeAsync()
48        {
49            await _page.CloseAsync();
50            await _browser.DisposeAsync();
51            _playwright.Dispose();
52        }
53
54        public async Task Click(string xpath) => 
55            await _page.ClickAsync(xpath, new PageClickOptions {  });
56
57        public async Task SetText(string xpath, string text)
58        {
59            await _page.TypeAsync(xpath, text, new PageTypeOptions { });
60        }
61
62        public async Task SendKey(string key, int count = 1)
63        {
64            for (var x = 0; x < count; x++)
65                await _page.Keyboard.PressAsync(key, new KeyboardPressOptions { });
66        }
67    }
68}
69
Full Screen

Keyboard.cs

Source: Keyboard.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.Transport.Channels;
27
28namespace Microsoft.Playwright.Core
29{
30    internal class Keyboard : IKeyboard
31    {
32        private readonly PageChannel _channel;
33
34        public Keyboard(PageChannel channel)
35        {
36            _channel = channel;
37        }
38
39        public Task DownAsync(string key) => _channel.KeyboardDownAsync(key);
40
41        public Task UpAsync(string key) => _channel.KeyboardUpAsync(key);
42
43        public Task PressAsync(string key, KeyboardPressOptions options = default)
44           => _channel.PressAsync(key, options?.Delay);
45
46        public Task TypeAsync(string text, KeyboardTypeOptions options = default)
47           => _channel.TypeAsync(text, options?.Delay);
48
49        public Task InsertTextAsync(string text) => _channel.InsertTextAsync(text);
50    }
51}
52
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 method in KeyboardPressOptions

Trigger KeyboardPressOptions code on LambdaTest Cloud Grid

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