1

I just started using SpecFlow along with Selenium & N Unit.

I have a basic question, maybe I know the answer to it, but want to get it confirmed.

Consider, there are two features - Register and Add new transaction. Two separate features with their respective step definitions. How do I share the IWebDriver element across both the features?

I do not want to launch a new browser again and add a transaction. I want to execute both as a flow.

My thoughts are this functionality is not allowed using SpecFlow as basic use of feature-based testing is violated as trying to run two features in the same session. Will context injection assist help in this case?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ReuseAutomator
  • 410
  • 3
  • 14
  • I did refer this link, but more info is always appreciated. http://stackoverflow.com/questions/2881685/in-specflow-how-can-i-share-data-between-steps-features – ReuseAutomator Jun 11 '15 at 08:38
  • if it is just for Webdriver you can try a singleton class to access single instance in all steps irrespective of feature. – Kavan Jun 11 '15 at 09:17

1 Answers1

3

What you want to do is a bad idea. you should start a new browser session for each feature, IMHO.

There is no guarantee in what order your tests will execute, this will be decided by the test runner, so you might get Feature2 running before Feature1.

In fact your scenarios should be independent as well.

you can share the webdriver instance between steps as in this answer but you should use specflows other features like scenario Background to do setup, or definign steps which do your comman setup.

EDIT

We have a similar issue with some of our tests and this is what we do:

We create a sceanrio for the first step

Feature: thing 1 is done

Scenario: Do step 1
    Given we set things for step 1 up
    When we execute step 1
    Then some result of step one should be verified

Then we do one for step 2 (which lets assume relies on step 1)

Feature: thing 2 is processed

Scenario: Do step 2
    Given we have done step 1
    And we set things for step 2 up
    When we execute step 2
    Then some result of step 2 should be verified

This first step Given we have done step 1

is a step that calls all the steps of feature 1:

[Given("we have done step 1")]
public void GivenWeHaveDoneStep1()
{
     Given("we set things for step 1 up");
     When("we execute step 1");
     Then("some result of step one should be verified");
}

Then if we have step 3 we do this:

Feature: thing 3 happens

Scenario: Do step 3
    Given we have done step 2
    And we set things for step 3 up
    When we execute step 3
    Then some result of step 3 should be verified

Again the Given we have done step 2 is a composite step that calls all the steps in the scenarion for step 2 (and hence all the steps for step 1)

[Given("we have done step 2")]
public void GivenWeHaveDoneStep2()
{
     Given("we have done step 1");
     Given("we set things for step 2 up");
     When("we execute step 2");
     Then("some result of step 2 should be verified");
}

We repeat this process so that when we get to step 5, it is running all the steps in the correct order. Sometimes one we get to step 5 we @ignore the previous 4 steps as they will all be called by step 5 anyway.

Community
  • 1
  • 1
Sam Holder
  • 32,535
  • 13
  • 101
  • 181
  • @Sam-I see what you meant by Features run in any order of choice. Is there a way to get this working in order. I plan to run a smoke flow-a set of 5 features in specific order. Will having all the smoke tests in specific folder & running them by numbering solve it. I know IMHO its not a great idea. – ReuseAutomator Jun 15 '15 at 02:22
  • I want to run 5th feature only after 3,4 is completed. But observing 5 running prior to those. – ReuseAutomator Jun 15 '15 at 04:35
  • Tried using your code for WebDriver session sharing between features in OrderedTest for MsTest, does not work. Any idea?It works in NUnit. – ReuseAutomator Jun 17 '15 at 08:07
  • I'm not sure what you mean by OrderedTest for MSTest. I'm not sure that this is supported by specflow – Sam Holder Jun 17 '15 at 08:09
  • MsTest has an option to run dependent feature first prior to other features, we can set the order of execution of features basically using OrderTests . This is to overcome the issue of features executing in random order in NUnit. But issue was the webdriver session share code did not work on MsTest. – ReuseAutomator Jun 17 '15 at 09:58
  • how are you telling specflow to generate tests using this feature of MSTest? I'll update my answer with how I would solve this problem. – Sam Holder Jun 17 '15 at 10:01
  • All the feature or stepfiles to be executed are saved in orderedtest file in order of execution. We just call this file to be run using MsTest.exe – ReuseAutomator Jun 17 '15 at 10:31
  • 1
    Please see my update, it might be a better solution than trying to force the tests to be ordered, as it will be independent of test provider. – Sam Holder Jun 17 '15 at 10:53
  • Good way, did not know that could be done. Can you direct me where I can get examples of these specflow advanced features. – ReuseAutomator Jun 22 '15 at 11:46