Getting Started

Get up and running with the Playwright C# Agentic Framework in minutes. This guide covers installation, configuration, and writing your first test.

Prerequisites

Before you begin, ensure you have:

  • .NET SDK 9.0+ – Download from dotnet.microsoft.com
  • Playwright browsers – Installed automatically during setup
  • Allure CLI (optional, for reporting) – brew install allure on macOS

Verify your .NET installation:

dotnet --version
# Should output 9.0 or higher

Installation

1. Clone and Restore

Clone the repository from GitHub:

git clone https://github.com/kamrankhan54/playwright-csharp-agentic-framework.git cd playwright-csharp-agentic-framework dotnet restore

2. Build the Solution

dotnet build

This builds all projects:

  • Framework/ – Core test framework
  • Agents/ – Healing agents console app
  • Tests.E2E/ – SpecFlow test project

3. Install Playwright Browsers

Install browsers for the test project:

# For Tests.E2E dotnet build ./Tests.E2E/Tests.E2E.csproj bash ./Tests.E2E/bin/Debug/net9.0/playwright.sh install --with-deps chromium

Install browsers for the Agents project:

# For Agents dotnet build ./Agents/Agents.csproj bash ./Agents/bin/Debug/net9.0/playwright.sh install chromium

Configuration

App Settings

Configure your test environment in Tests.E2E/Support/appsettings.json:

{
  "BaseUrl": "https://www.saucedemo.com/",
  "DefaultUserEmail": "standard_user",
  "DefaultUserPassword": "secret_sauce",
  "Headless": true
}

Environment-Specific Settings

Create environment-specific configuration files:

appsettings.qa.json:

{
  "BaseUrl": "https://qa.example.com/",
  "DefaultUserEmail": "qa_user",
  "DefaultUserPassword": "qa_password",
  "Headless": true
}

appsettings.staging.json:

{
  "BaseUrl": "https://staging.example.com/",
  "DefaultUserEmail": "staging_user",
  "DefaultUserPassword": "staging_password",
  "Headless": true
}

Run tests with a specific environment:

ENVIRONMENT=qa dotnet test ./Tests.E2E/Tests.E2E.csproj

Timeouts

Default timeouts are set to ~5 seconds in LifecycleHooks. Override via environment variable:

PW_TIMEOUT_MS=3000 dotnet test ./Tests.E2E/Tests.E2E.csproj

Your First Test

1. Create a Feature File

Create Tests.E2E/Features/Login.feature:

Feature: User Login
    As a user
    I want to log in to the application
    So that I can access my account

    Scenario: Successful login with valid credentials
        Given I navigate to the login page
        When I enter email "standard_user" and password "secret_sauce"
        And I click the login button
        Then I should be redirected to the dashboard

2. Implement Step Definitions

Create Tests.E2E/Steps/LoginSteps.cs:

using Framework.Pages;
using Framework.Support;
using TechTalk.SpecFlow;

namespace Tests.E2E.Steps;

[Binding]
public class LoginSteps
{
    private readonly LoginPage _loginPage;
    private readonly ScenarioContext _scenarioContext;

    public LoginSteps(LoginPage loginPage, ScenarioContext scenarioContext)
    {
        _loginPage = loginPage;
        _scenarioContext = scenarioContext;
    }

    [Given(@"I navigate to the login page")]
    public void GivenINavigateToTheLoginPage()
    {
        _loginPage.Navigate();
    }

    [When(@"I enter email ""(.*)"" and password ""(.*)""")]
    public void WhenIEnterEmailAndPassword(string email, string password)
    {
        _loginPage.EnterEmail(email);
        _loginPage.EnterPassword(password);
    }

    [When(@"I click the login button")]
    public void WhenIClickTheLoginButton()
    {
        _loginPage.ClickLogin();
    }

    [Then(@"I should be redirected to the dashboard")]
    public void ThenIShouldBeRedirectedToTheDashboard()
    {
        _loginPage.WaitForDashboard();
        Assert.True(_loginPage.IsOnDashboard());
    }
}

3. Create a Page Object

Create Framework/Pages/LoginPage.cs:

using Framework.Support;
using Microsoft.Playwright;

namespace Framework.Pages;

public class LoginPage : BasePage
{
    public LoginPage(IPage page, Configuration config) : base(page, config)
    {
    }

    private ILocator EmailInput => Page.Locator("#user-name");
    private ILocator PasswordInput => Page.Locator("#password");
    private ILocator LoginButton => Page.Locator("#login-button");
    private ILocator Dashboard => Page.Locator(".inventory_container");

    public async Task Navigate()
    {
        await Page.GotoAsync(Config.BaseUrl);
    }

    public async Task EnterEmail(string email)
    {
        await EmailInput.FillAsync(email);
    }

    public async Task EnterPassword(string password)
    {
        await PasswordInput.FillAsync(password);
    }

    public async Task ClickLogin()
    {
        await LoginButton.ClickAsync();
    }

    public async Task WaitForDashboard()
    {
        await Dashboard.WaitForAsync(new LocatorWaitForOptions { State = WaitForSelectorState.Visible });
    }

    public bool IsOnDashboard()
    {
        return Dashboard.IsVisibleAsync().Result;
    }
}

4. Run Your Test

dotnet test ./Tests.E2E/Tests.E2E.csproj --filter "FullyQualifiedName~Login"

Learning Selectors (First Time Setup)

Before using the healing feature, you need to build the selector registry. Run your tests on a green suite:

# Ensure all tests pass first dotnet test ./Tests.E2E/Tests.E2E.csproj # Learn and record selectors dotnet run --project ./Agents/Agents.csproj -- learn \ --test ./Tests.E2E/Tests.E2E.csproj \ --out ./agents/selectors.registry.json

This creates agents/selectors.registry.json with multiple locator strategies for each element, which the Healer uses for auto-repair.

Next Steps