Capybara & Cucumber | Getting cookies

前端 未结 4 1160
梦毁少年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:56

    Here is my code of step defenition:

    Then /^cookie "([^\"]*)" should be like "([^\"]*)"$/ do |cookie, value|
      cookie_value = Capybara.current_session.driver.request.cookies.[](cookie)
      if cookie_value.respond_to? :should
        cookie_value.should =~ /#{value}/
      else
        assert cookie_value =~ /#{value}/
      end 
    end
    

    Here is example of output when test fails:

    expected: /change/
         got: "/edit" (using =~)
    Diff:
    @@ -1,2 +1,2 @@
    -/change/
    +"/edit"
     (RSpec::Expectations::ExpectationNotMetError)
    ./features/step_definitions/web_steps.rb:244:in `/^cookie "([^\"]*)" should be like "([^\"]*)"$/'
    features/auth.feature:57:in `And cookie "go" should be like "change"'
    

提交回复
热议问题