简体   繁体   中英

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM