This article is part of the “Introduction to E2E Testing with Reqnroll × Playwright” series.
- Reqnroll Step Definitions in C#: How to Write and Use Them
- What You’ll Learn in This Guide
- What Are Step Definitions?
- Creating a Step Definition Class
- Basic Syntax of Step Definitions
- Matching Feature Files to Code
- Using Regular Expressions
- Passing Parameters
- Using Playwright in Step Definitions
- Best Practices for Writing Step Definitions
- Recommended Project Structure
- Summary
Reqnroll Step Definitions in C#: How to Write and Use Them
Conclusion
Step Definitions are the bridge between your Feature files and executable C# code in Reqnroll.
By mapping Given / When / Then steps to methods, you can turn readable BDD scenarios into fully automated tests—especially when combined with tools like Playwright.
What You’ll Learn in This Guide
In this tutorial, you’ll learn:
- What Step Definitions are
- Basic syntax in C#
- How Feature files map to Step Definitions
- How to pass parameters
- How to integrate with Playwright
This guide is ideal for developers using Reqnroll with C# and .NET.
What Are Step Definitions?
Step Definitions connect the steps written in a Feature file to actual C# code.
Example: Feature File
Given I open the login page
When I enter email "test@example.com"
Then I should see the dashboard
Corresponding Step Definitions
[Given(@"I open the login page")]
public async Task OpenLoginPage()
{
}
[When(@"I enter email ""(.*)""")]
public async Task EnterEmail(string email)
{
}
[Then(@"I should see the dashboard")]
public async Task DashboardDisplayed()
{
}
Each step in the Feature file is mapped to a C# method.
Creating a Step Definition Class
Step Definitions are organized into classes.
using Reqnroll;
[Binding]
public class LoginSteps
{
}
Important Attribute
[Binding]tells Reqnroll that this class contains Step Definitions
Basic Syntax of Step Definitions
Step Definitions use attributes to match Feature steps.
[Given(@"I open the login page")]
public async Task OpenLoginPage()
{
}
Available Attributes
| Attribute | Purpose |
|---|---|
| [Given] | Preconditions |
| [When] | User actions |
| [Then] | Expected results |
Matching Feature Files to Code
The text in your Feature file must match the Step Definition.
Feature File
Scenario: Successful login
Given I open the login page
When I click the login button
Then I should see the dashboard
Step Definitions
[Given(@"I open the login page")]
public async Task OpenLoginPage()
{
}
[When(@"I click the login button")]
public async Task ClickLoginButton()
{
}
[Then(@"I should see the dashboard")]
public async Task DashboardDisplayed()
{
}
If the text does not match, the step will not execute.
Using Regular Expressions
Reqnroll uses regular expressions to match steps.
Example
[When(@"I enter email ""(.*)""")]
public async Task EnterEmail(string email)
{
}
Feature File
When I enter email "test@example.com"
The value inside quotes is passed as a parameter.
Passing Parameters
Single Parameter
When I enter email "test@example.com"
[When(@"I enter email ""(.*)""")]
public async Task EnterEmail(string email)
{
Console.WriteLine(email);
}
Multiple Parameters
When I enter username "test" and password "1234"
[When(@"I enter username ""(.*)"" and password ""(.*)""")]
public async Task EnterLoginInfo(string username, string password)
{
}
Using Playwright in Step Definitions
You can call Playwright inside Step Definitions to automate browser actions.
[When(@"I enter email ""(.*)""")]
public async Task EnterEmail(string email)
{
await _page.Locator("#email").FillAsync(email);
}
[When(@"I click the login button")]
public async Task ClickLoginButton()
{
await _page.Locator("#login-button").ClickAsync();
}
This allows you to turn BDD steps into real UI interactions.
Best Practices for Writing Step Definitions
Keep Steps Small
Each Step Definition should handle one action:
- Enter email
- Enter password
- Click login
Match Text Exactly
Your Feature file and Step Definitions must match.
- Feature: “click the login button”
- Code: “click the login button”
Reuse Steps
You can reuse common steps across scenarios:
- Open login page
- Navigate to homepage
This improves maintainability.
Recommended Project Structure
A typical project structure looks like this:
Tests
├ Features
│ └ Login.feature
│
├ Steps
│ └ LoginSteps.cs
│
└ Hooks
└ PlaywrightHooks.cs
This keeps your test code organized and scalable.
Summary
Step Definitions are a core part of Reqnroll’s BDD approach.
Key takeaways:
- Step Definitions map Feature steps to C# methods
- Use
[Given],[When],[Then]attributes - Regular expressions enable parameter handling
- Can integrate with Playwright for UI automation
- Keep steps simple and reusable
By mastering Step Definitions, you can build clean, readable, and powerful E2E tests in .NET.
