Roadmap › Test Automation — Selenium, Playwright, API and BDD with C# › Module: UI Automation
Lessons in this course (10)
6. Playwright for .NET
Intermediate★ Must knowLesson 6 of 10
csharp
// dotnet add package Microsoft.Playwright.NUnit ; then: pwsh bin/Debug/net10.0/playwright.ps1 install
public class CheckoutTests : PageTest
{
[Test]
public async Task User_can_place_order()
{
await Page.GotoAsync("https://shop.example.com");
await Page.GetByRole(AriaRole.Link, new() { Name = "Laptops" }).ClickAsync();
await Page.GetByTestId("product-101").GetByRole(AriaRole.Button, new() { Name = "Add to cart" }).ClickAsync();
await Page.GetByRole(AriaRole.Button, new() { Name = "Checkout" }).ClickAsync();
await Expect(Page.GetByText("Order placed")).ToBeVisibleAsync(); // auto-retrying assertion
}
}| Selenium | Playwright | |
|---|---|---|
| Protocol | W3C WebDriver (BiDi coming) | Direct browser protocols (CDP / patched engines) |
| Waiting | Manual explicit waits | Auto-wait before actions + retrying assertions |
| Browsers | Chrome, Edge, Firefox, Safari, IE (legacy) | Chromium, Firefox, WebKit |
| Isolation | New driver per test (slow) | Browser contexts (fast, isolated) |
| Extras | Huge ecosystem, Grid, Appium for mobile | Tracing viewer, codegen, network mocking, API testing, video |
| Languages | Java, C#, Python, JS, Ruby | TS/JS, C#, Python, Java |
Useful features: playwright codegen (record locators), Page.RouteAsync (mock APIs), trace viewer (step-by-step DOM snapshots), StorageState (reuse login), parallel workers.