Skip to content

Your Turn

It is your turn to write your own specifications for the ModelApp using the Gherkin syntax.

Steps for Writing SpecFlow Test

  1. Create an MsTest Project
  2. Ensure the SpecFlow library for executing SpecFlow tests is installed. Via NuGet packages, search and install SpecFlow.MsTest
  3. Create Feature File. Right click on the test project and add new item, select SpecFlow Feature file, then name the file according to what feature is being tested. A sample feature with scenario is auto-generated. Edit this file according the feature being tested.
  4. Build your Solution and go to the Test Explorer to see your tests. These tests will fail if executed because feature file needs to be implemented and this is done with step definition file
  5. To generate the step definition file, right click anywhere in the feature file and select `Generate Step Definitions' and Select Generate.
  6. Modify the step definition file by replacing ScenarioContext.Current.Pending() with an actual implementation of your test feature file.
  7. Build your solution and run the test from the test explorer.

Below is a Sample Feature file that you can use to write your test

Feature: EditInquiry
    I should be able to edit inquiry submitted from the Entry List page

@mytag
Scenario: Edit a submitted inquiry.
    Given I navigate to ModelApp
    When I click the Entry List link
    And I click Edit on an existing entry
    And I update the email address
    And I click on Save
    Then I should return to Todo page
Cheat Sheet

Valid Test Implementation:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using System;
using System.Threading;
using TechTalk.SpecFlow;

namespace ModelApp.FunctionalTests.SpecFlowFeaturesTest
{
    [Binding]
    public class EditInquirySteps
    {
        IWebDriver driver = new EdgeDriver();
        string endpoint = Environment.GetEnvironmentVariable("APP_ENDPOINT");

        [When(@"I click the Entry List link")]
        public void WhenIClickTheEntryListLink()
        {
            driver.Navigate().GoToUrl(endpoint);
            driver.FindElement(By.LinkText("Entry List")).Click();
        }

        [When(@"I click Edit on an existing entry")]
        public void WhenIClickEditOnAnExistingEntry()
        {
            driver.FindElement(By.LinkText("Edit")).Click();
        }

        [When(@"I update the email address")]
        public void WhenIUpdateTheEmailAddress()
        {
            driver.FindElement(By.Id("Email")).Clear();
            driver.FindElement(By.Id("Email")).SendKeys("mytest@test.com");
        }

        [When(@"I click on Save")]
        public void WhenIClickOnSave()
        {
            driver.FindElement(By.CssSelector(".btn")).Click();
        }

        [Then(@"I should return to Todo page")]
        public void ThenIShouldReturnToTodoPage()
        {
            Assert.IsTrue(driver.Url.Contains("Todo"));

            Thread.Sleep(3000);
            driver.Quit();
        }
    }
}