Rails, Cucumber, Capybara: session is not persisted

后端 未结 3 2052
清酒与你
清酒与你 2020-12-19 02:36

I\'m trying to write a test for a feature that relies on some session stored data and my scenario looks like this:

Scenario: Create offer
  Given I am on the         


        
相关标签:
3条回答
  • 2020-12-19 02:55

    We had the problem with loosing the session due to capybara switching the host name in mid-test. The scenario was something like the following:

    # Good
    When I visit some page
    # will call 'http://capybarawhatever/some_page
    And I click the the button
    # will call 'http://capybarawhatever/some_new_page'
    Then I still have the session
    
    # Failing
    When I visit some page
    # will call 'http://capybarawhatever/some_page'
    And I do something that redirects me to "http://newhost.org/new_page"
    And I visit some page
    # No this may go to 'http://newhost.org/some_page
    Then I have lost my session
    

    This may be worth investigating. You can get the current_url in your session, and you may set a new host for capybara using host! 'newhost.org'

    0 讨论(0)
  • 2020-12-19 02:55

    Possibly this bug?

    0 讨论(0)
  • 2020-12-19 02:56

    Some of the drivers don't have a clear way of setting cookies. This is a hacky workaround until they are sorted out:

      def set_cookie(name, value, domain)
        driver = Capybara.current_session.driver rescue nil
    
        return unless driver
    
        case driver
        when Capybara::Driver::RackTest
          driver.set_cookie "#{name}=#{value}"
        when Capybara::Driver::Selenium
          visit '/' # must visit the domain before we can set the cookie
    
          br = driver.browser.send(:bridge)
    
          br.addCookie({
            'name'    => name,
            'domain'  => domain,
            'value'   => value,
            'path'    => '/',
            'expires' => (Time.now + 100.years).to_i
          })
        else
          raise "Unsupported driver #{driver}"
        end
      end
    
    0 讨论(0)
提交回复
热议问题