7

我想要一个 Cucumber 功能来测试设计的可记忆功能(一个记住我的 cookie)。

使用 capybara 很容易检查记住我复选框,但我应该如何模拟用户在关闭窗口后返回站点?

4

5 回答 5

1

我想出了以下机架测试 hack,并使用稍微干净的 selenium api 来测试黄瓜/水豚中的设计记住我的功能。它只是告诉驱动程序手动删除会话 cookie。并非所有驱动程序都受支持,我只实现了我使用过的两个:

http://gist.github.com/484787

这假定会话的 cookie 存储。从场景中删除 @announce 标记以消除冗长。

Matt Wynne 在邮件列表讨论中建议的另一种选择可能是查看其他 cookie 存储,并通过查询或文件删除来删除它们:

摘自敏捷 Rails 书:

config.action_controller.session_store = CGI::Session::PStore (or just :p_store)
config.action_controller.session_options[:tmpdir] = "/Users/dave/tmp" 
config.action_controller.session_options[:prefix] = "myapp_session_"

或者

rake db:sessions:create
config.action_controller.session_store = :active_record_store

Rails 也有一个重置会话方法,但我相信我们无法访问它,因为在使用 capybara 进行测试时我们无法挂钩到 rails 会话。

希望这可以帮助,

缺口

于 2010-08-27T12:50:59.343 回答
1

nruth 的要点真的很有帮助,但我觉得按名称删除 cookie 是在作弊。我创建了一个步骤,删除浏览器在关闭并重新启动时将删除的 cookie(任何未设置到期日期并在未来设置的 cookie)。

你可以在这个提交中看到它(虽然我只为 RackTest 驱动程序完成了它,因为我没有 Selenium 设置)。你也可以在这个 commit中看到我的 login/remember_me 功能。我将这些类重构为在此提交中分隔文件。

我希望这会有所帮助。

于 2010-12-17T03:57:35.030 回答
0

我使用电子邮件规范来实现这一点。我的场景如下所示:

@allow-rescue
Scenario: Create New Account (Everything cool)
Given I am not authenticated
When I go to register
And I fill in "Name" with "bill"
And I fill in "Email" with "bill@example.com"
And I fill in "Password" with "please"
And I fill in "Password Confirmation" with "please"
And I press "Sign up"
Then "bill@example.com" should receive an email
And I open the email
And I should see "Confirm my account" in the email body
When I follow "Confirm my account" in the email
Then I should see "Your account was successfully confirmed. You are now signed in."

请注意使用 Devise 时所需的场景上方的@allow-rescue装饰。

希望这可以帮助。

于 2010-08-12T14:42:46.927 回答
0

我想您可以使用 capybara 注销,然后重新登录,例如

Given I am on the login screen
And I select 'Remember Me'
And I click 'login'
Then I should be 'logged in'
When I click 'log out'
Then I should be 'logged out' #=> potentially destroy a session here?
When I click log in
Then I should be logged in
And I should not be directed to the login form.

应该使用 cabybara 的当前 cookie 状态来建模此流程。

于 2010-08-27T12:20:50.480 回答
0

您可以为此使用show_me_the_cookies,如下所示:

And(/^I leave the site$/) do
  expire_cookies
end
于 2015-06-11T17:27:25.413 回答