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

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

InputTests.cs

Source:InputTests.cs Github

copy

Full Screen

...77 {78 await Page.SetViewportAsync(TestConstants.IPhone.ViewPort);79 await Page.Mouse.DownAsync();80 await Page.Mouse.MoveAsync(100, 10);81 await Page.Mouse.UpAsync();82 }83 [Fact]84 public async Task ShouldTypeIntoTheTextarea()85 {86 await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");87 var textarea = await Page.QuerySelectorAsync("textarea");88 await textarea.TypeAsync("Type in this text!");89 Assert.Equal("Type in this text!", await Page.EvaluateExpressionAsync<string>("result"));90 }91 [Fact]92 public async Task ShouldClickTheButtonAfterNavigation()93 {94 await Page.GoToAsync(TestConstants.ServerUrl + "/input/button.html");95 await Page.ClickAsync("button");96 await Page.GoToAsync(TestConstants.ServerUrl + "/input/button.html");97 await Page.ClickAsync("button");98 Assert.Equal("Clicked", await Page.EvaluateExpressionAsync<string>("result"));99 }100 [Fact]101 public async Task ShouldUploadTheFile()102 {103 await Page.GoToAsync(TestConstants.ServerUrl + "/input/fileupload.html");104 var filePath = "./assets/file-to-upload.txt";105 var input = await Page.QuerySelectorAsync("input");106 await input.UploadFileAsync(filePath);107 Assert.Equal("file-to-upload.txt", await Page.EvaluateFunctionAsync<string>("e => e.files[0].name", input));108 Assert.Equal("contents of the file", await Page.EvaluateFunctionAsync(@"e => {109 const reader = new FileReader();110 const promise = new Promise(fulfill => reader.onload = fulfill);111 reader.readAsText(e.files[0]);112 return promise.then(() => reader.result);113 }", input));114 }115 [Fact]116 public async Task ShouldMoveWithTheArrowKeys()117 {118 await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");119 await Page.TypeAsync("textarea", "Hello World!");120 Assert.Equal("Hello World!", await Page.EvaluateExpressionAsync<string>("document.querySelector('textarea').value"));121 for (var i = 0; i < "World!".Length; i++)122 {123 dummy = Page.Keyboard.PressAsync("ArrowLeft");124 }125 await Page.Keyboard.TypeAsync("inserted ");126 Assert.Equal("Hello inserted World!", await Page.EvaluateExpressionAsync<string>("document.querySelector('textarea').value"));127 dummy = Page.Keyboard.DownAsync("Shift");128 for (var i = 0; i < "inserted ".Length; i++)129 {130 dummy = Page.Keyboard.PressAsync("ArrowLeft");131 }132 dummy = Page.Keyboard.UpAsync("Shift");133 await Page.Keyboard.PressAsync("Backspace");134 Assert.Equal("Hello World!", await Page.EvaluateExpressionAsync<string>("document.querySelector('textarea').value"));135 }136 [Fact]137 public async Task ShouldSendACharacterWithElementHandlePress()138 {139 await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");140 var textarea = await Page.QuerySelectorAsync("textarea");141 await textarea.PressAsync("a", new PressOptions { Text = "f" });142 Assert.Equal("f", await Page.EvaluateExpressionAsync<string>("document.querySelector('textarea').value"));143 await Page.EvaluateExpressionAsync("window.addEventListener('keydown', e => e.preventDefault(), true)");144 await textarea.PressAsync("a", new PressOptions { Text = "y" });145 Assert.Equal("f", await Page.EvaluateExpressionAsync<string>("document.querySelector('textarea').value"));146 }147 [Fact]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 keypress170 if (modifier.Key == "Shift")171 {172 Assert.Equal($"Keydown: ! Digit1 49 [{modifier.Key}]\nKeypress: ! Digit1 33 33 33 [{modifier.Key}]", await Page.EvaluateExpressionAsync<string>("getResult()"));173 }174 else175 {176 Assert.Equal($"Keydown: ! Digit1 49 [{modifier.Key}]", await Page.EvaluateExpressionAsync<string>("getResult()"));177 }178 await keyboard.UpAsync("!");179 Assert.Equal($"Keyup: ! Digit1 49 [{modifier.Key}]", await Page.EvaluateExpressionAsync<string>("getResult()"));180 await keyboard.UpAsync(modifier.Key);181 Assert.Equal($"Keyup: {modifier.Key} {modifier.Key}Left {modifier.Value} []", await Page.EvaluateExpressionAsync<string>("getResult()"));182 }183 }184 [Fact]185 public async Task ShouldReportMultipleModifiers()186 {187 await Page.GoToAsync(TestConstants.ServerUrl + "/input/keyboard.html");188 var keyboard = Page.Keyboard;189 await keyboard.DownAsync("Control");190 Assert.Equal("Keydown: Control ControlLeft 17 [Control]", await Page.EvaluateExpressionAsync<string>("getResult()"));191 await keyboard.DownAsync("Meta");192 Assert.Equal("Keydown: Meta MetaLeft 91 [Control Meta]", await Page.EvaluateExpressionAsync<string>("getResult()"));193 await keyboard.DownAsync(";");194 Assert.Equal("Keydown: ; Semicolon 186 [Control Meta]", await Page.EvaluateExpressionAsync<string>("getResult()"));195 await keyboard.UpAsync(";");196 Assert.Equal("Keyup: ; Semicolon 186 [Control Meta]", await Page.EvaluateExpressionAsync<string>("getResult()"));197 await keyboard.UpAsync("Control");198 Assert.Equal("Keyup: Control ControlLeft 17 [Meta]", await Page.EvaluateExpressionAsync<string>("getResult()"));199 await keyboard.UpAsync("Meta");200 Assert.Equal("Keyup: Meta MetaLeft 91 []", await Page.EvaluateExpressionAsync<string>("getResult()"));201 }202 [Fact]203 public async Task ShouldSendProperCodesWhileTyping()204 {205 await Page.GoToAsync(TestConstants.ServerUrl + "/input/keyboard.html");206 await Page.Keyboard.TypeAsync("!");207 Assert.Equal(string.Join("\n", new[] {208 "Keydown: ! Digit1 49 []",209 "Keypress: ! Digit1 33 33 33 []",210 "Keyup: ! Digit1 49 []" }), await Page.EvaluateExpressionAsync<string>("getResult()"));211 await Page.Keyboard.TypeAsync("^");212 Assert.Equal(string.Join("\n", new[] {213 "Keydown: ^ Digit6 54 []",214 "Keypress: ^ Digit6 94 94 94 []",215 "Keyup: ^ Digit6 54 []" }), await Page.EvaluateExpressionAsync<string>("getResult()"));216 }217 [Fact]218 public async Task ShouldSendProperCodesWhileTypingWithShift()219 {220 await Page.GoToAsync(TestConstants.ServerUrl + "/input/keyboard.html");221 var keyboard = Page.Keyboard;222 await keyboard.DownAsync("Shift");223 await Page.Keyboard.TypeAsync("~");224 Assert.Equal(string.Join("\n", new[] {225 "Keydown: Shift ShiftLeft 16 [Shift]",226 "Keydown: ~ Backquote 192 [Shift]", // 192 is ` keyCode227 "Keypress: ~ Backquote 126 126 126 [Shift]", // 126 is ~ charCode228 "Keyup: ~ Backquote 192 [Shift]" }), await Page.EvaluateExpressionAsync<string>("getResult()"));229 await keyboard.UpAsync("Shift");230 }231 [Fact]232 public async Task ShouldNotTypeCanceledEvents()233 {234 await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");235 await Page.FocusAsync("textarea");236 await Page.EvaluateExpressionAsync(@"{237 window.addEventListener('keydown', event => {238 event.stopPropagation();239 event.stopImmediatePropagation();240 if (event.key === 'l')241 event.preventDefault();242 if (event.key === 'o')243 Promise.resolve().then(() => event.preventDefault());244 }, false);245 }");246 await Page.Keyboard.TypeAsync("Hello World!");247 Assert.Equal("He Wrd!", await Page.EvaluateExpressionAsync<string>("textarea.value"));248 }249 [Fact]250 public async Task KeyboardModifiers()251 {252 var keyboard = Page.Keyboard;253 Assert.Equal(0, keyboard.Modifiers);254 await keyboard.DownAsync(Key.Shift);255 Assert.Equal(8, keyboard.Modifiers);256 await keyboard.DownAsync(Key.Alt);257 Assert.Equal(9, keyboard.Modifiers);258 await keyboard.UpAsync(Key.Shift);259 await keyboard.UpAsync(Key.Alt);260 Assert.Equal(0, keyboard.Modifiers);261 }262 [Fact]263 public async Task ShouldResizeTheTextarea()264 {265 await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");266 var dimensions = await Page.EvaluateFunctionAsync<Dimensions>(Dimensions);267 var mouse = Page.Mouse;268 await mouse.MoveAsync(dimensions.X + dimensions.Width - 4, dimensions.Y + dimensions.Height - 4);269 await mouse.DownAsync();270 await mouse.MoveAsync(dimensions.X + dimensions.Width + 100, dimensions.Y + dimensions.Height + 100);271 await mouse.UpAsync();272 var newDimensions = await Page.EvaluateFunctionAsync<Dimensions>(Dimensions);273 Assert.Equal(dimensions.Width + 104, newDimensions.Width);274 Assert.Equal(dimensions.Height + 104, newDimensions.Height);275 }276 [Fact]277 public async Task ShouldScrollAndClickTheButton()278 {279 await Page.GoToAsync(TestConstants.ServerUrl + "/input/scrollable.html");280 await Page.ClickAsync("#button-5");281 Assert.Equal("clicked", await Page.EvaluateExpressionAsync<string>("document.querySelector(\"#button-5\").textContent"));282 await Page.ClickAsync("#button-80");283 Assert.Equal("clicked", await Page.EvaluateExpressionAsync<string>("document.querySelector(\"#button-80\").textContent"));284 }285 [Fact]286 public async Task ShouldDoubleClickTheButton()287 {288 await Page.GoToAsync(TestConstants.ServerUrl + "/input/button.html");289 await Page.EvaluateExpressionAsync(@"{290 window.double = false;291 const button = document.querySelector('button');292 button.addEventListener('dblclick', event => {293 window.double = true;294 });295 }");296 var button = await Page.QuerySelectorAsync("button");297 await button.ClickAsync(new ClickOptions { ClickCount = 2 });298 Assert.True(await Page.EvaluateExpressionAsync<bool>("double"));299 Assert.Equal("Clicked", await Page.EvaluateExpressionAsync<string>("result"));300 }301 [Fact]302 public async Task ShouldClickAPartiallyObscuredButton()303 {304 await Page.GoToAsync(TestConstants.ServerUrl + "/input/button.html");305 await Page.EvaluateExpressionAsync(@"{306 const button = document.querySelector('button');307 button.textContent = 'Some really long text that will go offscreen';308 button.style.position = 'absolute';309 button.style.left = '368px';310 }");311 await Page.ClickAsync("button");312 Assert.Equal("Clicked", await Page.EvaluateExpressionAsync<string>("result"));313 }314 [Fact]315 public async Task ShouldSelectTheTextWithMouse()316 {317 await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");318 await Page.FocusAsync("textarea");319 const string text = "This is the text that we are going to try to select. Let's see how it goes.";320 await Page.Keyboard.TypeAsync(text);321 await Page.EvaluateExpressionAsync("document.querySelector('textarea').scrollTop = 0");322 var dimensions = await Page.EvaluateFunctionAsync<Dimensions>(Dimensions);323 await Page.Mouse.MoveAsync(dimensions.X + 2, dimensions.Y + 2);324 await Page.Mouse.DownAsync();325 await Page.Mouse.MoveAsync(100, 100);326 await Page.Mouse.UpAsync();327 Assert.Equal(text, await Page.EvaluateExpressionAsync<string>("window.getSelection().toString()"));328 }329 [Fact]330 public async Task ShouldSelectTheTextByTripleClicking()331 {332 await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");333 await Page.FocusAsync("textarea");334 const string text = "This is the text that we are going to try to select. Let's see how it goes.";335 await Page.Keyboard.TypeAsync(text);336 await Page.ClickAsync("textarea");337 await Page.ClickAsync("textarea", new ClickOptions { ClickCount = 2 });338 await Page.ClickAsync("textarea", new ClickOptions { ClickCount = 3 });339 Assert.Equal(text, await Page.EvaluateExpressionAsync<string>("window.getSelection().toString()"));340 }341 [Fact]342 public async Task ShouldTriggerHoverState()343 {344 await Page.GoToAsync(TestConstants.ServerUrl + "/input/scrollable.html");345 await Page.HoverAsync("#button-6");346 Assert.Equal("button-6", await Page.EvaluateExpressionAsync<string>("document.querySelector('button:hover').id"));347 await Page.HoverAsync("#button-2");348 Assert.Equal("button-2", await Page.EvaluateExpressionAsync<string>("document.querySelector('button:hover').id"));349 await Page.HoverAsync("#button-91");350 Assert.Equal("button-91", await Page.EvaluateExpressionAsync<string>("document.querySelector('button:hover').id"));351 }352 [Fact]353 public async Task ShouldFireContextmenuEventOnRightClick()354 {355 await Page.GoToAsync(TestConstants.ServerUrl + "/input/scrollable.html");356 await Page.ClickAsync("#button-8", new ClickOptions { Button = MouseButton.Right });357 Assert.Equal("context menu", await Page.EvaluateExpressionAsync<string>("document.querySelector('#button-8').textContent"));358 }359 [Fact]360 public async Task ShouldSetModifierKeysOnClick()361 {362 await Page.GoToAsync(TestConstants.ServerUrl + "/input/scrollable.html");363 await Page.EvaluateExpressionAsync("document.querySelector('#button-3').addEventListener('mousedown', e => window.lastEvent = e, true)");364 var modifiers = new Dictionary<string, string> { ["Shift"] = "shiftKey", ["Control"] = "ctrlKey", ["Alt"] = "altKey", ["Meta"] = "metaKey" };365 foreach (var modifier in modifiers)366 {367 await Page.Keyboard.DownAsync(modifier.Key);368 await Page.ClickAsync("#button-3");369 if (!(await Page.EvaluateFunctionAsync<bool>("mod => window.lastEvent[mod]", modifier.Value)))370 {371 Assert.True(false, $"{modifier.Value} should be true");372 }373 await Page.Keyboard.UpAsync(modifier.Key);374 }375 await Page.ClickAsync("#button-3");376 foreach (var modifier in modifiers)377 {378 if (await Page.EvaluateFunctionAsync<bool>("mod => window.lastEvent[mod]", modifier.Value))379 {380 Assert.False(true, $"{modifiers.Values} should be false");381 }382 }383 }384 [Fact]385 public async Task ShouldSpecifyRepeatProperty()386 {387 await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");...

Full Screen

Full Screen

KeyboardTests.cs

Source:KeyboardTests.cs Github

copy

Full Screen

...36 for (var i = 0; i < "inserted ".Length; i++)37 {38 _ = Page.Keyboard.PressAsync("ArrowLeft");39 }40 _ = Page.Keyboard.UpAsync("Shift");41 await Page.Keyboard.PressAsync("Backspace");42 Assert.Equal("Hello World!", await Page.EvaluateExpressionAsync<string>("document.querySelector('textarea').value"));43 }44 [Fact]45 public async Task ShouldSendACharacterWithElementHandlePress()46 {47 await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");48 var textarea = await Page.QuerySelectorAsync("textarea");49 await textarea.PressAsync("a");50 Assert.Equal("a", await Page.EvaluateExpressionAsync<string>("document.querySelector('textarea').value"));51 await Page.EvaluateExpressionAsync("window.addEventListener('keydown', e => e.preventDefault(), true)");52 await textarea.PressAsync("b");53 Assert.Equal("a", await Page.EvaluateExpressionAsync<string>("document.querySelector('textarea').value"));54 }55 [Fact]56 public async Task ElementHandlePressShouldSupportTextOption()57 {58 await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");59 var textarea = await Page.QuerySelectorAsync("textarea");60 await textarea.PressAsync("a", new PressOptions { Text = "ё" });61 Assert.Equal("ё", await Page.EvaluateExpressionAsync<string>("document.querySelector('textarea').value"));62 }63 [Fact]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 keypress86 if (modifier.Key == "Shift")87 {88 Assert.Equal($"Keydown: ! Digit1 49 [{modifier.Key}]\nKeypress: ! Digit1 33 33 [{modifier.Key}]", await Page.EvaluateExpressionAsync<string>("getResult()"));89 }90 else91 {92 Assert.Equal($"Keydown: ! Digit1 49 [{modifier.Key}]", await Page.EvaluateExpressionAsync<string>("getResult()"));93 }94 await keyboard.UpAsync("!");95 Assert.Equal($"Keyup: ! Digit1 49 [{modifier.Key}]", await Page.EvaluateExpressionAsync<string>("getResult()"));96 await keyboard.UpAsync(modifier.Key);97 Assert.Equal($"Keyup: {modifier.Key} {modifier.Key}Left {modifier.Value} []", await Page.EvaluateExpressionAsync<string>("getResult()"));98 }99 }100 [Fact]101 public async Task ShouldReportMultipleModifiers()102 {103 await Page.GoToAsync(TestConstants.ServerUrl + "/input/keyboard.html");104 var keyboard = Page.Keyboard;105 await keyboard.DownAsync("Control");106 Assert.Equal("Keydown: Control ControlLeft 17 [Control]", await Page.EvaluateExpressionAsync<string>("getResult()"));107 await keyboard.DownAsync("Alt");108 Assert.Equal("Keydown: Alt AltLeft 18 [Alt Control]", await Page.EvaluateExpressionAsync<string>("getResult()"));109 await keyboard.DownAsync(";");110 Assert.Equal("Keydown: ; Semicolon 186 [Alt Control]", await Page.EvaluateExpressionAsync<string>("getResult()"));111 await keyboard.UpAsync(";");112 Assert.Equal("Keyup: ; Semicolon 186 [Alt Control]", await Page.EvaluateExpressionAsync<string>("getResult()"));113 await keyboard.UpAsync("Control");114 Assert.Equal("Keyup: Control ControlLeft 17 [Alt]", await Page.EvaluateExpressionAsync<string>("getResult()"));115 await keyboard.UpAsync("Alt");116 Assert.Equal("Keyup: Alt AltLeft 18 []", await Page.EvaluateExpressionAsync<string>("getResult()"));117 }118 [Fact]119 public async Task ShouldSendProperCodesWhileTyping()120 {121 await Page.GoToAsync(TestConstants.ServerUrl + "/input/keyboard.html");122 await Page.Keyboard.TypeAsync("!");123 Assert.Equal(string.Join("\n", new[] {124 "Keydown: ! Digit1 49 []",125 "Keypress: ! Digit1 33 33 []",126 "Keyup: ! Digit1 49 []" }), await Page.EvaluateExpressionAsync<string>("getResult()"));127 await Page.Keyboard.TypeAsync("^");128 Assert.Equal(string.Join("\n", new[] {129 "Keydown: ^ Digit6 54 []",130 "Keypress: ^ Digit6 94 94 []",131 "Keyup: ^ Digit6 54 []" }), await Page.EvaluateExpressionAsync<string>("getResult()"));132 }133 [Fact]134 public async Task ShouldSendProperCodesWhileTypingWithShift()135 {136 await Page.GoToAsync(TestConstants.ServerUrl + "/input/keyboard.html");137 var keyboard = Page.Keyboard;138 await keyboard.DownAsync("Shift");139 await Page.Keyboard.TypeAsync("~");140 Assert.Equal(string.Join("\n", new[] {141 "Keydown: Shift ShiftLeft 16 [Shift]",142 "Keydown: ~ Backquote 192 [Shift]", // 192 is ` keyCode143 "Keypress: ~ Backquote 126 126 [Shift]", // 126 is ~ charCode144 "Keyup: ~ Backquote 192 [Shift]" }), await Page.EvaluateExpressionAsync<string>("getResult()"));145 await keyboard.UpAsync("Shift");146 }147 [Fact]148 public async Task ShouldNotTypeCanceledEvents()149 {150 await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");151 await Page.FocusAsync("textarea");152 await Page.EvaluateExpressionAsync(@"{153 window.addEventListener('keydown', event => {154 event.stopPropagation();155 event.stopImmediatePropagation();156 if (event.key === 'l')157 event.preventDefault();158 if (event.key === 'o')159 event.preventDefault();160 }, false);161 }");162 await Page.Keyboard.TypeAsync("Hello World!");163 Assert.Equal("He Wrd!", await Page.EvaluateExpressionAsync<string>("textarea.value"));164 }165 [Fact]166 public async Task ShouldSpecifyRepeatProperty()167 {168 await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");169 await Page.FocusAsync("textarea");170 await Page.EvaluateExpressionAsync("document.querySelector('textarea').addEventListener('keydown', e => window.lastEvent = e, true)");171 await Page.Keyboard.DownAsync("a");172 Assert.False(await Page.EvaluateExpressionAsync<bool>("window.lastEvent.repeat"));173 await Page.Keyboard.PressAsync("a");174 Assert.True(await Page.EvaluateExpressionAsync<bool>("window.lastEvent.repeat"));175 await Page.Keyboard.DownAsync("b");176 Assert.False(await Page.EvaluateExpressionAsync<bool>("window.lastEvent.repeat"));177 await Page.Keyboard.DownAsync("b");178 Assert.True(await Page.EvaluateExpressionAsync<bool>("window.lastEvent.repeat"));179 await Page.Keyboard.UpAsync("a");180 await Page.Keyboard.DownAsync("a");181 Assert.False(await Page.EvaluateExpressionAsync<bool>("window.lastEvent.repeat"));182 }183 [Fact]184 public async Task ShouldTypeAllKindsOfCharacters()185 {186 await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");187 await Page.FocusAsync("textarea");188 const string text = "This text goes onto two lines.\nThis character is 嗨.";189 await Page.Keyboard.TypeAsync(text);190 Assert.Equal(text, await Page.EvaluateExpressionAsync<string>("result"));191 }192 [Fact]193 public async Task ShouldSpecifyLocation()...

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.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 {135 if (key == "Alt")136 {137 return 1;138 }139 if (key == "Control")140 {141 return 2;142 }143 if (key == "Meta")144 {145 return 4;...

Full Screen

Full Screen

PupeteerExtensions.cs

Source:PupeteerExtensions.cs Github

copy

Full Screen

...125 public static async Task PressShiftEnterAsync(this Page page)126 {127 await page.Keyboard.DownAsync(Key.Shift);128 await page.Keyboard.PressAsync(Key.Enter);129 await page.Keyboard.UpAsync(Key.Shift);130 }131 132 public static async Task PressControlPaste(this Page page)133 {134 await page.Keyboard.DownAsync(Key.Control);135 await page.Keyboard.PressAsync("V");136 await page.Keyboard.UpAsync(Key.Control);137 }138 }139}

Full Screen

Full Screen

HunterSearch.cs

Source:HunterSearch.cs Github

copy

Full Screen

...44 {45 elementCountPrev = elementCount;46 await page.Keyboard.DownAsync($"{nameof(Key.Control)}");47 await page.Keyboard.PressAsync($"{nameof(Key.End)}");48 await page.Keyboard.UpAsync($"{nameof(Key.Control)}");49 await page.WaitForTimeoutAsync(1500);50 elementHandles = await page.QuerySelectorAllAsync(contentSelector);51 elementCount = elementHandles.Length;52 }53 Report($"query:{query} - found {elementCount} links");54 var downloadLinks = new List<string>(elementCount);55 foreach (var item in elementHandles)56 {57 var text = (await (await item.GetPropertyAsync("href")).JsonValueAsync()).ToString();58 downloadLinks.Add(text);59 }60 navigationOptions = new NavigationOptions { WaitUntil = new[] { WaitUntilNavigation.DOMContentLoaded } };61 foreach (var link in downloadLinks)62 {...

Full Screen

Full Screen

Mouse.cs

Source:Mouse.cs Github

copy

Full Screen

...50 }).ConfigureAwait(false);51 }52 }53 /// <summary>54 /// Shortcut for <see cref="MoveAsync(decimal, decimal, MoveOptions)"/>, <see cref="DownAsync(ClickOptions)"/> and <see cref="UpAsync(ClickOptions)"/>55 /// </summary>56 /// <param name="x"></param>57 /// <param name="y"></param>58 /// <param name="options"></param>59 /// <returns>Task</returns>60 public async Task ClickAsync(decimal x, decimal y, ClickOptions options = null)61 {62 options = options ?? new ClickOptions();63 await MoveAsync(x, y).ConfigureAwait(false);64 await DownAsync(options).ConfigureAwait(false);65 if (options.Delay > 0)66 {67 await Task.Delay(options.Delay).ConfigureAwait(false);68 }69 await UpAsync(options).ConfigureAwait(false);70 }71 /// <summary>72 /// Dispatches a <c>mousedown</c> event.73 /// </summary>74 /// <param name="options"></param>75 /// <returns>Task</returns>76 public Task DownAsync(ClickOptions options = null)77 {78 options = options ?? new ClickOptions();79 _button = options.Button;80 return _client.SendAsync("Input.dispatchMouseEvent", new Dictionary<string, object>()81 {82 { MessageKeys.Type, "mousePressed" },83 { MessageKeys.Button, _button },84 { MessageKeys.X, _x },85 { MessageKeys.Y, _y },86 { MessageKeys.Modifiers, _keyboard.Modifiers },87 { MessageKeys.ClickCount, options.ClickCount }88 });89 }90 /// <summary>91 /// Dispatches a <c>mouseup</c> event.92 /// </summary>93 /// <param name="options"></param>94 /// <returns>Task</returns>95 public Task UpAsync(ClickOptions options = null)96 {97 options = options ?? new ClickOptions();98 _button = MouseButton.None;99 return _client.SendAsync("Input.dispatchMouseEvent", new Dictionary<string, object>()100 {101 { MessageKeys.Type, "mouseReleased" },102 { MessageKeys.Button, options.Button },103 { MessageKeys.X, _x },104 { MessageKeys.Y, _y },105 { MessageKeys.Modifiers, _keyboard.Modifiers },106 { MessageKeys.ClickCount, options.ClickCount }107 });108 }109 }...

Full Screen

Full Screen

CollectVkWall.cs

Source:CollectVkWall.cs Github

copy

Full Screen

...35 if (cycleIndex >= pageCount) break;36 postCountPrev = postCount;37 await page.Keyboard.DownAsync($"{nameof(Key.Control)}");38 await page.Keyboard.PressAsync($"{nameof(Key.End)}");39 await page.Keyboard.UpAsync($"{nameof(Key.Control)}");40 await page.WaitForTimeoutAsync(300);41 postContent = await page.QuerySelectorAllAsync(postContentSelector);42 postCount = postContent.Length;43 cycleIndex++;44 }45 foreach (var item in postContent)46 {47 Report($"------------{i}------------------------------------------------");48 var text = (await (await item.GetPropertyAsync("innerText")).JsonValueAsync()).ToString();49 Report(text);50 result.Add(text);51 i++;52 }53 }...

