Search code examples
c#sessionseleniumbddspecflow

How to retain single session for the test in Specflow with specflow+excel


When login to google system, I should be able to naviagate to all menus

@Login
Scenario: Successful Login to Google
 Given Google system launched
 When Login with XYZ
 Then Google main page displayed
@menuNavigate
Scenario Outline: Navigate to page from google menu 
    Given User navigates to <Tab> using the tabs
    When The Page is completely loaded with <Header>
    Then the result should be that a control with controlId <ControlId> is displayed on the screen

@source:menuNavigate.xlsx
Scenarios:
| Tab | Header  | ControlId |

@Login test is successful. When @menuNavigate test is executed, it launches separate session.

Is there any way to continue the rest of the test with same browser session


Solution

  • You can probably do this on a per feature basis (ie you can reuse the same browser in all of the scenarios in a feature) by creating your browser instance in the [BeforeFeature] hook and then using this in each scenario. As long as your [beforeFeature and [BeforeScenario] hooks are in the same class then you can declare a static field which holds your browser instance and populate this in the [BeforeFeature] hook and reuse the field in the [BeforeScenario] hook.

    However consider whether you really want to do this. What will happen if your scenarios are run in a different order? Nothing guarantees test execution order. Or in parallel?

    I highly doubt that this approach will scale well in the long term.

    If you want to have some setup in common then create a background or create a step to do the setup and call this from each scenario. You can even call other steps from this setup step if you want the setup step to perform some actions which are already used in another test.