This article is part of the “Introduction to E2E Testing with Reqnroll × Playwright” series.
Reqnroll Hooks in .NET: Run Code Before and After Scenarios
Conclusion
Reqnroll Hooks let you run setup and cleanup code automatically at specific points in your tests.
By using Hooks, you can centralize common logic like browser setup, login, and teardown, making your E2E tests cleaner and easier to maintain.
What You’ll Learn in This Guide
In this tutorial, you’ll learn:
- What Hooks are in Reqnroll
- When Hooks are executed
- How to create and use Hooks in C#
- Common use cases in E2E testing
- How to integrate Hooks with Playwright
This guide is ideal for developers building BDD-based automated tests in .NET.
What Are Hooks in Reqnroll?
Hooks are methods that run automatically at specific points during test execution.
They allow you to execute logic at key moments such as:
- Before all tests start
- Before each scenario
- After each scenario
- After all tests complete
Hook Execution Timing
| Hook | When It Runs |
|---|---|
| BeforeTestRun | Before all tests |
| AfterTestRun | After all tests |
| BeforeFeature | Before each Feature |
| AfterFeature | After each Feature |
| BeforeScenario | Before each Scenario |
| AfterScenario | After each Scenario |
| BeforeStep | Before each Step |
| AfterStep | After each Step |
Using Hooks, you can manage shared setup and teardown logic across all tests.
Creating a Hooks Class
Hooks are defined in a dedicated class.
using Reqnroll;
[Binding]
public class Hooks
{
[BeforeScenario]
public void BeforeScenario()
{
Console.WriteLine("Scenario started");
}
[AfterScenario]
public void AfterScenario()
{
Console.WriteLine("Scenario finished");
}
}
Key Points
- Add the
[Binding]attribute - Decorate methods with Hook attributes
- Class name can be anything
Reqnroll automatically detects and executes these methods.
BeforeScenario Hook
BeforeScenario runs before each scenario starts.
[BeforeScenario]
public void Setup()
{
Console.WriteLine("Preparing test");
}
Common Use Cases
- Launch browser
- Initialize test data
- Perform login
In E2E testing, this is often used to set up the test environment.
AfterScenario Hook
AfterScenario runs after each scenario finishes.
[AfterScenario]
public void TearDown()
{
Console.WriteLine("Cleaning up");
}
Common Use Cases
- Close browser
- Take screenshots
- Clean test data
Proper teardown is essential for stable and repeatable tests.
BeforeStep and AfterStep Hooks
Hooks can also run before and after each step.
[BeforeStep]
public void BeforeStep()
{
Console.WriteLine("Step started");
}
[AfterStep]
public void AfterStep()
{
Console.WriteLine("Step finished");
}
Use Cases
- Logging step execution
- Capturing screenshots on failure
Be cautious: these run very frequently and may impact performance.
Feature-Level Hooks
You can also define Hooks at the Feature level.
[BeforeFeature]
public static void BeforeFeature()
{
Console.WriteLine("Feature started");
}
[AfterFeature]
public static void AfterFeature()
{
Console.WriteLine("Feature finished");
}
Important Note
- Must be defined as
staticmethods
Use Cases
- Database setup
- Test environment initialization
Benefits of Using Hooks
Centralized Common Logic
You can avoid duplicating setup code across Step Definitions.
Examples:
- Browser initialization
- Login process
- Database setup
Cleaner Test Code
Your Step Definitions can focus only on test behavior:
Scenario: Successful login
Given I open the login page
When I log in
Then I should see the dashboard
Environment setup is handled by Hooks.
Better Maintainability
If you need to change setup logic (e.g., browser configuration), you only update it in one place.
Using Hooks with Playwright
Hooks are commonly used to manage Playwright browser instances.
Example
using Microsoft.Playwright;
using Reqnroll;
[Binding]
public class PlaywrightHooks
{
public static IPage Page;
private static IBrowser browser;
private static IPlaywright playwright;
[BeforeScenario]
public async Task BeforeScenario()
{
playwright = await Playwright.CreateAsync();
browser = await playwright.Chromium.LaunchAsync();
var context = await browser.NewContextAsync();
Page = await context.NewPageAsync();
}
[AfterScenario]
public async Task AfterScenario()
{
await browser.CloseAsync();
playwright.Dispose();
}
}
What This Does
- Starts browser before each scenario
- Closes browser after each scenario
This ensures clean isolation between tests.
Summary
Reqnroll Hooks are essential for managing test execution flow.
Key takeaways:
- Hooks run automatically at specific test lifecycle stages
- Use them for setup, teardown, and shared logic
- Improve readability and maintainability
- Integrate easily with tools like Playwright
For modern E2E testing in .NET, using Hooks is a best practice you should not skip.