Full Screen

Full Screen

PageExtensions.cs

Source:PageExtensions.cs Github

copy

Full Screen

...18 await page.WaitForSelectorAsync(selector);19 await page.FocusAsync(selector);20 await page.Keyboard.DownAsync("Control");21 await page.Keyboard.PressAsync("A");22 await page.Keyboard.UpAsync("Control");23 await page.Keyboard.PressAsync("Backspace");24 await page.Keyboard.TypeAsync(text);25 }26 public static async Task ClickOn(this Page page, string selector)27 {28 await page.WaitForSelectorAsync(selector);29 await page.ClickAsync(selector);30 }31 }32}...

Full Screen

Full Screen

UpAsync

Using AI Code Generation

copy

Full Screen

1await page.Keyboard.UpAsync("ControlLeft");2await page.Keyboard.DownAsync("ControlLeft");3await page.Keyboard.PressAsync("ControlLeft");4await page.Keyboard.SendCharacterAsync("ControlLeft");5await page.Keyboard.SendCharactersAsync("ControlLeft");6await page.Keyboard.SendKeyAsync("ControlLeft");7await page.Keyboard.TypeAsync("ControlLeft");8await page.Keyboard.UpAsync("ControlRight");9await page.Keyboard.DownAsync("ControlRight");10await page.Keyboard.PressAsync("ControlRight");11await page.Keyboard.SendCharacterAsync("ControlRight");12await page.Keyboard.SendCharactersAsync("ControlRight");13await page.Keyboard.SendKeyAsync("ControlRight");14await page.Keyboard.TypeAsync("ControlRight");15await page.Keyboard.UpAsync("MetaLeft");16await page.Keyboard.DownAsync("MetaLeft");

