SpecFlow Tutorial For Automation Testing With Selenium C#

Image for post
Image for post

Note: The code samples may be displayed improperly because of markdown. I recommend continuing to read the original article on our blog to make sure all the examples are displayed properly.

Introduction To Behavior Driven Development (BDD)

Outside-In Development

Advantages of BDD

How To Write BDD Scenarios?

Scenario: Title/Short Description
Given [A Precondition]
When [Some Event]
Then [Some Outcome]
Scenario: Search for LambdaTest on DuckDuckGo
Given I am on the DuckDuckGo homepage
When I enter search term as LambdaTest
Then Search results for LambdaTest should appear

Feature Files In Gherkin

Feature: Search using Google and DuckDuckGo
Search for LambdaTest on Google
Search for LambdaTest on DuckDuckGo
Compare the results

@LambdaTestSearch
Scenario: Search for LambdaTest on DuckDuckGo
Given I am on the DuckDuckGo homepage
When I enter search term as LambdaTest
Then Search results for LambdaTest should appear

Scenario: Search for LambdaTest on Google
Given I am on the Google homepage
When I enter search term as LambdaTest
Then Search results for LambdaTest should appear

Tools For BDD Testing

Image for post
Image for post

Getting Started With SpecFlow

What Is SpecFlow?

Overview Of BDD With SpecFlow

Image for post
Image for post
Image for post
Image for post

Installing SpecFlow In Visual Studio 2019

Opera- https://github.com/operasoftware/operachromiumdriver/releasesFirefox- https://github.com/mozilla/geckodriver/releasesChrome- http://chromedriver.chromium.org/downloadsIE- https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriverMicrosoft Edge- https://blogs.windows.com/msedgedev/2015/07/23/bringing-automated-testing-to-microsoft-edge-through-webdriver/

1. Installation Of Necessary Plugins In Visual Studio

Image for post
Image for post

2. Installation of SpecFlow & Other Required Packages

Image for post
Image for post
Image for post
Image for post
Install-Package Specflow
Install-Package Specflow.NUnit
Install-Package SpecFlow.Tools.MsBuild.Generation
Install-Package NUnit3TestAdapter
Image for post
Image for post
Image for post
Image for post
PM> Get-Package

Id Versions
— — — — —
SpecFlow {3.0.225}
SpecFlow.NUnit {3.0.225}
SpecFlow.Tools.MsBuild.Generation {3.0.225}
Install-Package NUnit3TestAdapter {3.15.1}
……………………………………..
……………………………………..
Image for post
Image for post
Image for post
Image for post

Updating From SpecFlow 2.x To SpecFlow 3

Integration With Visual Studio

Image for post
Image for post

Configure SpecFlow Using specflow.json

Updating SpecFlow To Version 3

Install-Package [package name] -Version [package version]
Install-Package Specflow -Version 3.0.225
Install-Package SpecFlow.NUnit -Version 3.0.225
Install-Package SpecFlow.Tools.MsBuild.Generation -Version 3.0.225
Image for post
Image for post
Image for post
Image for post

SpecFlow & Selenium WebDriver

Image for post
Image for post

To-Do App: Test with SpecFlow & Selenium WebDriver

Image for post
Image for post
Image for post
Image for post
Install-Package NUnit
Install-Package NUnit.Console
Install-Package Selenium.WebDriver
Install-Package Selenium.Firefox.WebDriver
Image for post
Image for post
Given that I am on the LambdaTest Sample app
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Given that I am on the LambdaTest Sample app
[Given(@”that I am on the LambdaTest Sample app”)]
public void GivenThatIAmOnTheLambdaTestSampleApp()
{
driver = new FirefoxDriver();
driver.Url = test_url;
driver.Manage().Window.Maximize();
System.Threading.Thread.Sleep(2000);
}
[Then(@”select the first item”)]
public void ThenSelectTheFirstItem()
{
// Click on First Check box
IWebElement firstCheckBox = driver.FindElement(By.Name(“li1”));
firstCheckBox.Click();
}
[Then(@”close the browser instance”)]
public void ThenCloseTheBrowserInstance()
{
driver.Quit();
}
Image for post
Image for post
Image for post
Image for post

Using SpecFlow With Remote Selenium Grid

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.SetCapability(“user”,”Your Lambda Username”)
capabilities.SetCapability(“accessKey”,”Your Lambda Access Key”)
capabilities.SetCapability(“build”, “your build name”);
capabilities.SetCapability(“name”, “your test name”);
capabilities.SetCapability(“platform”, “Windows 10”);
capabilities.SetCapability(“browserName”, “Firefox”);
capabilities.SetCapability(“version”,”62.0");
Image for post
Image for post

SpecFlow With NUnit On Remote Selenium Grid

/* For using Remote Selenium WebDriver */
using OpenQA.Selenium.Remote;
String username = “user-name”;
String accesskey = “access-key”;
String gridURL = “@hub.lambdatest.com/wd/hub”;
……………………………………………………………..
DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.SetCapability(“user”, username);
capabilities.SetCapability(“accessKey”, accesskey);
capabilities.SetCapability(“build”, “Using Firefox WebDriver using C# and NUnit”);
capabilities.SetCapability(“name”, “Using Firefox WebDriver using C# and NUnit”);
capabilities.SetCapability(“platform”, “Windows 10”);
capabilities.SetCapability(“browserName”, “Firefox”);
capabilities.SetCapability(“version”, “62.0”);

………………………………………………………………………
………………………………………………………………………
………………………………………………………………………

driver = new RemoteWebDriver(new Uri(“https://user-name:access-key@hub.lambdatest.com/wd/hub”), capabilities, TimeSpan.FromSeconds(600));
Image for post
Image for post

It’s A Wrap!

Happy testing!

Written by

Product Growth at @lambdatesting (www.lambdatest.com)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store