How to use SendCharacterAsync method of PuppeteerSharp.Input.Keyboard class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Input.Keyboard.SendCharacterAsync

InputTests.cs

Source:InputTests.cs Github

copy

Full Screen

...148 public async Task ShouldSendACharacterWithSendCharacter()149 {150 await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");151 await Page.FocusAsync("textarea");152 await Page.Keyboard.SendCharacterAsync("嗨");153 Assert.Equal("嗨", await Page.EvaluateExpressionAsync("document.querySelector('textarea').value"));154 await Page.EvaluateExpressionAsync("window.addEventListener('keydown', e => e.preventDefault(), true)");155 await Page.Keyboard.SendCharacterAsync("a");156 Assert.Equal("嗨a", await Page.EvaluateExpressionAsync("document.querySelector('textarea').value"));157 }158 [Fact]159 public async Task ShouldReportShiftKey()160 {161 await Page.GoToAsync(TestConstants.ServerUrl + "/input/keyboard.html");162 var keyboard = Page.Keyboard;163 var codeForKey = new Dictionary<string, int> { ["Shift"] = 16, ["Alt"] = 18, ["Meta"] = 91, ["Control"] = 17 };164 foreach (var modifier in codeForKey)165 {166 await keyboard.DownAsync(modifier.Key);167 Assert.Equal($"Keydown: {modifier.Key} {modifier.Key}Left {modifier.Value} [{modifier.Key}]", await Page.EvaluateExpressionAsync<string>("getResult()"));168 await keyboard.DownAsync("!");169 // Shift+! will generate a keypress...

Full Screen

Full Screen

KeyboardTests.cs

Source:KeyboardTests.cs Github

copy

Full Screen

...64 public async Task ShouldSendACharacterWithSendCharacter()65 {66 await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");67 await Page.FocusAsync("textarea");68 await Page.Keyboard.SendCharacterAsync("嗨");69 Assert.Equal("嗨", await Page.EvaluateExpressionAsync<string>("document.querySelector('textarea').value"));70 await Page.EvaluateExpressionAsync("window.addEventListener('keydown', e => e.preventDefault(), true)");71 await Page.Keyboard.SendCharacterAsync("a");72 Assert.Equal("嗨a", await Page.EvaluateExpressionAsync<string>("document.querySelector('textarea').value"));73 }74 [Fact]75 public async Task ShouldReportShiftKey()76 {77 await Page.GoToAsync(TestConstants.ServerUrl + "/input/keyboard.html");78 var keyboard = Page.Keyboard;79 var codeForKey = new Dictionary<string, int> { ["Shift"] = 16, ["Alt"] = 18, ["Control"] = 17 };80 foreach (var modifier in codeForKey)81 {82 await keyboard.DownAsync(modifier.Key);83 Assert.Equal($"Keydown: {modifier.Key} {modifier.Key}Left {modifier.Value} [{modifier.Key}]", await Page.EvaluateExpressionAsync<string>("getResult()"));84 await keyboard.DownAsync("!");85 // Shift+! will generate a keypress...

Full Screen

Full Screen

Keyboard.cs

Source:Keyboard.cs Github

copy

Full Screen

...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....

Full Screen

Full Screen

BaseAuthUIFixture.cs

Source:BaseAuthUIFixture.cs Github

copy

Full Screen

...48 if(OperatingSystem.IsWindows())49 await Page.ConfirmScreenshot(screensName).ConfigureAwait(false);50 response.Url.Should().Contain("login.microsoftonline.com");51 await Page.WaitForSelectorAsync("input[name='loginfmt']").ConfigureAwait(false);52 await Page.Keyboard.SendCharacterAsync(TestGoodUserName).ConfigureAwait(false);53 await Page.Keyboard.PressAsync(Key.Enter).ConfigureAwait(false);54 await Page.WaitForSelectorAsync("input[name='passwd']:not(.moveOffScreen)").ConfigureAwait(false);55 await Page.Keyboard.SendCharacterAsync(TestPassword).ConfigureAwait(false);56 await Page.Keyboard.PressAsync(Key.Enter).ConfigureAwait(false);57 await Page.WaitForSelectorAsync("input[id='idSIButton9']").ConfigureAwait(false); ;58 Thread.Sleep(delay);59 if (OperatingSystem.IsWindows())60 await Page.ConfirmScreenshot(screensName).ConfigureAwait(false);61 await Page.Keyboard.PressAsync(Key.Enter).ConfigureAwait(false);62 await Page.GoToAsync(AuthUrl, options: new NavigationOptions { WaitUntil = new[] { WaitUntilNavigation.Networkidle0 } }).ConfigureAwait(false);63 //response = await page.WaitForNavigationAsync().ConfigureAwait(false);64 Authorized = true;65 }66 else67 {68 response = await Page.GoToAsync(AuthUrl).ConfigureAwait(false); ;69 }...

Full Screen

Full Screen

SendCharacterAsync

Using AI Code Generation

copy

Full Screen

1var page = await browser.NewPageAsync();2await page.Keyboard.SendCharacterAsync('a');3await page.Keyboard.SendCharacterAsync('b');4await page.Keyboard.SendCharacterAsync('c');5await page.Keyboard.SendCharacterAsync('d');6await page.Keyboard.SendCharacterAsync('e');7await page.Keyboard.SendCharacterAsync('f');8await page.Keyboard.SendCharacterAsync('g');9await page.Keyboard.SendCharacterAsync('h');10await page.Keyboard.SendCharacterAsync('i');11await page.Keyboard.SendCharacterAsync('j');12await page.Keyboard.SendCharacterAsync('k');13await page.Keyboard.SendCharacterAsync('l');14await page.Keyboard.SendCharacterAsync('m');15await page.Keyboard.SendCharacterAsync('n');16await page.Keyboard.SendCharacterAsync('o');17await page.Keyboard.SendCharacterAsync('p');18await page.Keyboard.SendCharacterAsync('q');19await page.Keyboard.SendCharacterAsync('r');20await page.Keyboard.SendCharacterAsync('s');21await page.Keyboard.SendCharacterAsync('t');22await page.Keyboard.SendCharacterAsync('u');23await page.Keyboard.SendCharacterAsync('v');24await page.Keyboard.SendCharacterAsync('w');25await page.Keyboard.SendCharacterAsync('x');26await page.Keyboard.SendCharacterAsync('y');27await page.Keyboard.SendCharacterAsync('z');

Full Screen

Full Screen

SendCharacterAsync

Using AI Code Generation

copy

Full Screen

1await page.Keyboard.SendCharacterAsync('a');2await page.Keyboard.SendCharacterAsync('b');3await page.Keyboard.SendCharacterAsync('c');4await page.Keyboard.SendCharacterAsync('d');5await page.Keyboard.SendCharacterAsync('a');6await page.Keyboard.SendCharacterAsync('b');7await page.Keyboard.SendCharacterAsync('c');8await page.Keyboard.SendCharacterAsync('d');9await page.Keyboard.SendCharacterAsync('a');10await page.Keyboard.SendCharacterAsync('b');11await page.Keyboard.SendCharacterAsync('c');12await page.Keyboard.SendCharacterAsync('d');13await page.Keyboard.SendCharacterAsync('a');14await page.Keyboard.SendCharacterAsync('b');15await page.Keyboard.SendCharacterAsync('c');16await page.Keyboard.SendCharacterAsync('d');17await page.Keyboard.SendCharacterAsync('a');18await page.Keyboard.SendCharacterAsync('b');19await page.Keyboard.SendCharacterAsync('c');20await page.Keyboard.SendCharacterAsync('d');21await page.Keyboard.SendCharacterAsync('a');22await page.Keyboard.SendCharacterAsync('b');23await page.Keyboard.SendCharacterAsync('c');24await page.Keyboard.SendCharacterAsync('d');25await page.Keyboard.SendCharacterAsync('a');26await page.Keyboard.SendCharacterAsync('b');27await page.Keyboard.SendCharacterAsync('c');28await page.Keyboard.SendCharacterAsync('d');

Full Screen

Full Screen

SendCharacterAsync

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 Console.WriteLine("Hello World!");9 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });10 var page = await browser.NewPageAsync();11 await page.WaitForSelectorAsync("input[name=q]");12 await page.FocusAsync("input[name=q]");13 await page.Keyboard.SendCharacterAsync("Hello World!");14 await Task.Delay(5000);15 await browser.CloseAsync();16 }17 }18}

