Cucumber Framework (BDD Automation) with Selenium

Cucumber & SpecFlow:


  • Cucumber is a testing framework which supports Behavior Driven Development(BDD).
  • Cucumber itself written in Ruby but it's allow to write tests in different language like java,C# and Python.
  • BDD framework is normally define application behaviour in simple English text using simple english grammar. That language normally known as GHERKIN.
  • So, Cucumber uses Gherkin formats to present user stories.
  • Cucumber in Visual studio for c# is known as SpecFlow.
  • Writing tests in GHERKIN language is  not an important part here,it's about creating a common understanding.
  • BDD should be used for verifying the most important parts of the application using end-to-end tests.
  • Sometimes it is valuable to implement BDD with Cucumber for unit tests and Integration testing which is a contineous process formly know as agile process.
  • The decision to use Cucumber or a unit testing framework is depending on the cooperation with the business. If they have opinions about the behaviour, then use Cucumber. If they are indifferent, use a unit testing framework.
Gherkin Language:
Gherkin is a language for describing an expected behaviour with some basic English text with some keys .

Ex :
Feature: Login Facebook

@mytag
Scenario: Valid Login
Given I have entered "www.facebook.com" into the chrome browser
And I have entered "QATEST1234" as username
      And I have entered "QATEST1234678" as password
      When I Click signin
      Then the login should successful
      But home page should not be missing.
Given - the preconditions of the test to be executed. Setup for open browser webdriver command.
When - It describe the starting point or trigger point of test.Like when I enter username
Then - the expected final state of the system. Example − THEN login should be successful.
And - It represents logical AND condtion between any statemnent. You can use And with Given,When , Then.
But - It represents logical OR condtion between any statemnent. You can use BUT with Given,When , Then.
EX:THEN login should be successful. BUT home page should not be missing.

Setup:

Open Visual studio :


Click on New Project ==>> Select Unit Test project templet and give a project name according to yours.





Delete the default class "UnitTest1".
Right click on your project ==>> Select "Manage Nuget Packages" ==>> Click online tab ==>>  click on Nuget.org ==>> Search Online "specflow"





After Installation verify the references package "TechTalk.SpecFlow" should display



Now Right click on Project and click"Add" ==>> New Item 



Then Select SpecFlow Feature File and change the name according to yours.


Now Write a scenario for Google Search like below example
Feature: GoogleSearch
@mytag
Scenario:GoogleSearch
Given I have entered "https://www.google.co.in/?gws_rd=ssl" into browser
And I have entered search text"SACHIN"
When I press search
Then the result should be get display
Now RightClick on Given /And/When/Then and select Generate step defination


Now click Generate button



After that check weather  steps.cs added to your project or not


Program added :
using System;
using TechTalk.SpecFlow;

namespace AutomationCucumber
{
    [Binding]
    public class GoogleSearchSteps
    {
        [Given(@"I have entered ""(.*)"" into browser")]
        public void GivenIHaveEnteredIntoBrowser(string p0)
        {
            ScenarioContext.Current.Pending();
        }
        
        [Given(@"I have entered search text""(.*)""")]
        public void GivenIHaveEnteredSearchText(string p0)
        {
            ScenarioContext.Current.Pending();
        }
        
        [When(@"I press search")]
        public void WhenIPressSearch()
        {
            ScenarioContext.Current.Pending();
        }
        
        [Then(@"the result should be get display")]
        public void ThenTheResultShouldBeGetDisplay()
        {
            ScenarioContext.Current.Pending();
        }
    }
}

  • Now Remove " ScenarioContext.Current.Pending();" from all methods.
  • Write some seleniumcode for those feature steps.
  • Before that make sure selenium and chrome/firefox driver.exe should be installed.

Installation of selenium in your project:
1.Right click onyour project ==>> Select "Manage Nuget Packages" ==>> Click online tab ==>>  click on Nuget.org ==>> Search Online "selenium.webDriver" ==>> Install selenium.


2. Right click onyour project ==>> Select "Manage Nuget Packages" ==>> Click online tab ==>>  click on Nuget.org ==>> Search Online "ChromeDriver/FirefoxDriver" ==>> Install driver.exe.



Now write selenium program on steps.cs file:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Threading;
using TechTalk.SpecFlow;

namespace AutomationCucumber
{
    [Binding]
    public class GoogleSearchSteps
    {
        public IWebDriver driver;
        [Given(@"I have entered ""(.*)"" into browser")]
        public void GivenIHaveEnteredIntoBrowser(string URL)
        {
            driver = new ChromeDriver();
            Thread.Sleep(4000);
            driver.Navigate().GoToUrl(URL);
            Thread.Sleep(5000);
        }
        
        [Given(@"I have entered search text""(.*)""")]
        public void GivenIHaveEnteredSearchText(string searchText)
        {
            driver.FindElement(By.Name("q")).SendKeys(searchText);
        }
        
        [When(@"I press search")]
        public void WhenIPressSearch()
        {
            driver.FindElement(By.Name("btnG")).Click();
        }
        
        [Then(@"the result should be get display")]
        public void ThenTheResultShouldBeGetDisplay()
        {
            Console.WriteLine("Results are getting display");
        }
    }
}

Now Add TestExplore to your project:
1. Click on Test option on visual studio.
2. Select Window ==> Test Explorer



Now Click on Build option in visual studio and select build solution option.


Errors will display as "The type or namespace name 'NUnit' could not be found (are you missing a using directive or an assembly reference?)"
Resolve the above issue by adding "nunit.framework.dll".
How to add "nunit.framework.dll" into our project ?
Ans :- Right click onyour project ==>> Select "Manage Nuget Packages" ==>> Click online tab ==>>  click on Nuget.org ==>> Search Online "nunit.framework.dll" ==>> Install nunit.framework.dll.



After adding the nunit.framework.dll file Again build the project.Now , this time Build will success.
Now Run the program , Sometimes You will face another issue as "A project with an Output Type of Class Library cannot be started directly.
In order to debug this project, add an executable project to this solution which references the library project. Set the executable project as the startup project."


Resolve by doing those steps.
Solution 1 :
Right click on the project you want to run -> Set as start up project
solution 2 : 


  • Right click the Project name in Solution Explorer of Visual Studio
  • Select Properties
  • Select Application tab
  • In Output type drop box, select the correct application type of your project
Re-run your project and let me know if it won’t work.

Once the problem got resolve ,scenarios will display left side under test explorer. Then select the scenario and run the program




Cucumber project is ready ...










3 comments:

  1. Madhu murali adapa14 June 2017 at 10:24

    Great Post Sunil. Thank you for posting. Really helping a lot.

    ReplyDelete
  2. I was searching step by step process for specflow in c#. Thank you for a detailed post with all kind of solution. Love the site. I was following this site since long time. Please do posting.

    Regards,
    Rajath

    ReplyDelete

Copyright © 2017 QALEARNINGGUIDE.COM || ALL RIGHTS RESERVED