使用水豚测试黄瓜中的饼干

发布于 2024-10-03 02:43:10 字数 910 浏览 4 评论 0原文

作为网站集成测试的一部分,我将黄瓜与水豚一起使用。水豚似乎无法模拟cookie的使用。

例如,我在用户登录时设置 cookie:

    def sign_in(user)
      cookies.permanent.signed[:remember_token] = [user.id, user.salt]
      current_user = user
    end

但是,当我稍后使用 cookies.inspect 获取 cookie 的值时,它返回 {} 这是水豚的已知限制吗?如果是这种情况,如何通过多个请求测试登录用户?

我应该添加我的测试:

Scenario: User is signed in when they press Sign In
 Given I have an existing account with email "[email protected]" and password "123456"
 And I am on the signin page
 When I fill in "Email" with "[email protected]"
 And I fill in "Password" with "123456"
 And I press "Sign In"
 Then I should see "Welcome Bob Jones"

As part of my integration tests for a website I am using cucumber with capybara. It seems that capybara cannot emulate the use of cookies.

For example I set the cookie when the user signs in:

    def sign_in(user)
      cookies.permanent.signed[:remember_token] = [user.id, user.salt]
      current_user = user
    end

However when I later fetch the value of cookies using cookies.inspect it returns {}
Is this a known limiting of capybara? How can I test a signed in user over multiple requests if this is the case?

I should add my test:

Scenario: User is signed in when they press Sign In
 Given I have an existing account with email "[email protected]" and password "123456"
 And I am on the signin page
 When I fill in "Email" with "[email protected]"
 And I fill in "Password" with "123456"
 And I press "Sign In"
 Then I should see "Welcome Bob Jones"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小红帽 2024-10-10 02:43:10

这是对我有用的步骤。它将 cookie“admin_cta_choice”设置为等于从输入值派生的模型 ID。

Given /I have selected CTA "([^"]+)"/ do |cta_name|
  cta = Cta.find_by_name(cta_name)
  raise "no cta with name '#{cta_name}'" if cta.nil?

  k = "admin_cta_choice"
  v = cta.id

  case Capybara.current_session.driver
  when Capybara::Poltergeist::Driver
    page.driver.set_cookie(k,v)
  when Capybara::RackTest::Driver
    headers = {}
    Rack::Utils.set_cookie_header!(headers,k,v)
    cookie_string = headers['Set-Cookie']
    Capybara.current_session.driver.browser.set_cookie(cookie_string)
  when Capybara::Selenium::Driver
    page.driver.browser.manage.add_cookie(:name=>k, :value=>v)
  else
    raise "no cookie-setter implemented for driver #{Capybara.current_session.driver.class.name}"
  end
end

Here's a step that works for me. It sets a cookie "admin_cta_choice" to be equal to a model id derived from the input value.

Given /I have selected CTA "([^"]+)"/ do |cta_name|
  cta = Cta.find_by_name(cta_name)
  raise "no cta with name '#{cta_name}'" if cta.nil?

  k = "admin_cta_choice"
  v = cta.id

  case Capybara.current_session.driver
  when Capybara::Poltergeist::Driver
    page.driver.set_cookie(k,v)
  when Capybara::RackTest::Driver
    headers = {}
    Rack::Utils.set_cookie_header!(headers,k,v)
    cookie_string = headers['Set-Cookie']
    Capybara.current_session.driver.browser.set_cookie(cookie_string)
  when Capybara::Selenium::Driver
    page.driver.browser.manage.add_cookie(:name=>k, :value=>v)
  else
    raise "no cookie-setter implemented for driver #{Capybara.current_session.driver.class.name}"
  end
end
尤怨 2024-10-10 02:43:10

这是在运行功能时显示 cookie 内容的好方法

https://gist.github.com/484787< /a>

here's a nice way to show the content of the cookies while running your feature

https://gist.github.com/484787

梦旅人picnic 2024-10-10 02:43:10

为什么不直接用selenium 来运行测试呢?只需在要使用真实浏览器运行的场景之前添加 @selenium 标签即可。 :)

Why don't you just run the test with selenium? Just add @selenium tag before the scenario you want to run with a real browser. :)

陌若浮生 2024-10-10 02:43:10

截至本文发布之日,该要点对我不起作用,但是这确实有效,而且更简单:

http://collectiveidea.com/blog/archives/2012/01/05/capybara-cucumber-and-how-the-cookie-crumbles/

That gist didn't work for me as of date of this posting, however this does, and is somewhat simpler:

http://collectiveidea.com/blog/archives/2012/01/05/capybara-cucumber-and-how-the-cookie-crumbles/

半岛未凉 2024-10-10 02:43:10

Capybara 没有用于读取和设置 cookie 的 API。

但是,您可以非常轻松地使用 Capyabara 模拟登录 - 只需访问登录链接即可。这将使您登录,包括为您设置所有 cookie。

要查看其实际效果,只需查看我的示例 Rails 应用。

Capybara doesn't have an API for reading and setting cookies.

However, you can simulate logging in with Capyabara very easily - just visit the login link. That will log you in, including setting all cookies for you.

To see this in action, just look at my example Rails app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文