Full Screen

Full Screen

SendCharacterAsync

Using AI Code Generation

copy

Full Screen

1using System.Threading.Tasks;2using PuppeteerSharp;3{4 {5 private readonly CDPSession _client;6 private readonly KeyboardWatcher _keyboardWatcher;7 private readonly Page _page;8 private readonly Keyboard _keyboard;9 public Keyboard(Page page)10 {11 _page = page;12 _client = page._client;13 _keyboardWatcher = new KeyboardWatcher(_client, page);14 _keyboard = new Keyboard(_page);15 }16 public async Task SendCharacterAsync(string character)17 {18 await _keyboard.SendCharacterAsync(character);19 }20 }21}22using System.Threading.Tasks;23using PuppeteerSharp;24{25 {26 private readonly CDPSession _client;27 private readonly KeyboardWatcher _keyboardWatcher;28 private readonly Page _page;29 private readonly Keyboard _keyboard;30 public Keyboard(Page page)31 {32 _page = page;33 _client = page._client;34 _keyboardWatcher = new KeyboardWatcher(_client, page);35 _keyboard = new Keyboard(_page);36 }37 public async Task SendCharacterAsync(string character)38 {39 await _keyboard.SendCharacterAsync(character);40 }41 }42}43using System.Threading.Tasks;44using PuppeteerSharp;45{46 {47 private readonly CDPSession _client;48 private readonly KeyboardWatcher _keyboardWatcher;49 private readonly Page _page;50 private readonly Keyboard _keyboard;51 public Keyboard(Page page)52 {53 _page = page;54 _client = page._client;55 _keyboardWatcher = new KeyboardWatcher(_client, page);56 _keyboard = new Keyboard(_page);57 }58 public async Task SendCharacterAsync(string character)59 {60 await _keyboard.SendCharacterAsync(character);61 }62 }63}64using System.Threading.Tasks;65using PuppeteerSharp;66{67 {68 private readonly CDPSession _client;69 private readonly KeyboardWatcher _keyboardWatcher;

Full Screen

Full Screen

SendCharacterAsync

Using AI Code Generation

copy

Full Screen

1var page = await browser.NewPageAsync();2await page.Keyboard.SendCharacterAsync("hello world");3await page.ScreenshotAsync("example.png");4var page = await browser.NewPageAsync();5await page.Keyboard.SendCharacterAsync("hello world");6await page.ScreenshotAsync("example.png");7var page = await browser.NewPageAsync();8await page.Keyboard.SendCharacterAsync("hello world");9await page.ScreenshotAsync("example.png");10var page = await browser.NewPageAsync();11await page.Keyboard.SendCharacterAsync("hello world");12await page.ScreenshotAsync("example.png");13var page = await browser.NewPageAsync();14await page.Keyboard.SendCharacterAsync("hello world");15await page.ScreenshotAsync("example.png");16var page = await browser.NewPageAsync();17await page.Keyboard.SendCharacterAsync("hello world");18await page.ScreenshotAsync("example.png");19var page = await browser.NewPageAsync();20await page.Keyboard.SendCharacterAsync("hello world");21await page.ScreenshotAsync("example.png");22var page = await browser.NewPageAsync();23await page.Keyboard.SendCharacterAsync("hello world");24await page.ScreenshotAsync("example.png");

Full Screen

Full Screen

SendCharacterAsync

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp;2using System.Threading.Tasks;3{4 static async Task Main()5 {6 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);7 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });8 var page = await browser.NewPageAsync();9 await page.GoToAsync(url);10 await page.FocusAsync("input[name='q']");11 await page.Keyboard.SendCharacterAsync("PuppeteerSharp");12 await page.Keyboard.PressAsync("Enter");13 await page.WaitForNavigationAsync();14 await page.ScreenshotAsync("screenshot.png");15 await browser.CloseAsync();16 }17}

