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
版本:在轨道上运行2.3.10,黄瓜0.10.0,黄瓜轨道0.3.2,水豚0.4.1.2
3 个解决方案
#1
6
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'
这可能值得研究。你可以在你的会话中获得current_url,你可以使用host为capybara设置一个新的主机! 'newhost.org'
#2
5
Some of the drivers don't have a clear way of setting cookies. This is a hacky workaround until they are sorted out:
一些驱动程序没有明确的设置cookie的方法。这是一个hacky解决方法,直到它们被整理出来:
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
#1
6
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'
这可能值得研究。你可以在你的会话中获得current_url,你可以使用host为capybara设置一个新的主机! 'newhost.org'
#2
5
Some of the drivers don't have a clear way of setting cookies. This is a hacky workaround until they are sorted out:
一些驱动程序没有明确的设置cookie的方法。这是一个hacky解决方法,直到它们被整理出来:
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