Domanda

We are applying BDD and using SpecFlow and Selenium. Acceptance criteria are specified in Gherkin.

I have a question regarding the Given clause.

An example might be...

  • Given I am on the order details page
  • When I submit the order
  • Then I see the order summary

So for the 'Given I am on the order details page' we have to be logged in and have an order ready to submit. But how do we get to that point?

If we already have the relevant existing SpecFlow steps we can use these (via Selenium) to get to the right point - something like this...

  • Given I am a registered user
  • And I am on the login page
  • And I log in using my registered username and password
  • And I create a new order
  • And I search for XYZ product
  • And I add the product to the order
  • And I select the order details page
  • When I submit the order
  • Then I see the order summary

We can even put all those steps in the step code under 'Given I am on the order details page'

However are we right to use Selenium to implement the Given? Or should we find some way to set all the relevant states/data in the session/database without going through the UI?

I suspect that there is no right or wrong here. I have to say it is working very well for us but we are always looking to improve our approach. So would be very interested to hear from anyone with real experience.

I hope Stack Overflow is the right forum for this (can't help but think it isn't - if so please don't shoot me down!)...

È stato utile?

Soluzione

I assume you are performing integration testing, so it makes sense to access the DB.

Selenium automates ui testing - I wouldn't use it to set up your data, because then changes to the ui will break tests that really only should rely on the initial valid order state.

Some of the steps might be required in order to execute your code - such as being logged in... but they aren't actually what you are testing in this test. Have you considered using the Background tag to set them up?

Feature: Order Details Page
As a customer ...
I want to ...
So that ...

Background: 
  #set up user authentication status
    Given I am logged in

  #set up order status to be consistent with this page
    And I have an order ready for submission

Scenario: I can submit my order
    ...etc...

Scenario: I can cancel my order
    ...etc...

With this pattern the complex "I am on the Orders Page" is broken into multiple background steps that set up a valid state, and the feature file now can refer to multiple actions on that page without duplication.

I think how you set up the data for your integration tests is up to you. But I feel like it is cleaner to construct a record in the correct state.

Avoid I log in using my registered username and password and instead just set up the authentication status directly with I am logged in. Set up the order similarly - don't rely on the production code that you aren't testing or your test will fail in the wrong circumstances.

You CAN re-use Given steps - but they shouldn't rely on production code.

Altri suggerimenti

You can call other steps from step definition

[Given(@"I am on the order details page ")]
public void GivenIAmOnTheOrderDetailsPage()
{
     Given("I am a registered user");
     And("I am on the login page");
     // so on
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top