How do I re-use Capybara sessions between tests?

后端 未结 4 1116
别那么骄傲
别那么骄傲 2021-02-05 09:27

I want to keep on using the same session and by that I mean Rails\' session between various Test::Unit integration tests that use Capybara. The Capybara::Sess

相关标签:
4条回答
  • 2021-02-05 09:47

    Add the following after your capybara code that interacts with the page:

    Capybara.current_session.instance_variable_set(:@touched, false)
    
    or
    
    page.instance_variable_set(:@touched, false)
    

    If that doesn't work, these might help:

    https://github.com/railsware/rack_session_access

    http://collectiveidea.com/blog/archives/2012/01/05/capybara-cucumber-and-how-the-cookie-crumbles/

    0 讨论(0)
  • 2021-02-05 09:48

    If what you are doing is trying to string together individual examples into a story (cucumber style, but without cucumber), you can use a gem called rspec-steps to accomplish this. For example, normally this won't work:

    describe "logging in" do
      it "when I visit the sign-in page" do 
        visit "/login"
      end
      it "and I fill in my registration info and click submit" do
        fill_in :username, :with => 'Foo'
        fill_in :password, :with => 'foobar'
        click_on "Submit"
      end
      it "should show a successful login" do
        page.should have_content("Successfully logged in")
      end
    end
    

    Because rspec rolls back all of its instance variables, sessions, cookies, etc.

    If you install rspec-steps (note: currently not compatible with rspec newer than 2.9), you can replace 'describe' with 'steps' and Rspec and capybara will preserve state between the examples, allowing you to build a longer story, e.g.:

    steps "logging in" do
      it "when I visit the sign-in page" #... etc.
      it "and I fill in" # ... etc.
      it "should show a successful" # ... etc.
    end
    
    0 讨论(0)
  • 2021-02-05 09:59

    You can prevent the call to @browser.manage.delete_all_cookies that happens between tests by monkey patching the Capybara::Selenium::Driver#reset! method. It's not a clean way of doing it, but it should work...

    Add the following code to your project so that it is executed after you require 'capybara':

    class Capybara::Selenium::Driver < Capybara::Driver::Base
      def reset!
        # Use instance variable directly so we avoid starting the browser just to reset the session
        if @browser
          begin
            #@browser.manage.delete_all_cookies <= cookie deletion is commented out!
          rescue Selenium::WebDriver::Error::UnhandledError => e
            # delete_all_cookies fails when we've previously gone
            # to about:blank, so we rescue this error and do nothing
            # instead.
          end
          @browser.navigate.to('about:blank')
        end
      end
    end
    

    For interest's sake, the offending line can be seen in Capybara's codebase here: https://github.com/jnicklas/capybara/blob/master/lib/capybara/selenium/driver.rb#L71

    0 讨论(0)
  • 2021-02-05 10:04

    It may be worth posting the reason why you need this kind of behaviour. Usually, having the need to monkey patch Capybara, is an indication that you are attempting to use it for something it was not intended for. It is often possible to restructure the tests, so that you don't need the cookies persisted across integration tests.

    0 讨论(0)
提交回复
热议问题