How to only log in once before all features are run

2,316 views
Skip to first unread message

Jesper Rønn-Jensen

unread,
Oct 10, 2012, 6:59:40 AM10/10/12
to cu...@googlegroups.com
For reasons which have been discussed previously in this forum, there is not a @BeforeAll hook or similar where you can run steps once before all other features.
Instead, I should just put code for it in env.rb.

Before I describe my problem, have a look at my setup:

I have a step like this defined in all feature files:

  Background: 
    Given I log in as testuser10


The step uses capybara/phantomjs:
Given /^I log in as (\w+)$/ do |username|
  step "I am not logged in"
    visit('/login')
    fill_in("username", :with => username)
    fill_in("password", :with => LOGIN_PW)
    click_button("Logon")
    page.should  have_content("Authentication successful")
  end

And we use cucumber to verify legacy web-app in several environments. For some of these environments, logging in takes up to 5 seconds. In case I could run this step just once, I could shave off 2-3 minutes in our current suite.

My intuition was to run this code directly in env.rb

    step "I log in as testuser10"
'

But it fails:
undefined method `step' for main:Object (NoMethodError)
/home/xleap/workspace/core/gitclone/webchannel/test-suite/cucumber/features/support/env.rb:107
/usr/lib/ruby/gems/1.8/gems/cucumber-1.2.1/bin/../lib/cucumber/rb_support/rb_language.rb:129:in `load'
/usr/lib/ruby/gems/1.8/gems/cucumber-1.2.1/bin/../lib/cucumber/rb_support/rb_language.rb:129:in `load_code_file'
/usr/lib/ruby/gems/1.8/gems/cucumber-1.2.1/bin/../lib/cucumber/runtime/support_code.rb:171:in `load_file'
/usr/lib/ruby/gems/1.8/gems/cucumber-1.2.1/bin/../lib/cucumber/runtime/support_code.rb:83:in `load_files!'
/usr/lib/ruby/gems/1.8/gems/cucumber-1.2.1/bin/../lib/cucumber/runtime/support_code.rb:82:in `each'
/usr/lib/ruby/gems/1.8/gems/cucumber-1.2.1/bin/../lib/cucumber/runtime/support_code.rb:82:in `load_files!'
/usr/lib/ruby/gems/1.8/gems/cucumber-1.2.1/bin/../lib/cucumber/runtime.rb:175:in `load_step_definitions'
/usr/lib/ruby/gems/1.8/gems/cucumber-1.2.1/bin/../lib/cucumber/runtime.rb:40:in `run!'
/usr/lib/ruby/gems/1.8/gems/cucumber-1.2.1/bin/../lib/cucumber/cli/main.rb:43:in `execute!'
/usr/lib/ruby/gems/1.8/gems/cucumber-1.2.1/bin/../lib/cucumber/cli/main.rb:20:in `execute'
/usr/lib/ruby/gems/1.8/gems/cucumber-1.2.1/bin/cucumber:14
/usr/bin/cucumber:19:in `load'
/usr/bin/cucumber:19


Now my question is: How do I run this code from env.rb?




Thanks!
/Jesper Rønn-Jensen

aslak hellesoy

unread,
Oct 10, 2012, 7:16:33 AM10/10/12
to cu...@googlegroups.com
On Wed, Oct 10, 2012 at 11:59 AM, Jesper Rønn-Jensen <jesp...@gmail.com> wrote:
> For reasons which have been discussed previously in this forum, there is not
> a @BeforeAll hook or similar where you can run steps once before all other
> features.
> Instead, I should just put code for it in env.rb.
>

Another way to implement a Before All hook is to use a global variable
in a regular hook:

Before do
if !$dunit
# do it
$dunit = true
end
end

Have you tried that?

Aslak
> --
> -- Rules --
>
> 1) Please prefix the subject with [Ruby], [JVM] or [JS].
> 2) Please use interleaved answers
> http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
> 3) If you have a question, don't reply to an existing message. Start a new
> topic instead.
>
> You received this message because you are subscribed to the Google Groups
> Cukes group. To post to this group, send email to cu...@googlegroups.com. To
> unsubscribe from this group, send email to
> cukes+un...@googlegroups.com. For more options, visit this group at
> https://groups.google.com/d/forum/cukes?hl=en
>
>

Jesper Rønn-Jensen

unread,
Oct 10, 2012, 7:48:27 AM10/10/12
to cu...@googlegroups.com


On Wednesday, October 10, 2012 1:16:57 PM UTC+2, Aslak Hellesøy wrote:
 
Another way to implement a Before All hook is to use a global variable
in a regular hook:

Before do
  if !$dunit
    # do it
    $dunit = true
  end
end


This works. Absolutely awesome. Oddly enough I did not think of that. I know you have previously been against a @BeforeAll hook (you wrote that you were against adding code that did nothing).

This example, however, proves that a @BeforeAll hook will be intuitive and useful.

In case you agree, we could add the hook? Until then, I will add this example to the "hooks" wiki page (with your permission)..


thanks for your quick and concise reply!

/Jesper

 

aslak hellesoy

unread,
Oct 10, 2012, 8:08:13 AM10/10/12
to cu...@googlegroups.com
Please go ahead and add it to the Wiki.

I'm still against adding explicit support for this in Cucumber, for
the simple reason that anyone can implement it with 5 lines of code.
Documenting the pattern seems enough.

Aslak

>
> thanks for your quick and concise reply!
>
> /Jesper
>
>
>

Jesper Rønn-Jensen

unread,
Oct 10, 2012, 8:36:20 AM10/10/12
to cu...@googlegroups.com


Aslak Hellesøy wrote:
Please go ahead and add it to the Wiki.


 
I'm still against adding explicit support for this in Cucumber, for
the simple reason that anyone can implement it with 5 lines of code.
Documenting the pattern seems enough.

Aslak


Fair enough. In case i sneak in the word BeforeAll hook, this is probably enough for people to find it :)

/Jesper

 
Reply all
Reply to author
Forward
0 new messages