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

c#, specflow

Solution

The problem is that SpecFlow bindings don't respect inheritance. All custom attributes are considered global, and so all SpecFlow does is search for a list of classes with a [Binding]then build up a dictionary for all the [Given]/[When]/[Then]s so that it can evaluate them for a best match. It will then create an instance of the class (if it hasn't already done so) and call the method on it.

As a result your simple cases all stay in the Steps1 class, because its the first perfect match. Your more complicated cases start instantiating more classes, hence multiple browsers, And your attempt to refactor won't work because your abstract base class doesn't have a [Binding] on it.

I'd probably start by flattening all your step class hierarchy, into one big AllSteps.cs class. This may seem counter-productive, but all you are really doing is arranging the code just how the current bindings appear to your SpecFlow features. This way you can start to refactor out the overlaps between the different GWT bindings.

At the moment your bindings are arranged around the scenarios. What you will need to do is refactor them around your functionality. Have a read of Whose Domain is it anyway? before you start and this will probably give you some good ideas. Then have a look at Sharing-Data-between-Bindings on the SpecFlow documentation to work out how to link between your new steps classes.

Problem

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.

Original source