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

时间:2010-11-14 09:40:50

标签: ruby-on-rails cucumber capybara

作为我对网站的集成测试的一部分,我使用黄瓜和水豚。似乎水豚不能模仿饼干的使用。

例如,我在用户登录时设置了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 "bob@jones.com" and password "123456"
 And I am on the signin page
 When I fill in "Email" with "bob@jones.com"
 And I fill in "Password" with "123456"
 And I press "Sign In"
 Then I should see "Welcome Bob Jones"

5 个答案:

答案 0 :(得分:4)

这是一个适合我的步骤。它将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

答案 1 :(得分:2)

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

https://gist.github.com/484787

答案 2 :(得分:1)

为什么不用硒进行测试?只需在要使用真实浏览器运行的场景之前添加@selenium标记。 :)

答案 3 :(得分:1)

截至发布日期,这个要点对我来说不起作用,不过这样做,而且有点简单:

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

答案 4 :(得分:0)

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

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

要查看此操作,请查看我的example Rails app