Capybara & Cucumber | Getting cookies

前端 未结 4 1148
梦毁少年i
梦毁少年i 2021-02-13 14:27

I\'m trying to get cookie values in the Cucumber step:

Step definitions

When /^I log in$/ do
  #          


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-13 14:34

    Step Definitions

    Then /^cookies should be set^/ do
      Capybara
        .current_session # Capybara::Session
        .driver          # Capybara::RackTest::Driver
        .request         # Rack::Request
        .cookies         # { "author" => "me" }
        .[]('author').should_not be_nil
    end
    

    This works, however, I'm still looking for a less verbose way. Moreover, I'd like to know how to get the session data in a step definition.

    Updated

    To get the session data one should do the following:

    Step Definitions

    Then /^session data should be set$/ do
      cookies = Capybara
        .current_session
        .driver
        .request
        .cookies
    
      session_key = Rails
        .application
        .config
        .session_options
        .fetch(:key)
    
      session_data = Marshal.load(Base64.decode64(cookies.fetch(session_key)))
    
      session_data['author'].should_not be_nil
    end
    

    This is quite verbose too.

提交回复
热议问题