Cucumber, repeating login steps in all scenarios Cucumber, repeating login steps in all scenarios selenium selenium

Cucumber, repeating login steps in all scenarios


Every scenario that requires a user to be logged in will need to have the user log in. This is part of the cost of running at the integration level. However log in should not be an expensive time consuming operation, you only have to fill in two fields and submit them. It should take < 100ms to process the login.

Now for unit testing this time is huge, but for an integration test, which by its nature involves a much bigger stack and usually simulated human interaction (otherwise why do you need your user to login) this time is a relatively small component of the overall scenario run time.

Because Cucumber works at the integration level it is best not to use it as a testing tool, rather it should be used as a tool to drive development. Instead of writing thousands of small assertions (like you might when unit testing) you need to write fewer larger scenarios i.e. each scenario needs to do more. As each scenario is doing more the need for each scenario to be completely independent of any other scenario increases (the more you do, the more likely you are to be side effects on other things that are done). Sharing sessions and trying to avoid resetting the db and session between each scenario turns out to be a false optimization that creates more problems than it solves.

Its perfectly fine for a scenario to do alot before you get to its when. For example imagine the following e-commerce scenario.

Scenario: Re-order favorite Given I have a favorite order When I view my orders And I re-order my favorite order Then I should be taken to the checkout And my favourite items should be in the basket

Now clearly an awful lot of stuff needs to happen before I can re-order e.g.

  • I need to register
  • I need to make at least one previous order
  • I need to choose a favorite order

and of course there are lots of other things like

  • there need to be products to be ordered

All of this means that this scenario will take time to run, but thats OK because you are getting alot of functionality from it. (when I wrote something similar a long time ago, the scenario took 1-2 seconds to run). The login time for this sort of scenario is trivial compared to the time required to do the rest of the setup.


I know nothing about Selenium with cucumber (but i like cucumber :-)

I'm from Selenium for Python. There I can do the following things:

from selenium import webdriverprofile = webdriver.FirefoxProfile(your_path_to_local_firefox_profile)# like C:/Users/<USERNAME>/AppData/Roaming/Mozilla/Firefox/Profiles/<PROFILE_FOLDER>browser = webdriver.Firefox(profile)

So, now with "[WIN] + [R]" -> Run -> "firefox.exe -p" I can create an extra profile for Selenium to use it in the code above, so I can use Firefox as well start with the profile on a trial basis. ALSO If your website with login you want to automate, cookies & cache etc. supports, then it could be that you do not have to log in via the firefox profile every time, but that the Firefox starting each time automatically logs in because he stored the login data.

I do not know if that helps, but I wanted to tell you.