简体   繁体   English

Cucumber / Capybara / Selenium - 设置饼干

[英]Cucumber/Capybara/Selenium - Setting the cookies

For my test case, I tried setting the cookies with following ways, But it is not getting set 对于我的测试用例,我尝试使用以下方式设置cookie,但它没有设置

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. Capybara必须首先访问您正在测试的网站。 It is a required step before you can set any cookie. 这是您可以设置任何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. 为获得最佳性能,只需运行此步骤一次,或查看cookie是否已设置。 Here is actual code that sets the cookieconsent cookie, which bypasses a cookie wall. 以下是设置cookieconsent cookie的实际代码,该cookie绕过cookie墙。

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. 我正在使用page.driver来获取驱动程序。 It's the same thing as Capybara.current_session.driver . 它和Capybara.current_session.driver

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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