9

I'm trying to write a test for a feature that relies on some session stored data and my scenario looks like this:

Scenario: Create offer
  Given I am on the start offer page
  When I select "Foo" from "bar"
  And I press "Go on"
  Then I should see "You are going to offer foo"

By using the debugger I found out, that the information is stored in the session correctly, but on every new request I get a fresh session.

Should'nt there be a working session for at least every scenario? Any ideas why this isn't the case?

Thanks in advance, Joe

Versions: Running on rails 2.3.10, cucumber 0.10.0, cucumber-rails 0.3.2, capybara 0.4.1.2

xijo
  • 4,366
  • 4
  • 23
  • 30
  • 1
    what driver do you use, have you changed any Capybara setting? – gertas Mar 03 '11 at 12:39
  • 1
    Can you post the relevant step implementations for this scenario? – Dylan Markow Mar 03 '11 at 15:41
  • 1
    To answer your first question: Yes, there should be persistent session state within a scenario. (And it should work with both the default Rack::Test driver and with Selenium.) Why it might not be working for you, I have no idea. – Jo Liss Mar 07 '11 at 21:16
  • @webren I did some workaround for my test, but nickgrim was pointing to something which looks very similar: http://blog.ardes.com/2010/4/28/capybara-and-rack-test-sessions-and-http-methods – xijo Jul 07 '11 at 14:45
  • @Joe I looked into that article, but that wasn't my issue (my paths already had leading slashes). Did your work-around involve mocking? – webren Jul 07 '11 at 16:56

3 Answers3

7

We had the problem with loosing the session due to capybara switching the host name in mid-test. The scenario was something like the following:

# Good
When I visit some page
# will call 'http://capybarawhatever/some_page
And I click the the button
# will call 'http://capybarawhatever/some_new_page'
Then I still have the session

# Failing
When I visit some page
# will call 'http://capybarawhatever/some_page'
And I do something that redirects me to "http://newhost.org/new_page"
And I visit some page
# No this may go to 'http://newhost.org/some_page
Then I have lost my session

This may be worth investigating. You can get the current_url in your session, and you may set a new host for capybara using host! 'newhost.org'

averell
  • 3,762
  • 2
  • 21
  • 28
  • I will investigate in this direction. There is a switch of context involved in the step, although the session information is only stored and used after the context switch. – xijo Mar 11 '11 at 07:49
  • I appears that the capybara session may also be [sensitive to relative URLS](http://blog.ardes.com/2010/4/28/capybara-and-rack-test-sessions-and-http-methods). – averell May 06 '11 at 09:50
5

Some of the drivers don't have a clear way of setting cookies. This is a hacky workaround until they are sorted out:

  def set_cookie(name, value, domain)
    driver = Capybara.current_session.driver rescue nil

    return unless driver

    case driver
    when Capybara::Driver::RackTest
      driver.set_cookie "#{name}=#{value}"
    when Capybara::Driver::Selenium
      visit '/' # must visit the domain before we can set the cookie

      br = driver.browser.send(:bridge)

      br.addCookie({
        'name'    => name,
        'domain'  => domain,
        'value'   => value,
        'path'    => '/',
        'expires' => (Time.now + 100.years).to_i
      })
    else
      raise "Unsupported driver #{driver}"
    end
  end
raggi
  • 1,290
  • 9
  • 12
  • 1
    Just so nobody gets confused: The drivers do handle cookies coming from the application -- it seems to me that your answer is about *manually* setting cookies. (Though I'm not sure that this solves the OP's problem.) – Jo Liss Mar 07 '11 at 21:18
  • You're correct, yeah, this is useful if you want to force a login, for example if a normal login process would be driven by an openid/oauth approach that might take time / require an internet connection. – raggi Mar 20 '11 at 04:36
  • just wanted to add that your case statement should be `case driver.class`, otherwise you're looking at a massive hash – Eric Hu May 12 '11 at 19:49
  • This worked with an older version of Cucumber/Capybara and I had to use `page.driver.set_cookie()` for it to work. Thanks! – Robert Fall May 21 '12 at 15:35
0

Possibly this bug?

nickgrim
  • 5,387
  • 1
  • 22
  • 28
  • @Ankur The points in the blog post felt like a reasonable answer to the OP's "Any ideas why this isn't the case?", so I'm not sure why you think this isn't an answer (though the link-only point is valid; in my defence, I wrote this over 2.5 years ago). – nickgrim Dec 05 '13 at 18:06
  • This was useful to me, since indeed my issue was that Capybara clears the session when it changes domain, even though I was navigating from /some/url to localhost:8080/some/other – comandante N Dec 30 '13 at 13:51