Full Screen

Full Screen

UpAsync

Using AI Code Generation

copy

Full Screen

1var keyboard = page.Keyboard;2await keyboard.UpAsync("ArrowUp");3var keyboard = page.Keyboard;4await keyboard.DownAsync("ArrowDown");5var keyboard = page.Keyboard;6await keyboard.SendCharacterAsync("ArrowUp");7var keyboard = page.Keyboard;8await keyboard.SendCharactersAsync("ArrowUp");9var keyboard = page.Keyboard;10await keyboard.SendEventAsync(new KeyboardEventArgs11{12});13var keyboard = page.Keyboard;14await keyboard.SendEventsAsync(new KeyboardEventArgs[] {15 {16 }17});18var keyboard = page.Keyboard;19await keyboard.TypeAsync("ArrowUp");20var keyboard = page.Keyboard;21await keyboard.PressAsync("ArrowUp");22var keyboard = page.Keyboard;23await keyboard.PressAsync("ArrowUp", new PressOptions24{25});26var keyboard = page.Keyboard;27await keyboard.PressAsync("ArrowUp", new PressOptions28{29});30var keyboard = page.Keyboard;31await keyboard.PressAsync("ArrowUp", new PressOptions32{33});

Full Screen

Full Screen

UpAsync

Using AI Code Generation

