Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specflow Feature files with same steps causing multiple browser instances to launch

Tags:

c#

specflow

I have at least 3 .feature files in my C# Specflow tests project in which I have the step, for instance:

Given I am at the Home Page

When I first wrote the step in the file Feateure1.feature and created the step method, I placed it in a step file, let's say, Steps1.cs, which inherits from a base class that initializes a FirefoxDriver. All my StepsXXXX.cs classes inherit from this base class.

Then, I wrote Feature2.feature, which also has a step Given I am at the Home Page. And the step was automaticaly bound to the one in Steps1.cs

'Till now, no problem. That's pretty much what I wanted - to have reusable steps throughout the test project. But the problem is, whenever I'm running a scenario that has steps in diferent StepsXXXX files, I get various browser instances running.

======

I'm pretty sure this is due to the fact that My StepsXXXX (binding classes) all inherit from this base class that has a IWebDriver of its own, and when the step is called, everything else (including the before/after scenario methods) is called. But I can't figure out how to work around this.

I still want reusable steps. I tried to put these steps in the base class, but it did not work. I thought of changing the bindings too, but specflow uses meaningfull strings to do so, and I don't want to change them to misleading strings.

Has anyone stumbled across this? Any help is really appreciated.

like image 992
Francisco Avatar asked Mar 19 '13 17:03

Francisco


People also ask

Can a feature file have multiple features?

It is not possible to have multiple feature inside single feature file. If you create multiple feature inside single feature file, you will get Gherkin Parser exception while running cucumber scenarios. So the answer is NO.

Which of the following files hold all scenarios in SpecFlow?

Use template files SpecFlow can then run tests using the processed files, and execute scenarios for all three currencies.

How do I update SpecFlow feature file?

when right-clicking on a feature file and selecting Run Custom Tool , make sure the SpecFlow extension is installed and enabled. To enable the extension in Visual Studio, select Tools | Extensions and Updates…, select the “SpecFlow for Visual Studio” extension, then select Enable.

How do I run a feature file in SpecFlow?

Note: Feature File can also be run by Right-clicking in the feature and choosing Run SpecFlow Scenarios.


1 Answers

You can use Scoped bindings using [Scope(Tag = "mytag", Feature = "feature title", Scenario = "scenario title")] to referred on specific scenario or feateure like this:

Feature: Feateure1

Scenario: demo
Given I am at the Home Page
When ....

[Binding, Scope(Feature = "Feateure1")]
public class Steps1{

 [Given(@"Given I am at the Home Page")]
 public void GivenIAmAtTheHomePage(){
  {  }
}

Feature: Feateure2

Scenario: demo
Given I am at the Home Page
When ....
...

[Binding,Scope(Feature = "Feateure2")]
public class Steps2{

 [Given(@"Given I am at the Home Page")]
 public void GivenIAmAtTheHomePage(){
 {  }

}
like image 125
TotPeRo Avatar answered Sep 27 '22 19:09

TotPeRo