Starting the login phase to the outline of the scenario in cucumbers

I am using a cucumber with webrat / mechanize to test a PHP site, and I am trying to improve the speed of the tests by avoiding unnecessary steps.

I want to use a script scheme to check access to many pages depending on the user who has registered:

Scenario Outline: Check page access is secure Given I am logged in as "<user>" And I am on <page> Then I should see "<message>" Examples: |user |page |message | |admin |home page |Welcome to my site | |admin |admin page|Site administration | |editor|home page |Welcome to my site | |editor|admin page|Access denied | |guest |home page |Please login | |guest |admin page|Access denied | ... 

This works, but given that I have 10 roles and hundreds of pages to check, there is a lot of overhead when starting the login step every time the outline works.

I am wondering if there is a way to start the login step once for each role and then visit each page in turn without having to log in every time. ie run "login, visit 1, visit 2, visit 3" instead of "login, visit 1, log in, visit 2, log in, visit 3".

I tried using interceptors and Background, but can't find an approach that works. Is it possible?

+6
cucumber
source share
2 answers

You can implement the Given step only to log in once for each role:

 # lazily log in each role as needed, and keep the login in a hash table $logins = Hash.new do |_logins, role| _logins[role] = do_expensive_login(role) end Given /^I am logged in as "([^"]+)"$/ |role| @login = $logins[role] end 

Of course, if future steps can change the login state or change the world in such a way that the login is no longer valid, this can disable you, so go carefully.

0
source share

Instead of posting all the information about what is available / protected in this function, consider placing them in the def step (it would be better to use definitions in your application, but this is not easy if your application is not in the process)

If you can live with a function that is abstract, like

 Given I am an admin Then I should be able to access admin pages 

Then you can do all the work much more efficiently in defs steps

Below is just a sketch of the code to give an idea of ​​what you can do ...

 # step def module AccessHelper AdminPages = { {page: ..., msg: ... ... } def login_as ... ; end def correct_message? msg ...; end def check_admin_access_for user @errors = [] login_as @I AdminPages.each do |page| visit page[:path] errors << page unless correct_message? end end end World(AccessHelper) Then "I should be able to access admin pages" do check_admin_access_for @I @errors.should be_empty end 

You can, of course, expand this using the full power of the ruby ​​to suit your special needs. The basic idea is that you can always take several actions of a cucumber and abstract them into one action of a cucumber.

Hope this is helpful

0
source share

All Articles