Creating the Base Scenarios

Validating the Title of a Website

Creating the SpecFlow Scenario

  1. Open up the ‘BaseScenarios.feature’ file and add the following test scenario. We can also add a tag (e.g. ‘@Chrome’ for Chrome or ‘@HeadlessFirefox’ for Firefox in headless mode, either above the ‘Scenario’ (to apply to the specific scenario) or above the ‘Feature’ (to apply to all scenarios within the feature)…

    The Given and Then steps are still undefined, so let’s add definitions for them now.

Creating the Undefined Step Definitions

  1. Copy the method below in BaseSteps class for the undefined Given step…
  2. Because our base page is also the search page, let’s slightly tweak our undefined ‘Given’ step definition so that we can use RegEx to either use the word ‘base’ or ‘search’ when referring to the step and being on the page, without capturing the match and also accept either DuckDuckGo user or internet user(see https://agileforall.com/just-enough-regular-expressions-for-cucumber/ for more info…
  3. Next, copy the method for the undefined Then step definition to the BaseSteps class…

Creating the Test Methods

  1. Open up the ‘BasePage.cs’ class file in the ‘Pages’ namespace and add the following method to navigate to the Base URL (as defined in your ‘Settings.cs’ class file, with the whole thing looking similar to below…
  2. Also add another method for asserting the page title contains the expected string…

    For the above to work, you will also need to add a GetTitle() method near the top of the class (near where Driver is declared), which returns Driver.GetTitle() …

The whole thing should look similar to below when done…

Connecting the Step Definitions to the Test Methods

We will now call the test methods from their appropriate step definitions. This whole POM approach is nice as we have suitable layers of abstraction, which helps the code be easier to read.  It also allows the step definitions to simply contain one line of code when calling the test methods.

  1. Open up the ‘BaseSteps.cs’ step definition class file again, and refactor the class to look like below, noting how we initialise our basePage object at the top and how we keep assertions outside of our page classes…

Validate the URL of the Website

Creating the SpecFlow scenario

  1. Open up the ‘BaseScenarios.feature’ file and add the following test scenario…

Creating the Undefined Step Definition

  1. Next, let’s create the undefined step definition for the Then step…

Creating the Test Method

We will now create the test method in C# for our undefined ‘Then’ step 😀

  1. Let’s add a GetUrl() property near the top of the BasePage class (near where Driver is declared), which returns Driver.GetUrl() …

Defining the Step Definition

  1. Open up the BaseSteps.cs class and add the assertions to the step definition, using the GetUrl property we just added to the BasePage…

    The whole thing should look like below…

Validate a PageSource String on the Website

Creating the SpecFlow Scenario

  1. Open up the ‘BaseScenarios.feature’ file and add the following test scenario…

Creating the Undefined Step Definition

  1. Next, let’s create the method for the undefined step definition and add it to the BaseSteps class again…

Creating the Test property

  1. Open up the BasePage class again and add a new property for the current page source

Defining the Step Definition

  1. Open up the ‘BaseSteps.cs’ class file again, and add the assertion to the step definition…

    The whole thing should look like below…

Validate Existence of Multiple Text in PageSource

Creating the SpecFlow Scenario

  1. Open up the ‘BaseScenarios.feature’ file and add the following test scenario…

Creating the Undefined Step Definition

  1. Next, create the undefined step definition for the Then step in BaseSteps class and add a Table parameter, like below…

Defining the Step Definition

  1. Open up the ‘BaseSteps.cs’ class file again, and define the step by adding the assertion for each item in the table…

    The whole thing should look like below now…

SpecFlow Background Step

  1. If you look at the four base scenarios in your ‘BaseScenarios.feature’ file, you will see that every scenario shares the step of ‘Given I navigate to the base URL’.  Because of this, we can clean up the Feature file a bit by declaring the ‘Given’ step once as a Background step, which will be applied at the beginning of every scenario in the Feature file (see https://github.com/cucumber/cucumber/wiki/Background for more information)…

    We have now successfully added our BaseScenarios.  In the next part, we will actually add our first proper test of searching for something on Google and then clicking through on a search result 😀
Digiprove sealCopyright secured by Digiprove © 2018
Liked it? Take a second to support Thomas on Patreon!

Previous Article

Next Article

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.