Developer Docs

Custom Tests with C#Bot

By default, C#Bot writes tests for the client-side, server-side and test target.

We will be making a simple Test Target Specflow test. The test will be a custom invalid login test that uses a mixture of C# bot-written steps and a custom step.

Setting Up The Solution

Navigate to your root project directory and run the following commands to setup the solution:

dotnet new sln
dotnet sln add ./testtarget/TestDataLib/TestDataLib.csproj
dotnet sln add ./testtarget/Selenium/SeleniumTests.csproj
dotnet sln add ./testtarget/API/APITests.csproj
dotnet sln add ./testtarget/Serverside/ServersideTests.csproj
dotnet sln add ./serverside/src/[PROJECTNAME].csproj

You should now be able to open the solution in your IDE.

Feature Files

The first step is to setup a Specflow Feature file. To do this, create a file in the /testtarget/Selenium/Tests/Manual/ folder called CustomFishnaticsLoginTests.feature. Open this file in your editor of choice, and paste in the following:

Feature: CustomFishnaticsLoginTests

Custom login tests

Scenario: I attempt to login to fishnatics with invalid credentials
    Given I login to the site with username  and password notreal then I expect login failure
    Then I want to print a simple message " cannot be logged in" to the test output

The first Given step is already defined as a bot-written step in the Fishnatics project. The second Then step is undefined and will need to be defined.

Step Definition

Navigate to the /testtarget/Selenium/Steps/Manual/ folder and create new file called CustomFishnaticsLoginSteps.cs. Open the file and paste in the following code.

using SeleniumTests.Setup;
using TechTalk.SpecFlow;

namespace SeleniumTests.Steps.Manual
    public class CustomFishnaticsLoginSteps
        private readonly ContextConfiguration _context;

        public CustomFishnaticsLoginSteps(ContextConfiguration contextConfiguration)
            _context = contextConfiguration;

The contextConfiguration gives access to all the important tools for building integrations tests (e.g. webDriver, testOutputHelper). We will use it in our step to print a simple message to the test output.

Add the following step definition to the CustomFishnaticsLoginSteps class.

[Then(@"I want to print a simple message ""([^""]*)"" to the test output")]
    public void ThenIWantToPrintASimpleMessageToTheTestOutput(string message)

Running Tests

Tests can be run through the command line, or through an IDE. Before running any Specflow tests, ensure that both server and client are running.

Command Line

Navigate to /testtarget/selenium/ and run the command:

dotnet test --filter Category=new

We use the @new tag added in the .feature file to run specific tests or groups of tests, by using the --filter Category=[tag].

Visual Studio

In order to run the tests through Visual Studio, the Specflow package needs to be installed. Follow the steps in this guide from Specflow to install Specflow for Visual Studio.

Open up the test explorer by going to Test -> Test Explorer in the Visual Studio navigation bar. Now find the CustomFishnaticsLoginTestsFeature in the test explorer, right click on it, and select Run.


Was this article helpful?

Thanks for your feedback!

If you would like to tell us more, please click on the link below to send us a message with more details.







On this page

New to Codebots?

We know our software can be complicated, so we are always happy to have a chat if you have any questions.