What are Selenium and SpecFlow?
Selenium is an open source browser automation tool which is used for end-to-end integration testing of web applications. Selenium is supported in multiple programming languages, including (but not limited to) Java, Python, and C#. C#Bot supports running tests in Chrome, Firefox, and Edge.
Specflow
SpecFlow is a free open source .Net tool for Behaviour Driven Development (BDD), if you are familiar with Java, SpecFlow is .Net equivalent of Cucumber. Rather than writing tests with code, they are instead written in Gherkin Syntax (Given-When-Then).
C# itself does not understand Gherkin ‘.feature’ files, therefore Specflow generates these into ‘.feature.cs’ class files at compile time. This allows xUnit to understand the defined Features so it can run the tests. It is important to note; developers do not need to touch the .feature.cs files, they can be ignored.
Selenium
Features
Selenium features are how the actions the test should take are set. These actions are split out into three primary categories:
-
Given steps define setup actions which must be performed before the actions which are being tested can be performed. Some examples are:
- Given I login with the admin account
- Given I login with the admin account
- Given I am on the homepage
- Given I navigate to the admin section
-
When steps define the actions which are being tested. Some examples are:
- When I click the create button
- When I type example into the name input
- When I create a valid entity
-
Then steps define the assertions which are carried out to ensure the test has passed. Some examples are:
- Then I assert that I am on the ExampleEntity backend page
- Then I assert that the entity has been created
- Then I assert that the no entities are selected
Multiple steps in a feature of the same type can also be chained together using an And step:
@BotWritten
Feature: Example Selenium Feature
@example
Scenario: Example Scenario
Given I login to the site with the admin account
And I am on the homepage
And I navigate to the ExampleEntity backend page
When I create an entity
Then I expect that an entity has been created
Scenarios and features can be grouped together with annotations such as the ‘@example’ tag on the scenario or feature level. These can be used group and filter the tests being run.
Step Definitions
The steps in the features define what actions the tests must perform when they are run. The step definitions define how these steps will be executed. Every step used in a selenium test must have a corresponding step definition, which defines the methods that will be run when the test is executed. The syntax for step definitions is standard C# syntax, however step definitions must also be annotated as follows:
[When("I create an entity")]
public void WhenICreateAnEntity() {
//Step Definition implementation goes here
}
The string in the annotation and the type of the step definition (Given, When, Then) must match for a step definition to be called when the scenario is being executed. Also remember that ‘And’ steps will use the relevant type, and do not need to be declared as ‘And’ step definitions.
Step definitions can also be created to include variables, which allows for customisation of scenarios without having to create additional step definition functions. The syntax for this is as follows:
[When("I create (.*) instances of the (.*) entity")]
public void WhenICreateEntities(int entityCount, string entityName) {
//Step Definition implementation goes here
}
This step definition would be called from a scenario like this. The variables in the step definition would be filled with the values entered into the step in the scenario:
When I create 5 instances of the ExampleEntity entity
Was this article helpful?