【问题标题】:Capybara email resets the sessionCapybara 电子邮件重置会话
【发布时间】:2017-04-19 19:11:44
【问题描述】:

在 Capybara 注册后,我需要测试特定的智能重定向流程。假设我的网站中有几个interesting_pages,我想在确认注册后将用户重定向到最后访问的有趣页面

小场景:

When I visit the interesting page "42"
And I visit a couple uninteresting pages
# Note during the visit to other pages, session[:interesting_page] is correctly set to the url of page "42"
When I sign up and I confirm via the link in the email
# At this point I have lost the session[:interesting_page]
Then I should be confirmed and redirected to the last visited interesting page "42"

对于实际实现,我决定选择controller.session[]= 作为suggested in this answer。在呈现page "42" 的控制器中,我将会话设置为session[:interesting_page] = request.original_url

在开发过程中,单击电子邮件中的确认链接后,我可以在设计确认期间成功重定向到 session[:interesting_page]

但是,当我尝试使用 Cucumber 进行测试时,Capybara-email 在单击电子邮件链接时似乎会重置会话,因此当我单击电子邮件中的链接以重新连接时,session[:interesting_page] 会被删除...

编辑When I sign up and I confirm via the link in the email 步骤基本上是通过设计注册控制器,我使用 Capybara::Email 来点击确认电子邮件

# Nested steps for "When I sign up and I confirm via the link in the email"
I fill in "email" with ...
...
I click "Sign up !"
Then I should see "Please confirm your account via the link !"
# At this stage session[:interesting_page] still exists and points to page "42"
And an email should have been sent to "#{user.email}"
And in the current mail I click "Je finalise mon inscription"

# The RegistrationController code I use is similar to devise and returns a pending_confirmation page
# respond_with resource, location: pending_confirmation_path


# Associated step implementations
Then(/an email should have been sent to "([^"]*)"/) do |address|
  open_email(address)
  expect(current_email).to be
end

When(/in the current mail I click "([^"]*)"$/) do |link|
  current_email.click_link link
end

【问题讨论】:

  • 显示您的When I sign up and I confirm via the link in the email 步骤
  • 还有When I visit the interesting page "42" 步骤 - 重要的是这些步骤最后有预期,以确保页面实际加载。如果没有这些期望,则会话 cookie 可能永远不会在浏览器中设置。
  • @ThomasWalpole 我刚刚添加了步骤代码。此外,我通过 cucumber 进行了检查,尝试在电子邮件步骤之前加载其他页面,并且确实设置了会话。

标签: ruby-on-rails session devise cucumber capybara


【解决方案1】:

Capybara 中的动作可以异步发生。这意味着,如果您不检查页面上应该可见的内容,一旦您执行的任何操作完成,并调用另一个访问或操作,则该操作的响应设置的任何 cookie 可能不会get set,这意味着您实际上并没有登录,也没有保存一些重要的数据。在您的情况下,这意味着在(至少)I click "Sign up !" 之后,您需要对成功注册后将出现在页面上的文本进行预期。

要检查的另一件事是电子邮件中 ​​url 的主机名(可能还有端口)与您正常访问所使用的主机名匹配。

【讨论】:

  • 嘿,对不起,我已经有类似的东西了,只是觉得不太相关。感谢您的提醒,它实际上可能解释了我有时在其他测试中看到的其他非确定性故障 xD
  • @CyrilDuchon-Doris - 是的,非常重要,这也是为什么应该始终使用“have_current_path”匹配器而不是eqcurrent_url。我刚刚添加了另一件事来检查,因为 cookie 是基于主机名存储的(可能端口取决于浏览器等)
  • 该死!请求主机从 127.0.0.1 更改为 localhost,这可能解释了不同的 session_id.... 127.0.0.1 是我点击任何电子邮件之前的主机名,之后它变为 localhost。我怎样才能统一呢?
  • @CyrilDuchon-Doris 是的,这肯定可以解释。这意味着在您的电子邮件中生成的 URL 使用“localhost”而不是 127.0.0.1 - 您可能需要在 test.rb 中配置 config.action_mailer.default_url_options,这可能需要将 Capyara.server_port 锁定到固定端口号,这样您就知道了在 default_url_options 中放入什么端口。
  • 谢谢,我确实解决了config.action_mailer.default_url_options = { host: '127.0.0.1', port: port_used_by_capybara }的问题。感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-18
  • 2015-03-20
  • 1970-01-01
  • 1970-01-01
  • 2013-01-18
  • 1970-01-01
  • 2019-09-05
相关资源
最近更新 更多