Capybara-webkit - rails session not saved / established

Ive setup capybara-webkit for my integration tests, and Im working in a very simple problem. My session is not saved. Use case is quite simple

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

Now in step 2, my application returns a test case to the login page - this means that the session is not configured properly.

any help is much appreciated

If I use @culerity instead of @javascript then this test case passes, so the problem is setting capybara-webkit

My env.rb to support capybara-webkit 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 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: It seems that the sessions are being established. I used the following code to reset the session in my steps.

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

and I got the following: it looks like a cookie is being set but not sent back

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

Update 2: barking the wrong tree. It seems that the user I created in my test case did not see the rails application, since my database cleanup strategy was set to transaction. see more information at https://groups.google.com/forum/#!msg/ruby-capybara/JI6JrirL9gM/R6YiXj4gi_UJ

+4
source share
1 answer

To add more clarity, the Capybara webkit or selenium driver runs in a different thread, and then in the application, so if you use transactional devices or database_cleaner with the strategy: the transaction and your data are not sent to db, and the other thread will not see it. Possible solutions:

  • Use database_cleaner with strategy: truncation. (solid but slow)
  • Add code to force active recording using a single transaction for all threads. (faster, but may have some problems, for example: after_commit hooks are not called because there is 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 use option 2, but it is possible.

+1
source

Source: https://habr.com/ru/post/1386657/


All Articles