我为我的集成测试设置了 capybara-webkit,我遇到了一个非常简单的问题。我的 session 没有被存储。用例非常简单
1. Login
2. Go to a specific page
3. Check if it has the approp content
现在在第 2 步,我的应用程序正在将测试用例返回到登录页面——这意味着 session 设置不正确。
任何帮助深表感谢
如果我使用@culerity 而不是@javascript,那么这个测试用例就会通过,所以问题似乎是 capybara-webkit 设置
我对 capybara-webkit 支持的 env.rb 如下
Spork.prefork do
require 'cucumber/rails'
require 'capybara'
require 'capybara/dsl'
require 'capybara/cucumber'
require 'capybara-webkit'
Capybara.run_server = false
Capybara.javascript_driver = :webkit
Capybara.default_selector = :css
# Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In
# order to ease the transition to Capybara we set the default here. If you'd
# prefer to use XPath just remove this line and adjust any selectors in your
# steps to use the XPath syntax.
# Capybara.default_host = "127.0.0.1:3000"
Capybara.app_host = "http://localhost:3000"
end
更新1:
看起来 session 正在设置中。我使用以下代码在我的步骤中转储 session
puts(Capybara.current_session.driver.browser.get_cookies)
我得到了以下信息 - 所以看起来 cookie 正在设置但没有被发回
["_jqt_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRiIlYmMwYzNjYjY0MGU3NTg0OWFlNTcwODhmM2I2MzE1YmRJIhBfY3NyZl90b2tlbgY7AEZJIjEwRzN6NG1NTzZqamNCNC9FdWZWeXBCMHdoeThueXBnaTJDcTVzbmJqQlBZPQY7AEZJIgpmbGFzaAY7AEZJQzolQWN0aW9uRGlzcGF0Y2g6OkZsYXNoOjpGbGFzaEhhc2h7BjoKYWxlcnRJIh9JbnZhbGlkIGVtYWlsIG9yIHBhc3N3b3JkLgY7AFQGOgpAdXNlZG86CFNldAY6CkBoYXNoewY7B1Q%3D--3fbe1c2a77a433228e7b7f2d8c8f0aec3ad5fb5f; HttpOnly; domain=localhost; path=/"]
更新 2:
叫错树了。似乎我在测试用例中创建的用户没有被 rails 应用程序看到,因为我的数据库清理策略设置为事务性。查看更多信息
https://groups.google.com/forum/#!msg/ruby-capybara/JI6JrirL9gM/R6YiXj4gi_UJ
最佳答案
为了增加清晰度,
Capybara webkit 或 selenium 驱动程序在与应用程序不同的线程中运行,因此,如果您使用事务性装置或 database_cleaner 与 strategy :transaction 并且您的数据未提交到 db 并且另一个线程将看不到它。
可能的解决方案是:
#Capybara use the same connection
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || retrieve_connection
end
end
# Forces all threads to share the same connection. This works on
# Capybara because it starts the web server in a thread.
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
我使用的是第二个选项,但这是有争议的。
关于ruby-on-rails - capybara-webkit - rails session 未保留/设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8531093/