使用水豚测试黄瓜中的饼干
作为网站集成测试的一部分,我将黄瓜与水豚一起使用。水豚似乎无法模拟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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是对我有用的步骤。它将 cookie“admin_cta_choice”设置为等于从输入值派生的模型 ID。
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.
这是在运行功能时显示 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
为什么不直接用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. :)
截至本文发布之日,该要点对我不起作用,但是这确实有效,而且更简单:
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/
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.