Skip to content

Latest commit

 

History

History
72 lines (52 loc) · 3.18 KB

Playwright.md

File metadata and controls

72 lines (52 loc) · 3.18 KB

Playwright

Playwright is a modern E2E testing library that allows you to automate browser interactions and test web applications across multiple browsers (Chromium, Firefox, and WebKit). Playwright for .NET brings the power of Playwright to the .NET ecosystem, enabling you to write E2E tests in C#.

To start using Playwright for .NET, install the Microsoft.Playwright NuGet package.

Here's an example of using Playwright for .NET to create an E2E test for a hypothetical e-commerce application:

  • The application has a homepage with a list of products and a search functionality.
  • Users can search for products by entering a keyword and clicking the search button.
  • The application displays the search results and allows users to view product details.

First, create a new .NET test project and install the Microsoft.Playwright NuGet package.

Next, write the E2E test for the search functionality using Playwright and xUnit:

using System.Threading.Tasks;
using Microsoft.Playwright;
using Xunit;

public class ECommerceTests
{
    [Fact]
    public async Task SearchFunctionalityTest()
    {
        // Initialize a new Playwright instance
        using var playwright = await Playwright.CreateAsync();

        // Launch a new browser
        await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
        {
            Headless = true
        });

        // Create a new browser context
        await using var context = await browser.NewContextAsync();

        // Open a new page
        var page = await context.NewPageAsync();

        // Navigate to the e-commerce application's homepage
        await page.GotoAsync("https://example.com");

        // Enter a search keyword
        await page.FillAsync("#search-input", "gaming laptop");

        // Click the search button
        await page.ClickAsync("#search-button");

        // Wait for the search results to load
        await page.WaitForSelectorAsync("#search-results");

        // Check that the search results contain the expected product
        var firstResultTitle = await page.GetTextContentAsync("#search-results .result-title");
        Assert.Contains("Gaming Laptop", firstResultTitle);
    }
}

This E2E test performs the following actions:

  1. Initializes a new Playwright instance and launches a browser (Chromium in this example).
  2. Creates a new browser context and opens a new page.
  3. Navigates to the e-commerce application's homepage.
  4. Enters a search keyword and clicks the search button.
  5. Waits for the search results to load.
  6. Checks that the search results contain the expected product.

By automating user interactions with the e-commerce application, the test verifies that the search functionality works correctly and delivers the expected results.

In this example, Playwright for .NET allows you to create E2E tests that simulate real-world scenarios, ensuring that your application functions correctly across different browsers. Keep in mind that this is a basic example; Playwright offers many advanced features, such as network interception, handling file uploads/downloads, and testing responsive designs, which can help you create more comprehensive E2E tests for your application.