简体   繁体   中英

capybara-webkit - rails session not retained/set

Ive setup capybara-webkit for my integration tests and Im running into a very simple problem. 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.

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

My env.rb for capybara-webkit support is as follows

    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. 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

["_jqt_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRiIlYmMwYzNjYjY0MGU3NTg0OWFlNTcwODhmM2I2MzE1YmRJIhBfY3NyZl90b2tlbgY7AEZJIjEwRzN6NG1NTzZqamNCNC9FdWZWeXBCMHdoeThueXBnaTJDcTVzbmJqQlBZPQY7AEZJIgpmbGFzaAY7AEZJQzolQWN0aW9uRGlzcGF0Y2g6OkZsYXNoOjpGbGFzaEhhc2h7BjoKYWxlcnRJIh9JbnZhbGlkIGVtYWlsIG9yIHBhc3N3b3JkLgY7AFQGOgpAdXNlZG86CFNldAY6CkBoYXNoewY7B1Q%3D--3fbe1c2a77a433228e7b7f2d8c8f0aec3ad5fb5f; HttpOnly; domain=localhost; path=/"]

Update 2: was barking up the wrong tree. 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. see more info at 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. Possible solutions are:

  1. Use database_cleaner with strategy :truncation. (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)

     #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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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