Capybara & Cucumber | Getting cookies

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

    It seems that Selenium API has changed. The suggested solution did not work, but I after spending a bit of time looking around, I found a solution.

    To save a cookie:

    browser = Capybara.current_session.driver.browser.manage
    browser.add_cookie :name => key, :value => val
    

    To read a cookie:

    browser = Capybara.current_session.driver.browser.manage
    browser.cookie_named(key)[:value]
    

    cookie_named returns an array consisting of "name" and "value", so we need an extra reference to extract cookie value.

提交回复
热议问题