Best Puppeteer-sharp code snippet using PuppeteerSharp.Input.DownOptions
InputTests.cs
Source:InputTests.cs
...386 {387 await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");388 await Page.FocusAsync("textarea");389 await Page.EvaluateExpressionAsync("document.querySelector('textarea').addEventListener('keydown', e => window.lastEvent = e, true)");390 await Page.Keyboard.DownAsync("a", new DownOptions { Text = "a" });391 Assert.False(await Page.EvaluateExpressionAsync<bool>("window.lastEvent.repeat"));392 await Page.Keyboard.PressAsync("a");393 Assert.True(await Page.EvaluateExpressionAsync<bool>("window.lastEvent.repeat"));394 }395 // @see https://github.com/GoogleChrome/puppeteer/issues/206396 [Fact]397 public async Task ShouldClickLinksWhichCauseNavigation()398 {399 await Page.SetContentAsync($"<a href=\"{TestConstants.EmptyPage}\">empty.html</a>");400 // This await should not hang.401 await Page.ClickAsync("a");402 }403 [Fact]404 public async Task ShouldTweenMouseMovement()...
ElementHandle.cs
Source:ElementHandle.cs
...219 await FocusAsync().ConfigureAwait(false);220 await Page.Keyboard.TypeAsync(text, options).ConfigureAwait(false);221 }222 /// <summary>223 /// Focuses the element, and then uses <see cref="Keyboard.DownAsync(string, DownOptions)"/> and <see cref="Keyboard.UpAsync(string)"/>.224 /// </summary>225 /// <param name="key">Name of key to press, such as <c>ArrowLeft</c>. See <see cref="KeyDefinitions"/> for a list of all key names.</param>226 /// <param name="options">press options</param>227 /// <remarks>228 /// If <c>key</c> is a single character and no modifier keys besides <c>Shift</c> are being held down, a <c>keypress</c>/<c>input</c> event will also be generated. The <see cref="DownOptions.Text"/> option can be specified to force an input event to be generated.229 /// </remarks>230 /// <returns></returns>231 public async Task PressAsync(string key, PressOptions options = null)232 {233 await FocusAsync().ConfigureAwait(false);234 await Page.Keyboard.PressAsync(key, options).ConfigureAwait(false);235 }236 /// <summary>237 /// The method runs <c>element.querySelector</c> within the page. If no element matches the selector, the return value resolve to <c>null</c>.238 /// </summary>239 /// <param name="selector">A selector to query element for</param>240 /// <returns>Task which resolves to <see cref="ElementHandle"/> pointing to the frame element</returns>241 public async Task<ElementHandle> QuerySelectorAsync(string selector)242 {...
Keyboard.cs
Source:Keyboard.cs
...6{7 /// <summary>8 /// Keyboard provides an api for managing a virtual keyboard. The high level api is <see cref="TypeAsync(string, TypeOptions)"/>, which takes raw characters and generates proper keydown, keypress/input, and keyup events on your page.9 /// 10 /// For finer control, you can use <see cref="Keyboard.DownAsync(string, DownOptions)"/>, <see cref="UpAsync(string)"/>, and <see cref="SendCharacterAsync(string)"/> to manually fire events as if they were generated from a real keyboard.11 /// </summary>12 public class Keyboard13 {14 private readonly CDPSession _client;15 private readonly HashSet<string> _pressedKeys = new HashSet<string>();16 internal int Modifiers { get; set; }17 internal Keyboard(CDPSession client)18 {19 _client = client;20 }21 /// <summary>22 /// Dispatches a <c>keydown</c> event23 /// </summary>24 /// <param name="key">Name of key to press, such as <c>ArrowLeft</c>. <see cref="KeyDefinitions"/> for a list of all key names.</param>25 /// <param name="options">down options</param>26 /// <remarks>27 /// If <c>key</c> is a single character and no modifier keys besides <c>Shift</c> are being held down, a <c>keypress</c>/<c>input</c> event will also generated. The <c>text</c> option can be specified to force an input event to be generated.28 /// If <c>key</c> is a modifier key, <c>Shift</c>, <c>Meta</c>, <c>Control</c>, or <c>Alt</c>, subsequent key presses will be sent with that modifier active. To release the modifier key, use <see cref="UpAsync(string)"/>29 /// After the key is pressed once, subsequent calls to <see cref="DownAsync(string, DownOptions)"/> will have <see href="https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/repeat">repeat</see> set to <c>true</c>. To release the key, use <see cref="UpAsync(string)"/>30 /// </remarks>31 /// <returns>Task</returns>32 public Task DownAsync(string key, DownOptions options = null)33 {34 var description = KeyDescriptionForString(key);35 var autoRepeat = _pressedKeys.Contains(description.Code);36 _pressedKeys.Add(description.Code);37 Modifiers |= ModifierBit(key);38 var text = options?.Text == null ? description.Text : options.Text;39 return _client.SendAsync("Input.dispatchKeyEvent", new Dictionary<string, object>40 {41 { MessageKeys.Type, text != null ? "keyDown" : "rawKeyDown" },42 { MessageKeys.Modifiers, Modifiers },43 { MessageKeys.WindowsVirtualKeyCode, description.KeyCode },44 { MessageKeys.Code, description.Code },45 { MessageKeys.Key, description.Key },46 { MessageKeys.Text, text },47 { MessageKeys.UnmodifiedText, text },48 { MessageKeys.AutoRepeat, autoRepeat },49 { MessageKeys.Location, description.Location },50 { MessageKeys.IsKeypad, description.Location == 3 }51 });52 }53 /// <summary>54 /// Dispatches a <c>keyup</c> event.55 /// </summary>56 /// <param name="key">Name of key to release, such as `ArrowLeft`. See <see cref="KeyDefinitions"/> for a list of all key names.</param>57 /// <returns>Task</returns>58 public Task UpAsync(string key)59 {60 var description = KeyDescriptionForString(key);61 Modifiers &= ~ModifierBit(key);62 _pressedKeys.Remove(description.Code);63 return _client.SendAsync("Input.dispatchKeyEvent", new Dictionary<string, object>64 {65 { MessageKeys.Type, "keyUp" },66 { MessageKeys.Modifiers, Modifiers },67 { MessageKeys.Key, description.Key },68 { MessageKeys.WindowsVirtualKeyCode, description.KeyCode },69 { MessageKeys.Code, description.Code },70 { MessageKeys.Location, description.Location }71 });72 }73 /// <summary>74 /// Dispatches a <c>keypress</c> and <c>input</c> event. This does not send a <c>keydown</c> or <c>keyup</c> event.75 /// </summary>76 /// <param name="charText">Character to send into the page</param>77 /// <returns>Task</returns>78 public Task SendCharacterAsync(string charText)79 => _client.SendAsync("Input.insertText", new Dictionary<string, object> { [MessageKeys.Text] = charText });80 /// <summary>81 /// Sends a <c>keydown</c>, <c>keypress</c>/<c>input</c>, and <c>keyup</c> event for each character in the text.82 /// </summary>83 /// <param name="text">A text to type into a focused element</param>84 /// <param name="options">type options</param>85 /// <remarks>86 /// To press a special key, like <c>Control</c> or <c>ArrowDown</c>, use <see cref="PressAsync(string, PressOptions)"/>87 /// </remarks>88 /// <returns>Task</returns>89 public async Task TypeAsync(string text, TypeOptions options = null)90 {91 var delay = 0;92 if (options?.Delay != null)93 {94 delay = (int)options.Delay;95 }96 var textParts = StringInfo.GetTextElementEnumerator(text);97 while (textParts.MoveNext())98 {99 var letter = textParts.Current;100 if (KeyDefinitions.ContainsKey(letter.ToString()))101 {102 await PressAsync(letter.ToString(), new PressOptions { Delay = delay }).ConfigureAwait(false);103 }104 else105 {106 await SendCharacterAsync(letter.ToString()).ConfigureAwait(false);107 }108 if (delay > 0)109 {110 await Task.Delay(delay).ConfigureAwait(false);111 }112 }113 }114 /// <summary>115 /// Shortcut for <see cref="DownAsync(string, DownOptions)"/> and <see cref="UpAsync(string)"/>116 /// </summary>117 /// <param name="key">Name of key to press, such as <c>ArrowLeft</c>. <see cref="KeyDefinitions"/> for a list of all key names.</param>118 /// <param name="options">press options</param>119 /// <remarks>120 /// If <paramref name="key"/> is a single character and no modifier keys besides <c>Shift</c> are being held down, a <c>keypress</c>/<c>input</c> event will also generated. The <see cref="DownOptions.Text"/> option can be specified to force an input event to be generated.121 /// Modifier keys DO effect <see cref="ElementHandle.PressAsync(string, PressOptions)"/>. Holding down <c>Shift</c> will type the text in upper case.122 /// </remarks>123 /// <returns>Task</returns>124 public async Task PressAsync(string key, PressOptions options = null)125 {126 await DownAsync(key, options).ConfigureAwait(false);127 if (options?.Delay > 0)128 {129 await Task.Delay((int)options.Delay).ConfigureAwait(false);130 }131 await UpAsync(key).ConfigureAwait(false);132 }133 private int ModifierBit(string key)134 {...
PressOptions.cs
Source:PressOptions.cs
...4 /// options to use when pressing a key.5 /// </summary>6 /// <seealso cref="Keyboard.PressAsync(string, PressOptions)"/>7 /// <seealso cref="ElementHandle.PressAsync(string, PressOptions)"/>8 public class PressOptions : DownOptions9 {10 /// <summary>11 /// Time to wait between <c>keydown</c> and <c>keyup</c> in milliseconds. Defaults to 0.12 /// </summary>13 public int? Delay { get; set; }14 }...
DownOptions.cs
Source:DownOptions.cs
1namespace PuppeteerSharp.Input2{3 /// <summary>4 /// options to use with <see cref="Keyboard.DownAsync(string, DownOptions)"/>5 /// </summary>6 public class DownOptions7 {8 /// <summary>9 /// If specified, generates an input event with this text10 /// </summary>11 public string Text { get; set; }12 }...
DownOptions
Using AI Code Generation
1using PuppeteerSharp.Input;2using PuppeteerSharp.Input;3using PuppeteerSharp.Input;4using PuppeteerSharp.Input;5using PuppeteerSharp.Input;6using PuppeteerSharp.Input;7using PuppeteerSharp.Input;8using PuppeteerSharp.Input;9using PuppeteerSharp.Input;10using PuppeteerSharp.Input;11using PuppeteerSharp.Input;12using PuppeteerSharp.Input;13using PuppeteerSharp.Input;
DownOptions
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4using PuppeteerSharp.Input;5using System.IO;6{7 {8 static async Task Main(string[] args)9 {10 var browser = await Puppeteer.LaunchAsync(new LaunchOptions11 {12 Args = new[] { "--start-maximized" }13 });14 var page = await browser.NewPageAsync();15 await page.ScreenshotAsync("google.png");16 await page.PdfAsync("google.pdf");17 await page.EvaluateFunctionAsync("() => alert('Hello from PuppeteerSharp!')");18 var downloadPath = Path.Combine(Directory.GetCurrentDirectory(), "downloads");19 await page.SetDownloadBehaviorAsync(new DownOptions { Behavior = "allow", DownloadPath = downloadPath });
DownOptions
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4using PuppeteerSharp.Input;5{6 {7 static void Main(string[] args)8 {9 MainAsync().Wait();10 }11 static async Task MainAsync()12 {13 var options = new LaunchOptions { Headless = false };14 var browser = await Puppeteer.LaunchAsync(options);15 var page = await browser.NewPageAsync();16 await page.ScreenshotAsync("google.png");17 await browser.CloseAsync();18 }19 }20}21using System;22using System.Threading.Tasks;23using PuppeteerSharp;24using PuppeteerSharp.Input;25{26 {27 static void Main(string[] args)28 {29 MainAsync().Wait();30 }31 static async Task MainAsync()32 {33 var options = new LaunchOptions { Headless = false };34 var browser = await Puppeteer.LaunchAsync(options);35 var page = await browser.NewPageAsync();36 {37 };38 await page.ScreenshotAsync("google.png", options);39 await browser.CloseAsync();40 }41 }42}
DownOptions
Using AI Code Generation
1using PuppeteerSharp.Input;2using System;3using System.Threading.Tasks;4{5 {6 public int ClickCount { get; set; }7 public float Delay { get; set; }8 }9}10using PuppeteerSharp.Input;11using System;12using System.Threading.Tasks;13{14 {15 public int ClickCount { get; set; }16 public float Delay { get; set; }17 }18}19using PuppeteerSharp.Input;20using System;21using System.Threading.Tasks;22{23 {24 public int ClickCount { get; set; }25 public float Delay { get; set; }26 }27}28using PuppeteerSharp.Input;29using System;30using System.Threading.Tasks;31{32 {33 public int ClickCount { get; set; }34 public float Delay { get; set; }35 }36}37using PuppeteerSharp.Input;38using System;39using System.Threading.Tasks;40{41 {42 public int ClickCount { get; set; }43 public float Delay { get; set; }44 }45}46using PuppeteerSharp.Input;47using System;48using System.Threading.Tasks;49{50 {51 public int ClickCount { get; set; }52 public float Delay { get; set; }53 }54}55using PuppeteerSharp.Input;56using System;57using System.Threading.Tasks;58{59 {60 public int ClickCount { get; set; }61 public float Delay { get; set; }62 }63}
DownOptions
Using AI Code Generation
1var options = new DownOptions{Delay = 100};2await page.Keyboard.DownAsync("Shift", options);3var options = new DownOptions{Delay = 100};4await page.Keyboard.DownAsync("Shift", options);5var options = new DownOptions{Delay = 100};6await page.Keyboard.DownAsync("Shift", options);7var options = new DownOptions{Delay = 100};8await page.Keyboard.DownAsync("Shift", options);9var options = new DownOptions{Delay = 100};10await page.Keyboard.DownAsync("Shift", options);11var options = new DownOptions{Delay = 100};12await page.Keyboard.DownAsync("Shift", options);13var options = new DownOptions{Delay = 100};14await page.Keyboard.DownAsync("Shift", options);15var options = new DownOptions{Delay = 100};16await page.Keyboard.DownAsync("Shift", options);17var options = new DownOptions{Delay = 100};18await page.Keyboard.DownAsync("Shift", options);19var options = new DownOptions{Delay = 100};20await page.Keyboard.DownAsync("Shift", options);21var options = new DownOptions{Delay = 100};22await page.Keyboard.DownAsync("Shift", options);23var options = new DownOptions{Delay = 100};24await page.Keyboard.DownAsync("Shift", options);
DownOptions
Using AI Code Generation
1var options = new DownOptions { Delay = 100 };2await page.Keyboard.DownAsync("ControlLeft", options);3await page.Keyboard.DownAsync("KeyA", options);4await page.Keyboard.UpAsync("ControlLeft", options);5var options = new DownOptions { Delay = 100 };6await page.Keyboard.DownAsync("ControlLeft", options);7await page.Keyboard.DownAsync("KeyA", options);8await page.Keyboard.UpAsync("ControlLeft", options);9var options = new DownOptions { Delay = 100 };10await page.Keyboard.DownAsync("ControlLeft", options);11await page.Keyboard.DownAsync("KeyA", options);12await page.Keyboard.UpAsync("ControlLeft", options);13var options = new DownOptions { Delay = 100 };14await page.Keyboard.DownAsync("ControlLeft", options);15await page.Keyboard.DownAsync("KeyA", options);16await page.Keyboard.UpAsync("ControlLeft", options);17var options = new DownOptions { Delay = 100 };18await page.Keyboard.DownAsync("ControlLeft", options);19await page.Keyboard.DownAsync("KeyA", options);20await page.Keyboard.UpAsync("ControlLeft", options);21var options = new DownOptions { Delay = 100 };22await page.Keyboard.DownAsync("ControlLeft", options);23await page.Keyboard.DownAsync("KeyA", options);24await page.Keyboard.UpAsync("ControlLeft", options);25var options = new DownOptions { Delay = 100 };26await page.Keyboard.DownAsync("ControlLeft", options);27await page.Keyboard.DownAsync("KeyA", options);28await page.Keyboard.UpAsync("ControlLeft", options);29var options = new DownOptions { Delay = 100 };30await page.Keyboard.DownAsync("ControlLeft", options);31await page.Keyboard.DownAsync("KeyA", options);
DownOptions
Using AI Code Generation
1var downOptions = new DownOptions {Text = "Hello"};2await page.Keyboard.DownAsync("Shift", downOptions);3var upOptions = new UpOptions {Text = "Hello"};4await page.Keyboard.UpAsync("Shift", upOptions);5var keyboardOptions = new KeyboardOptions {Text = "Hello"};6await page.Keyboard.DownAsync("Shift", keyboardOptions);7await page.Keyboard.UpAsync("Shift", keyboardOptions);8var keyboardOptions = new KeyboardOptions {Text = "Hello"};9await page.Keyboard.PressAsync("Shift", keyboardOptions);10var keyboardOptions = new KeyboardOptions {Text = "Hello"};11await page.Keyboard.SendCharacterAsync("Shift", keyboardOptions);12var keyboardOptions = new KeyboardOptions {Text = "Hello"};13await page.Keyboard.TypeAsync("Shift", keyboardOptions);14var keyboardOptions = new KeyboardOptions {Text = "Hello"};15await page.Keyboard.InsertTextAsync("Shift", keyboardOptions);16var keyboardOptions = new KeyboardOptions {Text = "Hello"};17await page.Keyboard.SendCharacterAsync("Shift", keyboardOptions);18var keyboardOptions = new KeyboardOptions {Text = "Hello"};19await page.Keyboard.SendCharacterAsync("Shift", keyboardOptions);20var keyboardOptions = new KeyboardOptions {Text = "Hello"};21await page.Keyboard.SendCharacterAsync("Shift", keyboardOptions);22var keyboardOptions = new KeyboardOptions {Text = "Hello"};23await page.Keyboard.SendCharacterAsync("Shift", keyboardOptions);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!