Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cucumber/Capybara/Selenium - Setting the cookies

For my test case, I tried setting the cookies with following ways, But it is not getting set

1)browser = Capybara.current_session.driver.browser
  browser.manage.add_cookie :name => "xxx", :value => "cookie"

2)driver = Capybara.current_session.driver
  br = driver.browser.send(:bridge)
  br.addCookie({
    'name'    => "xxx",
    'domain'  => "localhost",
    'value'   => "cookie",
    'path'    => '/',
    'expires' => (Time.now + 100.years).to_i
  })

Let me know if i miss anything or i have to do it in other way

like image 402
Muzaffer Avatar asked Dec 10 '25 17:12

Muzaffer


1 Answers

Capybara has to visit the website you're testing first. It is a required step before you can set any cookie.

This works:

visit '/'
browser = Capybara.current_session.driver.browser
browser.manage.add_cookie name: "name", value: "value"

For best performance, either run this step just once, or see if the cookie is already set. Here is actual code that sets the cookieconsent cookie, which bypasses a cookie wall.

browser = page.driver.browser
unless browser.manage.cookie_named("cookieconsent")
  visit '/'
  browser.manage.add_cookie name: "cookieconsent", value: "dismiss"
end

I am using page.driver to get the driver. It's the same thing as Capybara.current_session.driver.

like image 175
Joost Baaij Avatar answered Dec 12 '25 06:12

Joost Baaij



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!