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): examples for input, intro, languages, multi-pages (#6596)

Pavel Feldmanbbc3ebd5127d
diff --git a/docs/src/input.md b/docs/src/input.md
index 1527c0ba7b..42ce14bf64 100644
--- a/docs/src/input.md
+++ b/docs/src/input.md
@@ -77,6 +77,23 @@ page.fill('#local', '2020-03-02T05:15')
page.fill('text=First Name', 'Peter')
```
+```csharp
+// Text input
+await page.FillAsync("#name", "Peter");
+
+// Date input
+await page.FillAsync("#date", "2020-02-02");
+
+// Time input
+await page.FillAsync("#time", "13-15");
+
+// Local datetime input
+await page.FillAsync("#local", "2020-03-02T05:15");
+
+// Input through label
+await page.FillAsync("text=First Name", "Peter");
+```
+
### API reference
- [`method: Page.fill`]
@@ -145,6 +162,20 @@ page.uncheck('#subscribe-label')
page.check('text=XL')
```
+```csharp
+// Check the checkbox
+await page.CheckAsync("#agree");
+
+// Assert the checked state
+Assert.True(await page.IsCheckedAsync("#agree"));
+
+// Uncheck by input <label>.
+await page.UncheckAsync("#subscribe-label");
+
+// Select the radio button
+await page.CheckAsync("text=XL");
+```
+
### API reference
- [`method: Page.check`]
@@ -221,6 +252,21 @@ option = page.query_selector('#best-option')
page.select_option('select#colors', option)
```
+```csharp
+// Single selection matching the value
+await page.SelectOptionAsync("select#colors", "blue");
+
+// Single selection matching the label
+await page.SelectOptionAsync("select#colors", new SelectOptionValue { Label = "blue" }));
+
+// Multiple selected items
+await page.SelectOptionAsync("select#colors", new[] { "blue", "green", "red" });
+
+// Select the option via element handle
+var option = await page.QuerySelectorAsync("#best-option");
+await page.SelectOptionAsync("select#colors", option);
+```
+
### API reference
- [`method: Page.selectOption`]
@@ -313,6 +359,26 @@ page.hover('#item')
page.click('#item', position={ 'x': 0, 'y': 0})
```
+```csharp
+// Generic click
+await page.ClickAsync("button#submit");
+
+// Double click
+await page.DblClickAsync("#item");
+
+// Right click
+await page.ClickAsync("#item", button: MouseButton.Right);
+
+// Shift + click
+await page.ClickAsync("#item", modifiers: new[] { KeyboardModifier.Shift });
+
+// Hover over element
+await page.HoverAsync("#item");
+
+// Click the top left corner
+await page.ClickAsync("#item", position: new Position { X = 0, Y = 0 });
+```
+
Under the hood, this and other pointer-related methods:
- wait for element with given selector to be in DOM
@@ -342,6 +408,10 @@ await page.click('button#submit', force=True)
page.click('button#submit', force=True)
```
+```csharp
+await page.ClickAsync("button#submit", force: true);
+```
+
#### Programmatic click
If you are not interested in testing your app under the real conditions and want to simulate the click by any means possible, you can trigger the [`HTMLElement.click()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click) behavior via simply dispatching a click event on the element:
@@ -362,6 +432,10 @@ await page.dispatch_event('button#submit', 'click')
page.dispatch_event('button#submit', 'click')
```
+```csharp
+await page.DispatchEventAsync("button#submit", "click");
+```
+
### API reference
- [`method: Page.click`]
@@ -403,6 +477,11 @@ await page.type('#area', 'Hello World!')
page.type('#area', 'Hello World!')
```
+```csharp
+// Type character by character
+await page.TypeAsync("#area", "Hello World!");
+```
+
This method will emit all the necessary keyboard events, with all the `keydown`, `keyup`, `keypress` events in place. You can even specify the optional `delay` between the key presses to simulate real user behavior.
:::note
@@ -464,6 +543,17 @@ page.press('#name', 'Control+ArrowRight')
page.press('#value', '$')
```
+```csharp
+// Hit Enter
+await page.PressAsync("#submit", "Enter");
+
+// Dispatch Control+Right
+await page.PressAsync("#name", "Control+ArrowRight");
+
+// Press $ sign on keyboard
+await page.PressAsync("#value", "$");
+```
+
This method focuses the selected element and produces a single keystroke. It accepts the logical key names that are emitted in the [keyboardEvent.key](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key) property of the keyboard events:
```
@@ -511,6 +601,14 @@ page.press('#name', 'Shift+A')
page.press('#name', 'Shift+ArrowLeft')
```
+```csharp
+// <input id=name>
+await page.PressAsync("#name", "Shift+A");
+
+// <input id=name>
+await page.PressAsync("#name", "Shift+ArrowLeft");
+```
+
Shortcuts such as `"Control+o"` or `"Control+Shift+T"` are supported as well. When specified with the modifier, modifier is pressed and being held while the subsequent key is being pressed.
Note that you still need to specify the capital `A` in `Shift-A` to produce the capital character. `Shift-a` produces a lower-case one as if you had the `CapsLock` toggled.
@@ -600,6 +698,25 @@ page.set_input_files(
)
```
+```csharp
+// Select one file
+await page.SetInputFilesAsync("input#upload", "myfile.pdf");
+
+// Select multiple files
+await page.SetInputFilesAsync("input#upload", new[] { "file1.txt", "file12.txt" });
+
+// Remove all the selected files
+await page.SetInputFilesAsync("input#upload", new[] {});
+
+// Upload buffer from memory
+await page.SetInputFilesAsync("input#upload", new FilePayload
+{
+ Name = "file.txt",
+ MimeType = "text/plain",
+ Buffer = "this is a test".getBytes(StandardCharsets.UTF_8),
+});
+```
+
If you don't have input element in hand (it is created dynamically), you can handle the [`event: Page.fileChooser`] event
or use a corresponding waiting method upon your action:
@@ -632,6 +749,14 @@ file_chooser = fc_info.value
file_chooser.set_files("myfile.pdf")
```
+```csharp
+var fileChooser = await Task.WhenAll(
+ page.WaitForFileChooserAsync(),
+ page.ClickAsync("upload")
+);
+await fileChooser.SetFilesAsync("myfile.pdf");
+```
+
### API reference
- [FileChooser]
- [`method: Page.setInputFiles`]
@@ -660,6 +785,10 @@ await page.focus('input#name')
page.focus('input#name')
```
+```csharp
+await page.FocusAsync("input#name");
+```
+
### API reference
- [`method: Page.focus`]
diff --git a/docs/src/intro-csharp.md b/docs/src/intro-csharp.md
index 5eb337a43d..0bc23463d1 100644
--- a/docs/src/intro-csharp.md
+++ b/docs/src/intro-csharp.md
@@ -8,20 +8,27 @@ title: "Getting Started"
## Installation
-Install PlaywrightSharp package from NuGet in Visual Studio or from the CLI in your project root directory:
+Install Microsoft.Playwright package from NuGet in Visual Studio or from the CLI in your project root directory:
```sh
-dotnet add package PlaywrightSharp
+dotnet add package Microsoft.Playwright
```
## Usage
```csharp
-using var playwright = await Playwright.CreateAsync();
-await using var browser = await playwright.Chromium.LaunchAsync();
-var page = await browser.NewPageAsync();
-await page.GotoAsync("http://www.bing.com");
-await page.ScreenshotAsync(path: outputFile);
+using Microsoft.Playwright;
+using System.Threading.Tasks;
+
+class Example
+{
+ public static async Task Main()
+ {
+ using var playwright = await Playwright.CreateAsync();
+ await using var browser = await playwright.Chromium.LaunchAsync();
+ // Create pages, interact with UI elements, assert values
+ }
+}
```
## First script
@@ -29,13 +36,26 @@ await page.ScreenshotAsync(path: outputFile);
In our first script, we will navigate to `whatsmyuseragent.org` and take a screenshot in WebKit.
```csharp
-// FIXME:
+using Microsoft.Playwright;
+using System.Threading.Tasks;
+
+class Example
+{
+ public static async Task Main()
+ {
+ using var playwright = await Playwright.CreateAsync();
+ await using var browser = await playwright.Chromium.LaunchAsync();
+ var page = await browser.NewPageAsync();
+ await page.GotoAsync("whatsmyuseragent.org");
+ await page.ScreenshotAsync(path: "screenshot.png");
+ }
+}
```
-By default, Playwright runs the browsers in headless mode. To see the browser UI, pass the `headless=False` flag while launching the browser. You can also use [`option: slowMo`] to slow down execution. Learn more in the debugging tools [section](./debug.md).
+By default, Playwright runs the browsers in headless mode. To see the browser UI, pass the `headless: false` flag while launching the browser. You can also use [`option: slowMo`] to slow down execution. Learn more in the debugging tools [section](./debug.md).
```csharp
-// FIXME:
+await playwright.Firefox.LaunchAsync(headless: false, slowMo: 50);
```
## Record scripts
diff --git a/docs/src/languages.md b/docs/src/languages.md
index 61feb17d90..32dfa6db4c 100644
--- a/docs/src/languages.md
+++ b/docs/src/languages.md
@@ -36,11 +36,13 @@ The Playwright API is available in multiple languages.
## C#
-Playwright for C# is available in preview.
+[Playwright for .NET](https://playwright.dev/dotnet/docs/intro/) is available.
+
+* [Documentation](https://playwright.dev/dotnet/docs/intro/)
+* [API](https://playwright.dev/dotnet/docs/api/class-playwright)
+* [GitHub repo](https://github.com/microsoft/playwright-sharp)
+* [Playwright on NuGet](https://www.nuget.org/packages/Microsoft.Playwright)
```
-dotnet add package PlaywrightSharp
+dotnet add package Microsoft.Playwright
```
-
-* [Playwright on NuGet](https://www.nuget.org/packages/PlaywrightSharp/)
-* [GitHub repo](https://github.com/microsoft/playwright-sharp)
diff --git a/docs/src/multi-pages.md b/docs/src/multi-pages.md
index 6bb2287451..b10c9f396a 100644
--- a/docs/src/multi-pages.md
+++ b/docs/src/multi-pages.md
@@ -23,13 +23,10 @@ const browser = await chromium.launch();
const userContext = await browser.newContext();
const adminContext = await browser.newContext();
-// Load user and admin cookies
-await userContext.addCookies(userCookies);
-await adminContext.addCookies(adminCookies);
+// Create pages and interact with contexts independently
```
```java
-// FIXME
import com.microsoft.playwright.*;
public class Example {
@@ -41,9 +38,7 @@ public class Example {
// Create two isolated browser contexts
BrowserContext userContext = browser.newContext();
BrowserContext adminContext = browser.newContext();
- // Load user and admin cookies
- userContext.addCookies(userCookies);
- adminContext.addCookies(adminCookies);
+ // Create pages and interact with contexts independently
}
}
}
@@ -62,9 +57,7 @@ async def run(playwright):
user_context = await browser.new_context()
admin_context = await browser.new_context()
- # load user and admin cookies
- await user_context.add_cookies(user_cookies)
- await admin_context.add_cookies(admin_cookies)
+ # create pages and interact with contexts independently
async def main():
async with async_playwright() as playwright:
@@ -84,14 +77,30 @@ def run(playwright):
user_context = browser.new_context()
admin_context = browser.new_context()
- # load user and admin cookies
- user_context.add_cookies(user_cookies)
- admin_context.add_cookies(admin_cookies)
+ # create pages and interact with contexts independently
with sync_playwright() as playwright:
run(playwright)
```
+```csharp
+using Microsoft.Playwright;
+using System.Threading.Tasks;
+
+class Example
+{
+ public static async Task Main()
+ {
+ using var playwright = await Playwright.CreateAsync();
+ // Create a Chromium browser instance
+ await using var browser = await playwright.Chromium.LaunchAsync();
+ await using var userContext = await browser.NewContextAsync();
+ await using var adminContext = await browser.NewContextAsync();
+ // Create pages and interact with contexts independently.
+ }
+}
+```
+
### API reference
- [BrowserContext]
- [`method: Browser.newContext`]
@@ -140,6 +149,15 @@ page_two = context.new_page()
all_pages = context.pages()
```
+```csharp
+// Create two pages
+var pageOne = await context.NewPageAsync();
+var pageTwo = await context.NewPageAsync();
+
+// Get pages of a brower context
+var allPages = context.Pages;
+```
+
### API reference
- [Page]
- [`method: BrowserContext.newPage`]
@@ -189,6 +207,16 @@ new_page.wait_for_load_state()
print(new_page.title())
```
+```csharp
+// Get page after a specific action (e.g. clicking a link)
+var newPage = await Task.WhenAll(
+ context.WaitForPageAsync(),
+ page.ClickAsync("a[target='_blank']")
+);
+await newPage.WaitForLoadStateAsync();
+Console.WriteLine(await newPage.TitleAsync());
+```
+
If the action that triggers the new page is unknown, the following pattern can be used.
```js
@@ -225,6 +253,14 @@ def handle_page(page):
context.on("page", handle_page)
```
+```csharp
+// Get all new pages (including popups) in the context
+context.Page += async (_, page) => {
+ await page.WaitForLoadStateAsync();
+ Console.WriteLine(await page.TitleAsync());
+};
+```
+
### API reference
- [`event: BrowserContext.page`]
@@ -273,6 +309,16 @@ popup.wait_for_load_state()
print(popup.title())
```
+```csharp
+// Get popup after a specific action (e.g., click)
+var newPage = await Task.WhenAll(
+ page.WaitForPopupAsync(),
+ page.ClickAsync("#open")
+);
+await newPage.WaitForLoadStateAsync();
+Console.WriteLine(await newPage.TitleAsync());
+```
+
If the action that triggers the popup is unknown, the following pattern can be used.
```js
@@ -309,5 +355,13 @@ def handle_popup(popup):
page.on("popup", handle_popup)
```
+```csharp
+// Get all popups when they open
+page.Popup += async (_, popup) => {
+ await popup.WaitForLoadStateAsync();
+ Console.WriteLine(await page.TitleAsync());
+};
+```
+
### API reference
- [`event: Page.popup`]
\ No newline at end of file
diff --git a/docs/src/navigations.md b/docs/src/navigations.md
index 86d9f77ccd..f296d4c4a1 100644
--- a/docs/src/navigations.md
+++ b/docs/src/navigations.md
@@ -344,7 +344,7 @@ with page.expect_navigation():
```csharp
// Using waitForNavigation with a callback prevents a race condition
// between clicking and waiting for a navigation.
-await TaskUtils.WhenAll(
+await Task.WhenAll(
page.WaitForNavigationAsync(), // Waits for the next navigation
page.ClickAsync("div.delayed-navigation"); // Triggers a navigation after a timeout
);
@@ -393,7 +393,7 @@ with page.expect_navigation(url="**/login"):
```csharp
// Running action in the callback of waitForNavigation prevents a race
// condition between clicking and waiting for a navigation.
-await TaskUtils.WhenAll(
+await Task.WhenAll(
page.WaitForNavigationAsync("**/login"), // Waits for the next navigation
page.ClickAsync("a") // Triggers a navigation with a script redirect
);
@@ -434,7 +434,7 @@ popup.wait_for_load_state("load")
```
```csharp
-var popup = await TaskUtils.WhenAll(
+var popup = await Task.WhenAll(
page.WaitForPopupAsync(),
page.ClickAsync("a[target='_blank']") // Opens popup
);
diff --git a/docs/src/network.md b/docs/src/network.md
index 1eeddab2aa..c721b8d5eb 100644
--- a/docs/src/network.md
+++ b/docs/src/network.md
@@ -281,7 +281,7 @@ response = response_info.value
```csharp
// Use a glob URL pattern
-var response = await TaskUtils.WhenAll(
+var response = await Task.WhenAll(
page.WaitForResponseAsync("**/api/fetch_data"),
page.ClickAsync("button#update")
);
@@ -341,13 +341,13 @@ response = response_info.value
```csharp
// Use a regular expression
-var response = await TaskUtils.WhenAll(
+var response = await Task.WhenAll(
page.WaitForResponseAsync(new Regex("\\.jpeg$")),
page.ClickAsync("button#update")
);
// Use a predicate taking a Response object
-var response = await TaskUtils.WhenAll(
+var response = await Task.WhenAll(
page.WaitForResponseAsync(r => r.Url.Contains(token)),
page.ClickAsync("button#update")
);