Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

docs(dotnet): Keyboard examples (#6539)

Anže Vodovnikfce904fa4c16
diff --git a/docs/src/api/class-keyboard.md b/docs/src/api/class-keyboard.md
index c830f98457..ab2e6e6c03 100644
--- a/docs/src/api/class-keyboard.md
+++ b/docs/src/api/class-keyboard.md
@@ -54,6 +54,20 @@ page.keyboard.press("Backspace")
# result text will end up saying "Hello!"
```
+```csharp
+await page.Keyboard.TypeAsync("Hello World!");
+await page.Keyboard.PressAsync("ArrowLeft");
+
+await page.Keyboard.DownAsync("Shift");
+for (int i = 0; i < " World".Length; i++)
+ await page.Keyboard.PressAsync("ArrowLeft");
+
+await page.Keyboard.UpAsync("Shift");
+
+await page.Keyboard.PressAsync("Backspace");
+// Result text will end up saying "Hello!"
+```
+
An example of pressing uppercase `A`
```js
@@ -80,6 +94,12 @@ page.keyboard.press("Shift+KeyA")
page.keyboard.press("Shift+A")
```
+```csharp
+await page.Keyboard.PressAsync("Shift+KeyA");
+// or
+await page.Keyboard.PressAsync("Shift+A");
+```
+
An example to trigger select-all with the keyboard
```js
@@ -110,6 +130,13 @@ page.keyboard.press("Control+A")
page.keyboard.press("Meta+A")
```
+```csharp
+// on Windows and Linux
+await page.Keyboard.PressAsync("Control+A");
+// on macOS
+await page.Keyboard.PressAsync("Meta+A");
+```
+
## async method: Keyboard.down
Dispatches a `keydown` event.
@@ -165,6 +192,10 @@ await page.keyboard.insert_text("嗨")
page.keyboard.insert_text("嗨")
```
+```csharp
+await page.Keyboard.PressAsync("嗨");
+```
+
:::note
Modifier keys DO NOT effect `keyboard.insertText`. Holding down `Shift` will not type the text in upper case.
:::
@@ -242,6 +273,17 @@ page.screenshot(path="o.png")
browser.close()
```
+```csharp
+await page.GoToAsync("https://keycode.info");
+await page.Keyboard.PressAsync("A");
+await page.ScreenshotAsync("A.png");
+await page.Keyboard.PressAsync("ArrowLeft");
+await page.ScreenshotAsync("ArrowLeft.png");
+await page.Keyboard.PressAsync("Shift+O");
+await page.ScreenshotAsync("O.png");
+await browser.CloseAsync();
+```
+
Shortcut for [`method: Keyboard.down`] and [`method: Keyboard.up`].
### param: Keyboard.press.key
@@ -282,6 +324,11 @@ page.keyboard.type("Hello") # types instantly
page.keyboard.type("World", delay=100) # types slower, like a user
```
+```csharp
+await page.Keyboard.TypeAsync("Hello"); // types instantly
+await page.Keyboard.TypeAsync("World"); // types slower, like a user
+```
+
:::note
Modifier keys DO NOT effect `keyboard.type`. Holding down `Shift` will not type the text in upper case.
:::