Executing Selenium tests with SpecFlow

We can execute selenium tests using SpecFlow. In your visual studio project, you need to add Selenium NuGet Package as shown in below image.

Adding selenium package in SpecFlow project

Adding selenium package in SpecFlow project

Once the package is added, you can write the feature file as shown in below example.

Feature: Selenium Feature file

@selenium
Scenario: Selenium Scenario 1
	Given I am on the web page "http://www.softpost.org"
	Then I verify that title of the page contains "tutorial"

 

Here is the Step definition class for above feature file. Above test will open Firefox and navigate to www.softpost.org website. Then it will check the title of web page.

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

namespace SpecFlowRunnerProject
{
    [Binding]
    public class ParametersInFeatureFileSteps
    {
        IWebDriver driver;

       [Given(@"I am on the web page ""(.*)""")]
        public void GivenIAmOnTheWebPage(string p0)
        {
            driver= new FirefoxDriver();
            Console.WriteLine("I am on page " + p0);
            driver.Navigate().GoToUrl(p0);
        }
        
        [Then(@"I verify that title of the page contains ""(.*)""")]
        public void ThenIVerifyThatTitleOfThePageContains(string p0)
        {
            Console.WriteLine("Title of the web page is " + p0);
            Assert.IsTrue(driver.Title.ToLower().Contains(p0));
            driver.Close();
        }
    }
}

You may also like...