如何在测试之间重复使用Capybara会话? [英] How do I re-use Capybara sessions between tests?
问题描述
我想继续使用相同的会话,我的意思是Rails的会话在使用Capybara的各种 Test :: Unit
集成测试之间。 Capybara :: Session
对象在所有测试中都是相同的,因为它被重用,但是当我访问另一个页面在另一个测试,我立即注销。
I want to keep on using the same session and by that I mean Rails' session between various Test::Unit
integration tests that use Capybara. The Capybara::Session
object is the same in all the tests as it is re-used, but when I access another page in another test, I'm immediately logged out.
挖掘我发现在一个测试和下一个测试之间清除 capybara_session.driver.browser.manage.all_cookies
。
Digging in I found that capybara_session.driver.browser.manage.all_cookies
is cleared between one test and the next.
任何想法如何?或为什么?或者如何避免它?
Any ideas how? or why? or how to avoid it?
尝试解决方法是,我将cookie保存在类变量中,然后通过运行重新添加:
Trying to work-around that, I saved the cookie in a class variable and re-added later by running:
capybara_session.driver.browser.manage.add_cookie(@@cookie)
它似乎工作,cookie是有的,但是当有一个请求,cookie被替换为另一个,所以它没有效果。
and it seems to work, the cookie is there, but when there's a request, the cookie gets replaced for another one, so it had no effect.
有没有其他方法实现这个?
Is there any other way of achieving this?
推荐答案
与页面:
Capybara.current_session.instance_variable_set(:@touched, false)
or
page.instance_variable_set(:@touched, false)
t工作,这些可能有助于:
If that doesn't work, these might help:
https://github.com/railsware/ rack_session_access
http://collectiveidea.com/blog/archives/2012/01/05/capybara-cucumber-and-how-the-cookie-crumbles/
这篇关于如何在测试之间重复使用Capybara会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!