Rails,Cucumber,Capybara:session不会持久化 [英] Rails, Cucumber, Capybara: session is not persisted
问题描述
我试图写一个依赖于一些会话存储数据的功能的测试,我的场景看起来像这样:
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?
感谢提前,
Joe
Thanks in advance, Joe
:运行在rails 2.3.10,cucumber 0.10.0,cucumber-rails 0.3.2,capybara 0.4.1.2
Versions: Running on rails 2.3.10, cucumber 0.10.0, cucumber-rails 0.3.2, capybara 0.4.1.2
推荐答案
的驱动程序没有清楚的设置cookie的方式。这是一个奇怪的解决方法,直到他们被整理出来:
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
这篇关于Rails,Cucumber,Capybara:session不会持久化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!