Search code examples
seleniumbddspecflow

Specifying login in Feature Setup of SpecFlow and Selenium


I have set up SpecFlow to execute a number of Selenium tests to test a website. This all works fine!

I am just considering if there might be room for some optimization on the structuring of the SpecFlow features.

All the scenarios in a specific feature should use the same login. This is something I have hardcoded in the StepDefinition using the [BeforeScenario()] hook at the moment, since I don't really wan't pollute the scenarios with login info. It isn't relevant for the test.

But at the same time, I would like to remove the hardcoded part, and move this into my feature.

My question is two part.

  1. Can I specify login credentials in my feature description. Sort of like a Given:

    Feature: As a user I want to be able to see my ongoing orders, and interact with them.
    Given I am logged in with username abc and password xyz
    
    Scenario: See list of ongoing order
    Given I place an order
    When I navigate to MyOrders page
    Then I can see at least one order in the list
    
  2. Is this good practice?

    Does it make sense to do it like this, on the feature level I mean. The scenarios are not dependent on a specific order, and they would execute faster if I didn't need to log in for each scenario.

Thanks for your input.


Solution

  • When I first read your post, I thought of Dan North: Who's domain is it anyway? and that influences me to think that we should be trying to write our tests so that they stick to a single knowledge domain. As you talk about specific users and navigation and orders in a list, well its very like the example he gives in that your specification is crossing domains. This kind of leads me towards almost making your specification less specific, i.e. its not about navigating and checking a list, do you have an order or not!

    Scenario: See list of ongoing order Given I am logged in with username abc and password xyz And I place an order Then should have at least one order

    I wanted to give you another example link, to a post I cant find, which talks about the benefits that a team has got by actually defining some example users and what value that gives their testing. So in your business domain you might have a user called Jack who will have placed a large order in the system, and another prospective client Jill with no orders. In this way tests such as

    Given I am Jack
    When I search for my orders
    Then I will find 1
    
    Given I am Jill
    When I search for my orders
    Then I will find 0
    

    can guarantee that you are only testing your Search functionality, and less about getting the setup in place.

    I'd also suggest that you have a look at Liz Keogh: Acceptance Criteria vs Scenarios who asserts that more general broad definitions are less valuable than very specific examples. (It is Specification By Example after all :-) )

    So to answer your question, I think having the specified user is a good thing, but doing it how you are doing it is heading a very complicated route.