【问题标题】:capybara-webkit - rails session not retained/setcapybara-webkit - rails 会话未保留/设置
【发布时间】:2012-01-21 19:09:42
【问题描述】:

我已经为我的集成测试设置了 capybara-webkit,我遇到了一个非常简单的问题。我的会话没有被存储。用例非常简单

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

现在在第 2 步,我的应用正在将测试用例返回到登录页面 - 这意味着会话设置不正确。

非常感谢任何帮助

如果我使用@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: 看起来会话正在设置中。我使用以下代码在我的步骤中转储会话

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

我得到了以下信息 - 所以看起来 cookie 正在设置但没有被发回

[ “_ jqt_session = BAh7CEkiD3Nlc3Npb25faWQGOgZFRiIlYmMwYzNjYjY0MGU3NTg0OWFlNTcwODhmM2I2MzE1YmRJIhBfY3NyZl90b2tlbgY7AEZJIjEwRzN6NG1NTzZqamNCNC9FdWZWeXBCMHdoeThueXBnaTJDcTVzbmJqQlBZPQY7AEZJIgpmbGFzaAY7AEZJQzolQWN0aW9uRGlzcGF0Y2g6OkZsYXNoOjpGbGFzaEhhc2h7BjoKYWxlcnRJIh9JbnZhbGlkIGVtYWlsIG9yIHBhc3N3b3JkLgY7AFQGOgpAdXNlZG86CFNldAY6CkBoYXNoewY7B1Q%3D - 3fbe1c2a77a433228e7b7f2d8c8f0aec3ad5fb5f;仅Http;域=本地主机;路径= /”] P>

更新 2: 叫错树了。似乎我在测试用例中创建的用户没有被 rails 应用程序看到,因为我的数据库清理策略设置为事务性。查看更多信息 https://groups.google.com/forum/#!msg/ruby-capybara/JI6JrirL9gM/R6YiXj4gi_UJ

【问题讨论】:

    标签: ruby-on-rails session-cookies capybara-webkit


    【解决方案1】:

    为了增加清晰度, Capybara webkit 或 selenium 驱动程序在与应用程序不同的线程中运行,因此如果您使用事务性装置或 database_cleaner 与 strategy :transaction 并且您的数据未提交到 db 并且另一个线程将看不到它。 可能的解决方案是:

    1. 将 database_cleaner 与策略 :truncation 一起使用。 (稳定,但很慢)
    2. 添加代码以对所有线程使用单个事务强制活动记录。 (更快,但可能有一些问题,例如: 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
      

    我使用的是第二个选项,但这是有争议的。

    【讨论】:

      猜你喜欢
      • 2018-05-19
      • 1970-01-01
      • 2015-02-26
      • 2023-03-05
      • 1970-01-01
      • 2015-07-29
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多