copy

Full Screen

1await page.Keyboard.UpAsync("Control");2await page.Keyboard.UpAsync("Control");3await page.Keyboard.UpAsync("Control");4await page.Keyboard.UpAsync("Control");5await page.Keyboard.UpAsync("Control");6await page.Keyboard.UpAsync("Control");7await page.Keyboard.UpAsync("Control");8await page.Keyboard.UpAsync("Control");9await page.Keyboard.UpAsync("Control");10await page.Keyboard.UpAsync("Control");11await page.Keyboard.UpAsync("Control");12await page.Keyboard.UpAsync("Control");13await page.Keyboard.UpAsync("Control");14await page.Keyboard.UpAsync("Control");15await page.Keyboard.UpAsync("Control");16await page.Keyboard.UpAsync("Control");17await page.Keyboard.UpAsync("Control");

Full Screen

Full Screen

UpAsync

Using AI Code Generation

copy

Full Screen

1var page = await browser.NewPageAsync();2var keyboard = page.Keyboard;3await keyboard.UpAsync("Control");4await page.ScreenshotAsync("screenshot.png");5var page = await browser.NewPageAsync();6var keyboard = page.Keyboard;7await keyboard.DownAsync("Control");8await keyboard.UpAsync("Control");9await page.ScreenshotAsync("screenshot.png");10var page = await browser.NewPageAsync();11var keyboard = page.Keyboard;12await keyboard.PressAsync("Control");13await keyboard.UpAsync("Control");14await page.ScreenshotAsync("screenshot.png");15var page = await browser.NewPageAsync();16var keyboard = page.Keyboard;17await keyboard.PressAsync("Control");18await keyboard.DownAsync("Control");19await keyboard.UpAsync("Control");20await page.ScreenshotAsync("screenshot.png");21var page = await browser.NewPageAsync();22var keyboard = page.Keyboard;23await keyboard.PressAsync("Control");24await keyboard.DownAsync("Control");25await keyboard.UpAsync("Control");26await keyboard.ReleaseAsync("Control");27await page.ScreenshotAsync("screenshot.png");28var page = await browser.NewPageAsync();29var keyboard = page.Keyboard;30await keyboard.PressAsync("Control");31await keyboard.DownAsync("Control");32await keyboard.ReleaseAsync("Control");33await keyboard.UpAsync("Control");34await page.ScreenshotAsync("screenshot.png");35var page = await browser.NewPageAsync();36var keyboard = page.Keyboard;

