BddDotNet based autotest framework with Playwright integration
Key features:
- Gherkin syntax support
- C# expressions support for Gherkin syntax
- Component framework for UI testing
- Page object pattern support
- Playwright integration
- Test data management framework
Feature: CheckoutForm
Scenario: checkout form validation test
    Given navigate to '@Data.Common.HomePageUrl'
    When set following values:
    | Name                  | Value      |
    | checkout > first name | first_name |
    | checkout > last name  | last_name  |
    And click on 'checkout > continue to checkout'
    Then should be visible:
    | Name                              |
    | checkout > username error message |
    And should have following values:
    | Name                              | Value                      |
    | checkout > username error message | Your username is required. |- .NET 8+
- Visual Studio 2022 or Visual Studio Code
- Reqnroll plugin for Visual Studio 2022 or Cucumber plugin for Visual Studio Code
- https://www.nuget.org/packages/AutoTests.Framework
- https://www.nuget.org/packages/AutoTests.Framework.Playwright
You can also find example in Bootstrap.Tests project for UI testing
Guide:
- Create new console application for .NET 9
- Install nuget packages:
| Description | Package | 
|---|---|
| Add AutoTests.Framework with Playwright integration | |
| Add Gherkin language support | |
| Official Microsoft Playwright package | 
- Configure application in Program.cs. Example:
using AutoTests.Framework;
using AutoTests.Framework.Playwright;
using BddDotNet;
using Bootstrap.Tests.Pages;
using Microsoft.Testing.Platform.Builder;
var builder = await TestApplication.CreateBuilderAsync(args);
var services = builder.AddBddDotNet();
services.SinglePageChromiumPlaywright(new() { Headless = false });
services.Page<BootstrapApplication>();
services.SourceGeneratedGherkinScenarios();
services.SourceGeneratedGherkinSteps();
using var testApp = await builder.BuildAsync();
return await testApp.RunAsync();- Add page objects. Example:
using AutoTests.Framework.Pages;
namespace Bootstrap.Tests.Pages;
internal sealed class BootstrapApplication
{
    [Route("checkout")]
    public required Checkout Checkout { get; init; }
}
internal sealed class Checkout
{
    [Route("continue to checkout")]
    [Options(".btn-primary")]
    public required Button ContinueToCheckout { get; init; }
    [Route("first name")]
    [Options("#firstName")]
    public required Input FirstName { get; init; }
    [Route("last name")]
    [Options("#lastName")]
    public required Input LastName { get; init; }
    [Route("username error message")]
    [Options("#username ~ .invalid-feedback")]
    public required Label UsernameErrorMessage { get; init; }
}- Done. You can add gherkin feature files with test scenarios. Example CheckoutForm.feature:
Feature: CheckoutForm
Scenario: checkout form validation test
    Given navigate to 'https://getbootstrap.com/docs/4.3/examples/checkout/'
    When set following values:
    | Name                  | Value      |
    | checkout > first name | first_name |
    | checkout > last name  | last_name  |
    And click on 'checkout > continue to checkout'
    Then should be visible:
    | Name                              |
    | checkout > username error message |
    And should have following values:
    | Name                              | Value                      |
    | checkout > username error message | Your username is required. |- Install BddDotNet.Gherkin.CSharpExpressionsnuget package
- Add service for avaiable C# expressions
using AutoTests.Framework.Resources;
namespace Bootstrap.Tests;
public sealed class CSharpExpressions(IDynamicDataService dynamicDataService)
{
    public dynamic Data { get; } = dynamicDataService.Data;
}- Configure BddDotNet.Gherkin.CSharpExpressionsinProgram.cs
services.CSharpExpressions<CSharpExpressions>(ScriptOptions.Default.AddReferences("Microsoft.CSharp"));
services.DynamicResourcesData([Assembly.GetExecutingAssembly()]);- Now You can create json file in Datasubfolder and get access to content in feature to it like:
Feature: CheckoutForm
Scenario: checkout form validation test
    Given navigate to '@Data.Common.HomePageUrl' #return value HomePageUrl from Data\Common.jsonYou can find example in 'Bootstrap.Tests'
You can create custom components with any custom logic. Example of button component:
using AutoTests.Framework.Contracts;
using AutoTests.Framework.Options;
using AutoTests.Framework.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Playwright;
namespace AutoTests.Framework.Playwright;
public sealed class Button([ServiceKey] string path, IOptionsService optionsService, IPage page) : IComponent, IClick
{
    private readonly string locator = optionsService.GetOptions<string>(path);
    public async Task ClickAsync()
    {
        await page.ClickAsync(locator);
    }
}Example of step with components:
using AutoTests.Framework.Routing;
namespace Demo;
internal sealed class Steps(IRoutingService routingService)
{
    [When("click on '(.*)'")]
    public async Task ClickStep(string path)
    {
        await routingService.GetComponent<IClick>(path).ClickAsync();
    }
}Components:
- [required] Should implement IComponent interface
- [required] Should implement contract interfaces like IClick, IVisible, e.t.c
- [optional] Could reqeust options from IOptionsServiceusing[ServiceKey] string pathas a path for current component. Use can use it for example for locators
- [optional Could inject any other custom services. In this example IPageis a Playwright service for browser control.
You can find example in 'AutoTests.Framework.Playwright' for default components like button, input, e.t.c