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)…

    You should notice that the ‘Given’ and ‘Then’ lines are highlighted.  This is because they do not yet have any step definitions attached to them.

Creating the Undefined Step Definitions

  1. Right-click on the ‘Given’ scenario line and select ‘Generate Step Definitions’
  2. Copy the method for the undefined step definition to the clipboard and then paste it in to ‘BaseScenariosSteps.cs’ class file like below…
  3. 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 (see https://agileforall.com/just-enough-regular-expressions-for-cucumber/ for more info…
  4. Next, right-click on the ‘Then’ scenario line and select ‘Generate Step Definitions’
  5. Copy the method for the undefined step definition to the clipboard and then paste it in to ‘ValidationSteps.cs’ (due to it being an assertion where we verify/check something)…

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. Also make sure the ‘using’ reference of ‘Utils.Selenium.Settings is added’ above the namespace)…
  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.

Due to the method we created earlier in the ‘Page’ class, where we made a generic method called InstanceOf<T>() that takes the same generic class and initialises a new object from PageFactory with the correct elements for the page, we can now use that when calling our test methods 🙂

  1. Open up the ‘BaseScenariosSteps.cs’ class file again, and refactor the ‘Given’ step definition so it initialises the page (in this case ‘BasePage’) with the correct elements and then calls the NavigateBaseUrl() method, like below…
  2. Open up the ‘ValidationSteps.cs’ class file again, and refactor the ‘Then’ step definition so it initialises the page (in this case ‘BasePage’ again) with the correct elements and then calls the ValidatePageTitle() method, like below…

Validate the URL of the Website

Creating the SpecFlow scenario

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

    You should notice that only the  ‘Then’ line is highlighted.  This is because it does not yet have any step definition attached to it, but the ‘Given’ line does, as we have already added code for that scenario line and are simply reusing it in this scenario.

Creating the Undefined Step Definition

  1. Right-click on the ‘Then I see the page Url contains “https://www.google.com”‘ scenario line and select ‘Generate Step Definitions’
  2. Copy the method for the undefined step definition to the clipboard and then paste it in to ‘ValidationSteps.cs’…

Creating the Test Method

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

  1. Open up the ‘BasePage.cs’ class file in the ‘Pages’ namespace and add the following method to assert that the page Url contains the expected string…

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

Connecting the Step Definition to the Test Method

  1. Open up the ‘ValidationSteps.cs’ class file again, and refactor the ‘Then I see the page Url contains “https://www.google.com”’ step definition so it initialises the page (in this case ‘BasePage’ again) with the correct elements and then calls the ValidatePageUrl() method, 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…

    You should again notice that only the  ‘Then I see “Google Search” in the PageSource ’ step is highlighted.  This is again because it does not yet have any step definition attached to it, but the ‘Given’ line does, as we have already added code for that scenario line and are simply reusing it in this scenario.

Creating the Undefined Step Definition

  1. Right-click the ‘Then I see “Google Search” in the PageSource’ step and select ‘Generate Step Definitions’
  2. Copy the method for the undefined step definition to the clipboard and then paste it in to ‘ValidationSteps.cs’…

Creating the Test Method

  1. Open up the ‘BasePage.cs’ class file again in the ‘Pages’ namespace and add the following method to assert that the PageSource contains the expected string…

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

Connecting the Step Definition to the Test Method

  1. Open up the ‘ValidationSteps.cs’ class file again, and refactor the ‘Then I see “Google Search” in the PageSource’ step definition so it initialises the page (in this case ‘BasePage’ again) with the correct elements and then calls the ValidatePageSource() method, 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…

    You should again notice that only the ‘Then I see’ line is highlighted.  This is again because it does not yet have any step definition attached to it, but the ‘Given’ line does, as we have already added code for that scenario line and are simply reusing it in this scenario.

Creating the Undefined Step Definition

  1. Right-click the ‘Then I see’ step and select ‘Generate Step Definitions’
  2. Copy the method for the undefined step definition to the clipboard and then paste it in to ‘ValidationSteps.cs’…

Creating the Test Method

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

  1. Open up the ‘BasePage.cs’ class file in the ‘Pages’ namespace and add the following method to assert the PageSource contains the multiple expected strings…

Connecting the Step Definition to the Test Method

  1. Open up the ‘ValidationSteps.cs’ class file again, and refactor the ‘Then I see’ step definition so it initialises the page (in this case ‘BasePage’ again) with the correct elements and then calls the ValidateMultipleInPageSource() method, like below…

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!

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.