Full Screen

Full Screen

UpAsync

Using AI Code Generation

copy

Full Screen

1var page = await browser.NewPageAsync();2await page.Keyboard.UpAsync("Shift");3await page.Keyboard.DownAsync("Shift");4await page.Keyboard.TypeAsync("Hello World");5var page = await browser.NewPageAsync();6await page.Keyboard.UpAsync("Shift");7await page.Keyboard.DownAsync("Shift");8await page.Keyboard.TypeAsync("Hello World");9var page = await browser.NewPageAsync();10await page.Keyboard.UpAsync("Shift");11await page.Keyboard.DownAsync("Shift");12await page.Keyboard.TypeAsync("Hello World");13var page = await browser.NewPageAsync();14await page.Keyboard.UpAsync("Shift");15await page.Keyboard.DownAsync("Shift");16await page.Keyboard.TypeAsync("Hello World");17var page = await browser.NewPageAsync();18await page.Keyboard.UpAsync("Shift");19await page.Keyboard.DownAsync("Shift");20await page.Keyboard.TypeAsync("Hello World");21var page = await browser.NewPageAsync();22await page.Keyboard.UpAsync("Shift");23await page.Keyboard.DownAsync("Shift");24await page.Keyboard.TypeAsync("Hello World");25var page = await browser.NewPageAsync();26await page.Keyboard.UpAsync("Shift");27await page.Keyboard.DownAsync("Shift");28await page.Keyboard.TypeAsync("Hello World");29var page = await browser.NewPageAsync();

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