Writing simple BDD code with Selenium and Specflow
In last post we created all the necessary environment setup ready for testing our application using BDD with Selenium and Specflow, in this post, we will start to write a simple code starting with
Creating a Feature file
Creating a Step Definition file
And write out a very simple
Scenario in Feature file
Steps in Step Definition file
Here is how feature file for our simple scenario looks
Here is the complete video for our above discussion
Here is the Step definition for the above feature file
IWebDriver currentDriver = null;
[Given(@"I have navigated to Google page")]
public void GivenIHaveNavigatedToGooglePage()
{
Browser.Current.Navigate().GoToUrl(ConfigurationManager.AppSettings["seleniumBaseUrl"]);
currentDriver = Browser.Current;
}
[Given(@"I see the Google page fully loaded")]
public void GivenISeeTheGooglePageFullyLoaded()
{
if(currentDriver.FindElement(By.Name("q")).Displayed == true)
Console.WriteLine("Page loaded fully");
else
Console.WriteLine("Page failed to load");
}
[When(@"I type search keyword as")]
public void WhenITypeSearchKeywordAs(Table table)
{
dynamic tableDetail = table.CreateDynamicInstance();
currentDriver.FindElement(By.Name("q")).SendKeys(tableDetail.Keyword);
}
[Then(@"I should see the result for keyword")]
public void ThenIShouldSeeTheResultForKeyword(Table table)
{
dynamic tableDetail = table.CreateDynamicInstance();
string key = tableDetail.Keyword;
if (currentDriver.FindElement(By.PartialLinkText(key)).Displayed == true)
Console.WriteLine("Control exist");
else
Console.WriteLine("Control not exist");
}
Thanks for watching the video and reading the post !!!
Please leave your comments and let me know if there is anything need to be improved in this post.
Thanks,
Karthik KK
Post Author:
Karthik kk
18 Replies to “Writing simple BDD code with Selenium and Specflow”
Just loved the way you teach! Very complete and very detailed. You really explain every other concept very clearly which helps beginners understand them very well.
I am always getting an exception while running on Chrome:
Autofac.Core.DependencyResolutionException : An exception was thrown while invoking the constructor ‘Void .ctor()’ on type ‘ChromeDriver’. —> unknown error: unrecognized Blink revision: 3b3c00f2d95c45cca18ab944acced413fb759311
(Driver info: chromedriver=2.10.267521,platform=Windows NT 6.3 x86_64)
i even tried on 2-3 different machines and same issue was observed. Do you have any idea what could be the crux of this issue?
Test Name: GoogleSearchForExecuteAutomation on Chrome
Test Outcome: Not Run
Result Message:
No matching step definition found for one or more steps.
using System;
using TechTalk.SpecFlow;
namespace MyNamespace
{
[Binding]
public class StepDefinitions
{
[Given(@”I have navigated to Google Page”)]
public void GivenIHaveNavigatedToGooglePage()
{
ScenarioContext.Current.Pending();
}
[Then(@”I should see the result for keyboard”)]
public void ThenIShouldSeeTheResultForKeyboard(Table table)
{
ScenarioContext.Current.Pending();
}
}
}
Result StandardOutput:
Given I have navigated to Google Page
-> No matching step definition found for the step. Use the following code to create one:
[Given(@”I have navigated to Google Page”)]
public void GivenIHaveNavigatedToGooglePage()
{
ScenarioContext.Current.Pending();
}
And I see the Google page fully loaded
-> skipped because of previous errors
When I type search keyword as
— table step argument —
| Keyword |
| execute automation |
-> skipped because of previous errors
Then I should see the result for keyboard
— table step argument —
| Keyword |
| Execute Automation |
-> No matching step definition found for the step. Use the following code to create one:
[Then(@”I should see the result for keyboard”)]
public void ThenIShouldSeeTheResultForKeyboard(Table table)
{
ScenarioContext.Current.Pending();
}
I truly love your blog.. Excellent colors & theme. Did you create this site yourself? Please reply back as I’m hoping to create my own blog and would love to learn where you got this from or exactly what the theme is called. Many thanks!
I am always getting an exception while running on Firefox:
Test Name: GoogleSearchWithSelenium
Test FullName: TestDemo.Feature.GoogleSearchFeature.GoogleSearchWithSelenium
Test Source: D:\Aventis\TestDemo\TestDemo\Feature\GoogleSearch.feature : line 5
Test Outcome: Failed
Test Duration: 0:00:00.2892561
Result StackTrace:
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at OpenQA.Selenium.Firefox.Internal.Executable.LocateFirefoxBinaryFromPlatform()
at OpenQA.Selenium.Firefox.Internal.Executable..ctor(String userSpecifiedBinaryPath)
at OpenQA.Selenium.Firefox.FirefoxBinary..ctor(String pathToFirefoxBinary)
at OpenQA.Selenium.Firefox.FirefoxDriver..ctor()
at TestDemo.Step.GoogleSearchSteps.GivenIHaveNavigatedToGoogleSearchPage() in D:\Aventis\TestDemo\TestDemo\Step\GoogleSearchSteps.cs:line 24
at lambda_method(Closure , IContextManager )
at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStepMatch(BindingMatch match, Object[] arguments)
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStep(IContextManager contextManager, StepInstance stepInstance)
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnAfterLastStep()
at TechTalk.SpecFlow.TestRunner.CollectScenarioErrors()
at TestDemo.Feature.GoogleSearchFeature.ScenarioCleanup()
at TestDemo.Feature.GoogleSearchFeature.GoogleSearchWithSelenium() in D:\Aventis\TestDemo\TestDemo\Feature\GoogleSearch.feature:line 11
Result Message:
Test method TestDemo.Feature.GoogleSearchFeature.GoogleSearchWithSelenium threw exception:
System.ComponentModel.Win32Exception: The system cannot find the file specified
Result StandardOutput:
-> Using app.config
Given I have navigated to Google search page
-> error: The system cannot find the file specified
And I see the page fully loaded
-> skipped because of previous errors
When I type keyword as
— table step argument —
| keyword |
| Selenium |
-> skipped because of previous errors
Then I should see the result for keyword
— table step argument —
| keyword |
| Selenium |
-> skipped because of previous errors
Do you have any idea what could be the crux of this issue?
Hello Karthik
I am getting following error when I install Baseclass.Contrib.SpecFlow.Selenium.NUnit .
Severity Code Description Project File Line Suppression State
Error CS1029 #error: ‘Generation error: Method ‘Initialize’ in type ‘Baseclass.Contrib.SpecFlow.Selenium.NUnit.GeneratorPlugin’ from assembly ‘Baseclass.Contrib.SpecFlow.Selenium.NUnit.SpecFlowPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ does not have an implementation.’ BDDSfecFlowSelenium C:\Users\Techjini\documents\visual studio 2015\Projects\BDDSfecFlowSelenium\BDDSfecFlowSelenium\features\GoogleSearch.feature.cs 1 Active
I try to run two feature files parallel, using Spec flow With NUnit.
HTML report generated successfully but both the feature logs is updating to 1st feature scenario.
Example: In report it is showing
Feature Execution Details
Feature: Login
Scenario
Login user as Administrator [show]
Feature: User Form
Scenario
User Details form entry verification
If you observed above execution log is displaying only for Feature: Login, Scenario: Login User As Administrator [show].
if i try to click on Show link it is showing log of both feature file scenarios.
How can i achieve logs individually while running on parallel-execution.
I want to know if there is any way to implement following scenario in specflow+excel with selenium C#. Please let me know if there any solution with specflow+excel.
Scenario:
suppose i have 2 users and each user log in successfully with their credentials.
After login they can enter multiple records for a single form from an excel file as feature.
if one test depends on other test output, how to handel this.
if TestA passes, then only TestB should execute. How to control this in Parallel execution
I’m getting an error whenever I tried to execute my code using the above video tutorial. Here is the stack trace:
Test Name: GoogleSearchForSelenium on Chrome
Test FullName: SpecIntro.Features.GoogleSearchFeature.GoogleSearchForSelenium on Chrome
Test Source: C:\Users\yours\source\repos\SpecIntro\SpecIntro\Features\GoogleSearch.feature : line 6
Test Outcome: Failed
Test Duration: 0:00:00.0000001
Result Message: Invalid signature for SetUp or TearDown method: GoogleSearchForSelenium
Just loved the way you teach! Very complete and very detailed. You really explain every other concept very clearly which helps beginners understand them very well.
Thanks for having this blog created!
Glad you like it !!!
Thanks for reading and keep reading.
Thanks
Karthik
Got an Idea how BDD and Specflow handshake with selenium,
food for thought! Thanks karthi for sharing this video.
Glad that you have an idea now Abdul !!!
Thanks,
Karthik KK
I am always getting an exception while running on Chrome:
Autofac.Core.DependencyResolutionException : An exception was thrown while invoking the constructor ‘Void .ctor()’ on type ‘ChromeDriver’. —> unknown error: unrecognized Blink revision: 3b3c00f2d95c45cca18ab944acced413fb759311
(Driver info: chromedriver=2.10.267521,platform=Windows NT 6.3 x86_64)
i even tried on 2-3 different machines and same issue was observed. Do you have any idea what could be the crux of this issue?
Can you try to use latest Chrome driver or chrome browser.
Test Name: GoogleSearchForExecuteAutomation on Chrome
Test Outcome: Not Run
Result Message:
No matching step definition found for one or more steps.
using System;
using TechTalk.SpecFlow;
namespace MyNamespace
{
[Binding]
public class StepDefinitions
{
[Given(@”I have navigated to Google Page”)]
public void GivenIHaveNavigatedToGooglePage()
{
ScenarioContext.Current.Pending();
}
[Then(@”I should see the result for keyboard”)]
public void ThenIShouldSeeTheResultForKeyboard(Table table)
{
ScenarioContext.Current.Pending();
}
}
}
Result StandardOutput:
Given I have navigated to Google Page
-> No matching step definition found for the step. Use the following code to create one:
[Given(@”I have navigated to Google Page”)]
public void GivenIHaveNavigatedToGooglePage()
{
ScenarioContext.Current.Pending();
}
And I see the Google page fully loaded
-> skipped because of previous errors
When I type search keyword as
— table step argument —
| Keyword |
| execute automation |
-> skipped because of previous errors
Then I should see the result for keyboard
— table step argument —
| Keyword |
| Execute Automation |
-> No matching step definition found for the step. Use the following code to create one:
[Then(@”I should see the result for keyboard”)]
public void ThenIShouldSeeTheResultForKeyboard(Table table)
{
ScenarioContext.Current.Pending();
}
I truly love your blog.. Excellent colors & theme. Did you create this site yourself? Please reply back as I’m hoping to create my own blog and would love to learn where you got this from or exactly what the theme is called. Many thanks!
Hi karthik,
I am not able to see my test in test explorer. I am performing the same steps as you performed
Hi Karthik,
On performing the above steps, my build is successful but the test is not seen in test explorer. Any input from your end to resolve this?
Got it in one of your videos..Thanks a ton
I am always getting an exception while running on Firefox:
Test Name: GoogleSearchWithSelenium
Test FullName: TestDemo.Feature.GoogleSearchFeature.GoogleSearchWithSelenium
Test Source: D:\Aventis\TestDemo\TestDemo\Feature\GoogleSearch.feature : line 5
Test Outcome: Failed
Test Duration: 0:00:00.2892561
Result StackTrace:
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at OpenQA.Selenium.Firefox.Internal.Executable.LocateFirefoxBinaryFromPlatform()
at OpenQA.Selenium.Firefox.Internal.Executable..ctor(String userSpecifiedBinaryPath)
at OpenQA.Selenium.Firefox.FirefoxBinary..ctor(String pathToFirefoxBinary)
at OpenQA.Selenium.Firefox.FirefoxDriver..ctor()
at TestDemo.Step.GoogleSearchSteps.GivenIHaveNavigatedToGoogleSearchPage() in D:\Aventis\TestDemo\TestDemo\Step\GoogleSearchSteps.cs:line 24
at lambda_method(Closure , IContextManager )
at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStepMatch(BindingMatch match, Object[] arguments)
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStep(IContextManager contextManager, StepInstance stepInstance)
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnAfterLastStep()
at TechTalk.SpecFlow.TestRunner.CollectScenarioErrors()
at TestDemo.Feature.GoogleSearchFeature.ScenarioCleanup()
at TestDemo.Feature.GoogleSearchFeature.GoogleSearchWithSelenium() in D:\Aventis\TestDemo\TestDemo\Feature\GoogleSearch.feature:line 11
Result Message:
Test method TestDemo.Feature.GoogleSearchFeature.GoogleSearchWithSelenium threw exception:
System.ComponentModel.Win32Exception: The system cannot find the file specified
Result StandardOutput:
-> Using app.config
Given I have navigated to Google search page
-> error: The system cannot find the file specified
And I see the page fully loaded
-> skipped because of previous errors
When I type keyword as
— table step argument —
| keyword |
| Selenium |
-> skipped because of previous errors
Then I should see the result for keyword
— table step argument —
| keyword |
| Selenium |
-> skipped because of previous errors
Do you have any idea what could be the crux of this issue?
Hello Karthik
I am getting following error when I install Baseclass.Contrib.SpecFlow.Selenium.NUnit .
Severity Code Description Project File Line Suppression State
Error CS1029 #error: ‘Generation error: Method ‘Initialize’ in type ‘Baseclass.Contrib.SpecFlow.Selenium.NUnit.GeneratorPlugin’ from assembly ‘Baseclass.Contrib.SpecFlow.Selenium.NUnit.SpecFlowPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ does not have an implementation.’ BDDSfecFlowSelenium C:\Users\Techjini\documents\visual studio 2015\Projects\BDDSfecFlowSelenium\BDDSfecFlowSelenium\features\GoogleSearch.feature.cs 1 Active
This package is no more supported by latest version of NUnit and Specflow, so please dont use it.
Thanks,
Karthik KK
Hi Karthik,
I try to run two feature files parallel, using Spec flow With NUnit.
HTML report generated successfully but both the feature logs is updating to 1st feature scenario.
Example: In report it is showing
Feature Execution Details
Feature: Login
Scenario
Login user as Administrator [show]
Feature: User Form
Scenario
User Details form entry verification
If you observed above execution log is displaying only for Feature: Login, Scenario: Login User As Administrator [show].
if i try to click on Show link it is showing log of both feature file scenarios.
How can i achieve logs individually while running on parallel-execution.
I want to know if there is any way to implement following scenario in specflow+excel with selenium C#. Please let me know if there any solution with specflow+excel.
Scenario:
suppose i have 2 users and each user log in successfully with their credentials.
After login they can enter multiple records for a single form from an excel file as feature.
if one test depends on other test output, how to handel this.
if TestA passes, then only TestB should execute. How to control this in Parallel execution
Hello Karthik,
I’m getting an error whenever I tried to execute my code using the above video tutorial. Here is the stack trace:
Test Name: GoogleSearchForSelenium on Chrome
Test FullName: SpecIntro.Features.GoogleSearchFeature.GoogleSearchForSelenium on Chrome
Test Source: C:\Users\yours\source\repos\SpecIntro\SpecIntro\Features\GoogleSearch.feature : line 6
Test Outcome: Failed
Test Duration: 0:00:00.0000001
Result Message: Invalid signature for SetUp or TearDown method: GoogleSearchForSelenium