SpecFlow Tutorial For Automation Testing With Selenium C#

To demonstrate the usage of SpecFlow Selenium C# with Local Selenium WebDriver, we take the example of a simple to-do app. Install SpecFlow.Tools.MsBuild.Generation package for https://www.globalcloudteam.com/ your project using the PM command Install-Package. Selenium uses the Selenium WebDriver for performing actions on the corresponding web elements displayed on the web page.

What is the SpecFlow feature

In case you require in-depth information about Selenium WebDrivers and web element locators, we recommend you check the Selenium C# tutorial that covers those topics in more detail. For better organization of the code, we create two folders – Features and StepDefinitions . Go to the section below and add a new item to the list.

SpecFlow & Selenium WebDriver

For the exact parameter rules please check the cucumber expressions page. In the later case a configurable conversion is applied. F# https://www.globalcloudteam.com/tech/specflow/ allows providing any characters in method names, so you can make the regular expression as the method name, if you use F# bindings.

This allows us to create scenarios in plain English using the Gherkin language, which is easily understood by everyone. As I promised above in this SpecFlow tutorial, time to dive into Gherkin. Feature files in Gherkin are plain simple txt files (with .feature extension) that can contain one or many scenarios depending on the test being performed. A feature file should only have only one feature but can have ‘N’ number of scenarios. A scenario outlines how a particular feature should behave with different types/values of input parameters. Gherkin is a domain-specific language using which you can come up with scenarios that describe business behavior, without getting into the technical implementation.

Automation Testing Cloud

Parameters have to be defined in the method signature to be able to receive these arguments. If both table and multi-line text argument are used for the step, the multi-line text argument is provided first. These will match to the parameters of the step definition method. Embedding images allow you to visually communicate with the reader. In the above example, we are displaying the mock-up the UX designer has put together for this feature, taking away the need to navigate to another application to view a mock-up.

What is the SpecFlow feature

For better traceability, the generated unit-test method names are derived from the scenario outline title and the first value of the examples . Scenario steps are defined using text and can have additional table called DataTable or multi-line text called DocString arguments. The Gherkin syntax allows any combination of these three types of steps, but a scenario usually has distinct blocks of Given, When and Then statements. You don’t need to have coding knowledge while writing tests using the BDD approach, all the tests are written in simple English language. It is a strategy to build conversations among different teams of the project to unite towards identifying users’ stories. In order to allow Gherkin to be written in a number of languages, the keywords have been translated into multiple languages.

Step Definition Methods Rules¶

SpecFlow uses the official Gherkin parser, which supports over 70 languages. These tests are then tied to your application code using so-called bindings, allowing you to execute the tests using the testing framework of your choice. You can also execute your tests using SpecFlow’s own dedicated test runner, SpecFlow+ Runner. SpecFlow generates a call inside the unit test method for each scenario step. The call is performed by the SpecFlow runtime that will execute the step definition matching to the scenario step. The scenario steps are a primary way to execute any custom code to automate the application.

Can be automated with a single step definition, with ‘Domain’ and ‘Communication’ as parameters. Assume you’re creating a scenario for validating a product added to an e-commerce application’s shopping cart that requires you to be logged in as a prerequisite. BDD is a communication and cooperation tool since it connects critical stakeholders, such as product owners, developers, and testers. This guarantees that they are all on the same page regarding the project’s requirements and expectations. 3) Make a new class called «CalculatorApplication» that will serve as our test application. This is a basic class including functions for doing addition, multiplication, division, square root, and other operations on the provided input.


Behaviour-Driven Development is a software development process that prioritizes human-readable descriptions of software requirements. These requirements are written using Gherkin syntax which is the most commonly used syntax for describing examples with Given/When/Then in plain text files called feature files. Although it is important to keep the feature files simple and easy to read, it is also vital that they carry all the necessary information and leave no room for ambiguity. For the.Net framework, SpecFlow is an open-source version of Cucumber. BDD tests may be produced using the Gherkin language and performed using the NUnit test framework utilizing SpecFlow with Selenium C#. It’s also compatible with MSTest, xUnit, and other test frameworks.

LivingDoc displays Gherkin feature specifications and their automated validation results as a single source of truth outside of an IDE – within your browser. With this understanding let’s move on the next topic where we will talk about Gherkin and the syntax it provided to write application tests/behavior. If we now come back to BDD/BRDSL we will see that we are able to describe tests in a more readable format.

Data Tables¶

SpecFlow is open source and provided under a BSD license. Automation testing with Selenium Grid has scalability issues hence, it is recommended to use remote Selenium Grid for improved test coverage. By doing minimal changes in the existing local Selenium WebDriver implementation, existing tests can be ported to a more scalable Remote Selenium Grid. I hope this SpecFlow tutorial gave you good insights about using SpecFlow with Selenium, C#. Share your thoughts about this SpecFlow tutorial below.

  • Once the folder for feature file is created, we are good to go to create a feature file.
  • SpecFlow is a BDD framework for .NET which boosts your productivity by helping you with writing your feature files and automation code in your favorite IDE using C# and .NET methods.
  • In this chapter, we are done with Feature file but still, we are not near to running the test using the feature file.
  • We may set up the ScenarioContext in the function Object() , just like any other class.
  • There is no change in the SpecFlow feature file and remaining business logic remains completely unchanged.

When SpecFlow tries to execute a step, it looks for a matching step definition to execute. One major hurdle in using the local Selenium Grid is that it cannot be scaled for full-fledged automated cross browser testing. Once the folders are created, we create a new Feature file – ToDoApp.feature by right-clicking on the folder to add a new item.

Our Team

The only exceptions are the feature and scenario descriptions. The snapshot above shows that the test has Completed i.e. passed and the video grab shows the overall execution of the test. You can visit Automation Dashboard to check the execution status of the test. Step 1 – OpenQA.Selenium.Remote package/namespace is imported as RemoteWebDriver class is defined in it.

Comparte este paquete