Full Screen

Full Screen

SendCharacterAsync

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 var browser = await Puppeteer.LaunchAsync(new LaunchOptions9 {10 });11 var page = await browser.NewPageAsync();12 await page.Keyboard.SendCharacterAsync("A");13 await page.Mouse.ClickAsync();14 }15 }16}17using System;18using System.Threading.Tasks;19using PuppeteerSharp;20{21 {22 static async Task Main(string[] args)23 {24 var browser = await Puppeteer.LaunchAsync(new LaunchOptions25 {26 });27 var page = await browser.NewPageAsync();28 await page.Keyboard.SendCharacterAsync("A");29 await page.Keyboard.PressAsync("Enter");30 }31 }32}33using System;34using System.Threading.Tasks;35using PuppeteerSharp;36{37 {38 static async Task Main(string[] args)39 {40 var browser = await Puppeteer.LaunchAsync(new LaunchOptions41 {42 });43 var page = await browser.NewPageAsync();44 await page.Keyboard.SendCharacterAsync("A");45 await page.Keyboard.PressAsync("Enter");46 await page.WaitForNavigationAsync();47 }48 }49}

Full Screen

Full Screen

SendCharacterAsync

Using AI Code Generation

copy

Full Screen

1await page.ClickAsync("body");2await page.Keyboard.SendCharacterAsync("a");3await page.Keyboard.SendCharacterAsync("b");4await page.Keyboard.SendCharacterAsync("c");5await page.Keyboard.SendCharacterAsync("d");6await page.Keyboard.SendCharacterAsync("e");7await page.Keyboard.SendCharacterAsync("f");8await page.Keyboard.SendCharacterAsync("g");9await page.Keyboard.SendCharacterAsync("h");10await page.Keyboard.SendCharacterAsync("i");11await page.Keyboard.SendCharacterAsync("j");12await page.Keyboard.SendCharacterAsync("k");13await page.Keyboard.SendCharacterAsync("l");14await page.Keyboard.SendCharacterAsync("m");15await page.Keyboard.SendCharacterAsync("n");16await page.Keyboard.SendCharacterAsync("o");17await page.Keyboard.SendCharacterAsync("p");18await page.Keyboard.SendCharacterAsync("q");19await page.Keyboard.SendCharacterAsync("r");20await page.Keyboard.SendCharacterAsync("s");21await page.Keyboard.SendCharacterAsync("t");22await page.Keyboard.SendCharacterAsync("u");23await page.Keyboard.SendCharacterAsync("v");24await page.Keyboard.SendCharacterAsync("w");25await page.Keyboard.SendCharacterAsync("x");26await page.Keyboard.SendCharacterAsync("y");27await page.Keyboard.SendCharacterAsync("z");28await page.Keyboard.SendCharacterAsync("1");29await page.Keyboard.SendCharacterAsync("2");30await page.Keyboard.SendCharacterAsync("3");31await page.Keyboard.SendCharacterAsync("4");32await page.Keyboard.SendCharacterAsync("5");33await page.Keyboard.SendCharacterAsync("6");34await page.Keyboard.SendCharacterAsync("7");35await page.Keyboard.SendCharacterAsync("8");36await page.Keyboard.SendCharacterAsync("9");37await page.Keyboard.SendCharacterAsync("0");38await page.Keyboard.SendCharacterAsync("!");39await page.Keyboard.SendCharacterAsync("@");40await page.Keyboard.SendCharacterAsync("#");41await page.Keyboard.SendCharacterAsync("$");42await page.Keyboard.SendCharacterAsync("%");43await page.Keyboard.SendCharacterAsync("^");44await page.Keyboard.SendCharacterAsync("&");45await page.Keyboard.SendCharacterAsync("*");46await page.Keyboard.SendCharacterAsync("(");47await page.Keyboard.SendCharacterAsync(")");48await page.Keyboard.SendCharacterAsync("-");49await page.Keyboard.SendCharacterAsync("_");50await page.Keyboard.SendCharacterAsync("+");51await page.Keyboard.SendCharacterAsync("=");52await page.Keyboard.SendCharacterAsync("`");53await page.Keyboard.SendCharacterAsync("~");

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful