简体   繁体   English

capybara-webkit - rails会话未保留/设置

[英]capybara-webkit - rails session not retained/set

Ive setup capybara-webkit for my integration tests and Im running into a very simple problem. 我为我的集成测试设置了capybara-webkit,我遇到了一个非常简单的问题。 My session is not being stored. 我的会话没有存储。 The use case is pretty simple 用例非常简单

       1. Login
       2. Go to a specific page
       3. Check if it has the approp content

Now at step 2 my app is returning the test case to the login page - which means the session is not being set properly. 现在在第2步,我的应用程序将测试用例返回到登录页面 - 这意味着会话未正确设置。

any help is much appreciated 任何帮助深表感谢

If I use @culerity instead of @javascript then this test case passes so the problem seems to be the capybara-webkit setup 如果我使用@culerity而不是@javascript那么这个测试用例通过,所以问题似乎是capybara-webkit设置

My env.rb for capybara-webkit support is as follows 我对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

Update 1: Looks like the sessions is being set. 更新1:看起来正在设置会话。 I used the following code to dump the session in my steps 我使用以下代码在我的步骤中转储会话

     puts(Capybara.current_session.driver.browser.get_cookies)

and I got the follwoing - so looks like cookie is being set but not being sent back 而且我接下来了 - 所以看起来像是设置了cookie而不是被送回去

["_jqt_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRiIlYmMwYzNjYjY0MGU3NTg0OWFlNTcwODhmM2I2MzE1YmRJIhBfY3NyZl90b2tlbgY7AEZJIjEwRzN6NG1NTzZqamNCNC9FdWZWeXBCMHdoeThueXBnaTJDcTVzbmJqQlBZPQY7AEZJIgpmbGFzaAY7AEZJQzolQWN0aW9uRGlzcGF0Y2g6OkZsYXNoOjpGbGFzaEhhc2h7BjoKYWxlcnRJIh9JbnZhbGlkIGVtYWlsIG9yIHBhc3N3b3JkLgY7AFQGOgpAdXNlZG86CFNldAY6CkBoYXNoewY7B1Q%3D--3fbe1c2a77a433228e7b7f2d8c8f0aec3ad5fb5f; HttpOnly; domain=localhost; path=/"] [ “_jqt_session = BAh7CEkiD3Nlc3Npb25faWQGOgZFRiIlYmMwYzNjYjY0MGU3NTg0OWFlNTcwODhmM2I2MzE1YmRJIhBfY3NyZl90b2tlbgY7AEZJIjEwRzN6NG1NTzZqamNCNC9FdWZWeXBCMHdoeThueXBnaTJDcTVzbmJqQlBZPQY7AEZJIgpmbGFzaAY7AEZJQzolQWN0aW9uRGlzcGF0Y2g6OkZsYXNoOjpGbGFzaEhhc2h7BjoKYWxlcnRJIh9JbnZhbGlkIGVtYWlsIG9yIHBhc3N3b3JkLgY7AFQGOgpAdXNlZG86CFNldAY6CkBoYXNoewY7B1Q%3D - 3fbe1c2a77a433228e7b7f2d8c8f0aec3ad5fb5f;仅Http;域=本地主机;路径= /”]

Update 2: was barking up the wrong tree. 更新2:正在咆哮错误的树。 Seems that the user I was creating in my test case was not being seen by the rails app as my database cleaner strategy was set to transactional. 似乎我在测试用例中创建的用户没有被rails应用程序看到,因为我的数据库清理策略设置为事务性。 see more info at https://groups.google.com/forum/#!msg/ruby-capybara/JI6JrirL9gM/R6YiXj4gi_UJ 请访问https://groups.google.com/forum/#!msg/ruby-capybara/JI6JrirL9gM/R6YiXj4gi_UJ查看更多信息

To add more clarity, Capybara webkit or selenium driver is running in a different thread then app, so if you are using transactional fixtures or database_cleaner with strategy :transaction and your data isn't commited to db and another thread won't see it. 为了增加更多的清晰度,Capybara webkit或selenium驱动程序运行在一个不同的线程然后应用程序,所以如果你使用事务夹具或database_cleaner与策略:事务,你的数据不会提交给db,另一个线程将不会看到它。 Possible solutions are: 可能的解决方案是

  1. Use database_cleaner with strategy :truncation. 将database_cleaner与策略一起使用:截断。 (solid, but a slow) (稳固,但很慢)
  2. Add code to force active record using single transaction for all threads. 添加代码以使用单个事务为所有线程强制激活记录。 (faster, but may have some issues, ex.: after_commit hooks aren't called, as there's no commit) (更快,但可能有一些问题,例如:没有调用after_commit挂钩,因为没有提交)

     #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 

I'm using 2-nd option, but it's arguable. 我正在使用第二种选择,但这是有争议的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM