0

So we are using SpecFlow (C# with Selenium WebDriver) in order to do functional testing.

I am still pretty new to using SpecFlow and Selenium. My mentor has told me that they want scenarios written in a way that only has one when and then (you set up your test, you my perform an action or two and then assert).

So I have a Scenario Outline, like so:

Scenario Outline: Outline label Given I am on the proper page When I apply filter "" with "" And I click filter Then the data should be filtered Examples: | filter | params | | Date | Today | | Name | Some Name | | ... [20+ more] ... |

However my mentor said it would be best to instead to do something like this: Scenario: Scenario label Given I am on the proper page When I apply and filter with | filter | params | | Date | Today | | Name | Some Name | | ... [20+ more] ... | Then the data should be filtered

The point is so that the browser doesn't have to close and re-open for each of the filter tests. Instead the filter would be cleared before the next.

However, I am unsure of how best to approach this task in the best way. Since each of the filters would need to be verified before the next is done, where would the assertion be done? Within the "When I apply and filter with" and then the "Then the data should be filtered" just checks to make sure nothing failed? That seems a bit odd as the "When" case should stop as soon as one fails.

Alternatively the "When" could just store everything in the table and the "Then" would apply the filters and do the assertion -- but it then ceases to be a then as it is doing something more than asserting the expected conditions have been met.

I've been browsing around for trying to similar problems with solutions, but I haven't found anything.

I guess my basic problem here is how do I perform multiple, different yet similar, steps without having to open/close the browser for each individual example (as occurs with the Scenario Outline)? Is this even good practice or possible (without having to do multiple when/thens in a single scenario)? Is doing multiple when/thens (e.g. When ... Then ... When ... Then ...) a bad idea as well?

Thanks in advance! I've been trying to research related issues for a couple days but have found nothing that helps.

ian.aldrighetti
  • 378
  • 3
  • 11

3 Answers3

3

A good option to avoid having to close/re-open the browser between tests is to look at Hooks in SpecFlow. You can use a [BeforeFeature] or [BeforeTestRun] hook (or more variations) to launch the browser once, then run numerous tests before closing the browser at the end of the tests.

If you need to get your system back to a certain state after/before each test, you could have one [BeforeFeature]/[BeforeTestRun] hook to launch the browser at the beginning, then one or more [AfterScenario] / [BeforeScenario] hooks to clean up any scenario-specific data so that you keep each test clean but avoid closing the browser to clean up your session/cookies/etc. You could even clear the filter in an [AfterScenario] hook with WebDriver if you needed to, before the next test starts.

Scott Zetrouer
  • 211
  • 2
  • 6
  • Thanks! As I was still trying to find an answer (and waiting for other input here), this is exactly what I did. I made a ContinuousDriver tag, as by default the project seems to close and re-open the WebDriver. By not closing it the tests in the Scenario Outline went from ~45 second average down to ~22 seconds. I will need to look into clearing cookies and the like next. – ian.aldrighetti Feb 18 '15 at 17:05
  • I will look into clearing cookies and the like as well, that will help as well. Might help simplify the continuous driver hooks I have going. – ian.aldrighetti Feb 18 '15 at 17:08
  • ian - how did you implement the ContinuousDriver tag? I tried to do something similar with resolving the driver and launching the browser in a BeforeTestRun hook but can't get the IObjectContainer as the hook method must be static. – danio Jul 16 '15 at 09:36
0

This is how I solved the issue... SpecFlow Browser Document

You'll notice that I used a singleton dictionary to store the open browser instances for the life of the feature. Then in the AfterFeature hook, I go through and close all the open browsers.

Brantley Blanchard
  • 1,208
  • 3
  • 14
  • 23
-2


Here are my thoughts on the subject,

First of all, it seems more like a Specflow question, rather then a Selenium one.
Do you know the difference between Scenario and Scenario Outline?
Scenario is a single test. It can have table inside, but that just to organize that specific step. You can take the arguments out of the sentence and put them into a table which gives you somewhat more dynamic step.
Instead of:

When I apply filter filter_a with kuku

You can write:

When I apply filter with
|filter | param |
|filter_a | kuku |

On the other hand, Scenario Outline is a template for different tests with the same format, so each table row is actually a different set of parameters for each scenario.
Here is one example from many others in Google:
How can I repeat steps in a Specflow Scenario Outline

So that's about Scenario and Scenarios Outline.

How do you handle closing of the webdriver between the tests is your choice. I guess that depends on the logic.
One thing I can tell about the validation - it would be much easier for you if you validate the filters in each step and not in the end because in the latter if the tests fails it would be harder to debug and find what caused the issue. Also if one of the filters fails there is no point to process farther, right? That's of course in case I understood your question and the table is just a set of parameters for one test.

Community
  • 1
  • 1
user3734429
  • 322
  • 1
  • 10