Search code examples
c#seleniumcucumberspecflowgherkin

Grouping Scenarios By Login - Specflow / Cucumber / Gherkin


I am currently setting up a new UI Automation framework for a fairly complex application which has a number of input fields which require automating using Specflow & Selenium.

As part of these screens, we have different UI conditions which apply, based off a number of scenarios, based on user and database setup. As there are a number of steps involved I don't want to have to sign in each time I need the same login e.g. Scenarios 1-10 can use the same session, but Scenario 11 - 14 need a different login but use the same page. Is it possible to do this, or would this have to be separated out into different features - If so how can these be grouped when run against a report or in Visual Studio?

Currently I have a feature file created like below (I have stripped out data with ... for this example):

Feature: Some Page
description of page

Given a user signs into ... as a user with permissions to the '...' screen
    And a user navigates to the ... screen
    Then present ...

Scenario: 02 - Customer Name Populated
    Given the user enters the customer '...'
    Then the selected record is entered into the Customer Field
    And '...' is displayed in the Customer Name field
    
Scenario: 03 - Default Detail Line Values Are Populated
    Given a user has entered customer record '...' into the transaction screen
    Then a default detail line is populated with the following values
    | Table Headings ...|
|Table Values ...|

Solution

  • Being logged in is a prerequisite for certain scenarios. From a behavior driven development standpoint I would expect to see Given the user is logged in as "X" in those scenarios. This seems to be more of an optimization task.

    Modify the step that logs the user in to detect if the user is already logged in. This would likely involve Selenium detecting a certain piece of text or a certain link on the page. For instance, if your application has a small greeting for the user in the main menu (e.g. "You are logged in as: X" or "Greetings, X!") then have selenium detect the presence of that element.

    If present, do nothing. If not present, click the "Log Off" button, and then proceed to log in. Just beware of race conditions between Selenium and your test. If you find you need to keep waiting for certain elements to be present, measure the time it takes to do this. It might not be any faster than logging out and back in again with each scenario.

    If this is not acceptable, you can always try before/after scenario hooks, or a before/after feature hook. You need to be careful, though. If you run tests concurrently you need to take care to make sure you store info in a thread safe manner.