Specflow Cheat Sheet
Gherkin Keywords
- Feature
- Scenario
- Background
- Given
- When
- Then
- And
- But
- Scenario Outline
- Examples
Feature File
Gherkin
Copy
Feature: Login functionality Scenario: Successful login Given the user is on the login page When the user enters valid credentials Then the user should be logged in successfully
Step Definition File
C#
Copy
[Binding] public class LoginSteps { [Given("the user is on the login page")] public void navigateToLoginPage() { // Code to navigate to the login page Console.WriteLie("User is on the login page"); } [When("the user enters valid credentials")] public void enterValidCredentials() { // Code to enter valid login credentials Console.WriteLie("User enters valid credentials"); } [Then("the user should be logged in successfully")] public void verifySuccessfulLogin() { // Code to verify successful login Console.WriteLie("User is logged in successfully"); } }
Parameterization
Gherkin
Copy
Feature: Login functionality Scenario: Successful login Given the user is on the login page When the user enters username "root" and password "admin" Then the user should be logged in successfully
C#
Copy
using TechTalk.SpecFlow; using Xunit; [Binding] public class LoginSteps { [When("the user enters username {string} and password {string}")] public void theUserEntersUsernameRootAndPasswordAdmin(String username, String password) { Console.WriteLine("The user enters username " + username + " and password " + password + "."); } }
Scenario Outline
Gherkin
Copy
Feature: Login functionality Scenario Outline: Successful login Given the user is on the login page When the user enters username <username> and password <password> Then the user should be logged in successfully Examples: | username | password | | "root" | "admin" | | "admin" | "admin" |
C#
Copy
using TechTalk.SpecFlow; using Xunit; [Binding] public class LoginSteps { [When("the user enters username {string} and password {string}")] public void theUserEntersUsernameRootAndPasswordAdmin(string username, string password) { Console.WriteLine("The user enters username " + username + " and password " + password + "."); } }
Background
Gherkin
Copy
Feature: Login functionality Background: Given the user is on the login page Scenario: Successful login When the user enters username "root" and password "admin" Then the user should be logged in successfully
C#
Copy
using TechTalk.SpecFlow; using Xunit; [Binding] public class LoginSteps { [Given("the user is on the login page")] public void navigateToLoginPage() { // Code to navigate to the login page Console.WriteLine("User is on the login page"); } }
Data Table
Gherkin
Copy
Feature: User registration Scenario: Successful User registration Given the following user details | Name | Age | City | | John | 25 | New York | When the user submits the registration form Then the user should be registered successfully
C#
Copy
using TechTalk.SpecFlow; using Xunit; [Binding] public class UserDetails { [Given("the following user details")] public void enterUserDetails(Table table) { foreach (var row in table.Rows) { String name = row["Name"]; String age = row["Age"]; String city = row["City"]; // Perform any necessary operations with the numbers } } }
Hooks
C#
Copy
[BeforeScenario] public void BeforeScenario() { // Code to set up test data or initialize resources } [AfterScenario] public void AfterScenario() { // Code to clean up resources or perform cleanup actions } [BeforeFeature] public static void BeforeFeature() { // Code to set up feature-specific data or initialize resources } [AfterFeature] public static void AfterFeature() { // Code to clean up resources or perform cleanup actions after a feature } [BeforeTestRun] public static void BeforeTestRun() { // Code to perform global setup tasks or initialize shared resources } [AfterTestRun] public static void AfterTestRun() { // Code to perform global cleanup tasks or release shared resources } [Before("@smoke")] public void BeforeSmokeTests() { // Code to set up environment before running smoke tests }
Tags
Gherkin
Copy
@smoke Feature: Login functionality @positive @regression Scenario: Successful login Given the user is on the login page When the user enters valid credentials Then the user should be logged in successfully
To run scenarios with a specific tag, you can use the following command:
dotnet test --filter "Tags=smoke"
Context Injection
C#
Copy
using TechTalk.SpecFlow; [Binding] public class LoginSteps { private readonly ScenarioContext _scenarioContext; public LoginSteps(ScenarioContext scenarioContext) { _scenarioContext = scenarioContext; } [Given("I am on the login page")] public void GivenIAmOnTheLoginPage() { // Automation logic to navigate to the login page } [When("I enter my username as '(.*)' and password as '(.*)'")] public void WhenIEnterMyUsernameAndPassword(string username, string password) { _scenarioContext["Username"] = username; _scenarioContext["Password"] = password; } [Then("I should be logged in")] public void ThenIShouldBeLoggedIn() { string username = _scenarioContext["Username"] as string; string password = _scenarioContext["Password"] as string; // Automation logic to verify login with the provided username and password } }