BDD framework in Webdriver using C#

In my previous post, I have posted BDD framework in Webdriver using Java. In this post I will show you how to create BDD framework using C#. To achieve this I will use SpecFlow. This is same as Cucumber and you can write specs (features) file by using the same Gherkin language.
Setup:
1. Install Visual studio.
2. Download and install SpecFlow from link :”click” For more detail about SpecFlow go to link ”click
3. Download NUnit Test Adapter from link:”click”.
Steps to create project: 
1. Go to File >>New >>Project and click.
2. Select Visual C# >> Class Library.
3. Enter project name into name filed and click on "OK" button, as below screen.


Add Reference Library:
1. Now add NUnit, SpecFlow and WebDriver in your project using NuGet.
2. Right click on your project select "Manage NuGet Packages".
3. Select online option and enter NUnit into search field, after click on search button you should see NUnit,
4. Click on install button of NUnit.


5. Repeat above installation steps for SpecFlow and WebDriver
Step to create scripts:
1. Now you are ready to create test scripts. Right click on project and goto on Add>>New Item
2. On opened dialog box select "SpecFlow Feature File" and enter file name like "GoogleSearch" and click on “Add” button.

3. You should see a feature file default template code generated in file. Copy below code and past into “GoogleSearch.feature” file.
Feature: Customer Transfer's Fund
           
Scenario: Google Search
        Given user is on google search page
        When enter "testing" text into search field
        And click on search button
        And click on advance search icon
        And click on advance search link
        And click on advance search button
        Then verify first link text "testing"
4. To add step definition file right click on project and go to “Add>>New Item” in open dialog box, select “SpecFlow Step Definition” and enter file name “GoogleSearchSteps.cs” in to name filed. Click on "Add" button.
Add below code in file.
using NUnit.Framework;
using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TechTalk.SpecFlow;

namespace WebdriverBdd
{
    [Binding]
    public class GoogleSearchStep
    {
        // For additional details on SpecFlow step definitions see http://go.specflow.org/doc-stepdef

        private IWebDriver driver;

        [Given(@"user is on google search page")]
        public void UserIsOnGoogleSearchPage()
        {
            driver = Configuration.Driver;
            //driver = new ChromeDriver(@"D:\Selenium");
            driver.Navigate().GoToUrl("https://www.google.co.in");
            driver.Manage().Window.Maximize();
            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
        }


        [When(@"enter ""(.*)"" text into search field")]
        public void WhenUserEneteredIntoThePayeeNameField(string payeeName)
        {
            driver.FindElement(By.Id("gbqfq")).SendKeys(payeeName);
        }

        [When(@"click on search button")]
        public void ClickOnSearchButton()
        {
            driver.FindElement(By.Id("gbqfb")).Click();
        }


        [When(@"click on advance search icon")]
        public void ClcikOnAdvanceIcon()
        {
            driver.FindElement(By.Id("abar_button_opt")).Click();
        }



        [When(@"click on advance search link")]
        public void ClickOnAdvanceSearchLink()
        {
            driver.FindElement(By.XPath("//div[text()=\"Advanced search\"]")).Click();
        }


        [When(@"click on advance search button")]
        public void ClcikOnAdvanceSearchButton()
        {
            driver.FindElement(By.XPath("//input[@value='Advanced Search']")).Click();
        }

        [Then(@"verify first link text ""(.*)"" ok")]
        public void VerifyFirstLinkTest(string message)
        {
            String text = driver.FindElement(By.XPath("//*[@id='rso']/li[1]/div/h3/a")).Text;
            Assert.IsTrue(text.Contains(message));
        }

    }
}         



5. Right click on project and add new class with name "Configuration.cs" Add below code into file. and add below code:


using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TechTalk.SpecFlow;

namespace WebdriverBdd
{
        [Binding]
        public class Configuration
        {
            private static IWebDriver driver;

            public static IWebDriver Driver
            {              
                get { return driver ?? (driver = new FirefoxDriver()); }
            }

            [AfterTestRun]
            public static void AfterTestRun()
            {
                Driver.Close();
                Driver.Quit();
                driver = null;
            }
       
    }
}

6. Now your first webdriver c# test script is ready in BDD framework. Go to "Test>> Window" click on "Test Explorer" as below screen:

You should see like below screen of "Test Explorer"
7. Click on run button to execute your test scenario.

2 comments:

  1. very easily explained. thank you

    ReplyDelete
  2. Using nunit how to execute group execution(Smoke and Regression)?

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution