水豚中的多个会话 [英] Multiple sessions in capybara
问题描述
我想在我的 web 应用程序上测试以多个用户身份登录,我正在使用 cucumber 和 capybara 来执行此操作.我发现 这个链接 关于如何处理多个会话,但是不幸的是,cucumber 似乎无法找到定义的 in_session
方法.如何访问它?
I want to test logging in as multiple users on my webapp, and I am using cucumber and capybara to do this. I found this link on how to handle multiple sessions, but unfortunately, it doesn't look like cucumber can find the in_session
method that is defined. How can I access it?
Given I go to teacher login in Steve's browser
When I enter "Steve" in the username field
And I enter "StevesPassword" in the password field
And I click login
Then I should see "Steve Lastname" as the current user
When I go to teacher login in Lisa's browser
And I enter "Lisa" in the username field
And I enter "LisasPassword" in the password field
And I click login
Then I should see "Lisa Lastname" as the current user
Ruby 代码
#standard_definitions/switching_sessions.rb
When /^(.*) in (.*) browser$/ do |next_step, name|
in_session(name) do
step next_step
end
end
<小时>
#features/support/sessions.rb
module Capybara
module Driver
module Sessions
def set_session(id)
Capybara.instance_variable_set("@session_pool", {
"#{Capybara.current_driver}#{Capybara.app.object_id}" => $sessions[id]
})
end
def in_session(id, &block)
$sessions ||= {}
$sessions[:default] ||= Capybara.current_session
$sessions[id] ||= Capybara::Session.new(Capybara.current_driver, Capybara.app)
set_session(id)
yield
set_session(:default)
end
end
end
end
错误
当我运行它时,我得到以下信息:
Errors
When I run this, I get the following:
Scenario: multiple users # features/Provebank/Provebank.feature:23
Given I go to teacher login in Steve's browser # features/step_definitions/standard_definitions/switching_sessions.rb:5
undefined method `in_session' for Capybara::Driver::Sessions:Module (NoMethodError)
./features/step_definitions/standard_definitions/switching_sessions.rb:8:in `/^(.*) in (.*) browser$/'
features/test.feature:24:in `Given I go to teacher login in Steve's browser'
推荐答案
正如文章顶部提到的,您链接的现在已包含在 Capybara 库中."- 除了方法被命名为 Capybara.using_session
而不是 in_session,所以你不需要 features/support/session.rb.请注意,您的测试编写方式以及您共享的链接中的代码,只有以在 xxxx 的浏览器中"结尾的步骤实际上会发生在不同的会话中,而不是您在这些行下标记的那些.要使这些步骤组中的每一个都发生在它们自己的会话中,您要么需要在每一行上在 xxx 的浏览器中",要么在一个步骤中使用 Capybara.session_name = <name>
用于以后的步骤.如果直接设置 session_name,如果您希望将来的场景默认为该会话,您可能希望在 After 块中将其重置为 :default.
As mentioned at the top of the article you linked "This is now included in the Capybara library." - except the method is named Capybara.using_session
rather than in_session, so you don't need features/support/session.rb. Note that the way your test is written, and the code in the link you shared, only the steps ending in "in xxxx's browser" would actually occur in a different session, not the ones you have tabbed over under those lines. To make each of those groups of steps occur in their own sessions you would either need "in xxx's browser" on every line or instead use Capybara.session_name = <name>
in a step which would then be used for future steps. If setting the session_name directly you may wish to reset it to :default in an After block if you want future scenarios to default to that session.
# only next_step will be executed in the alternate session
When /^(.) in (.*) browser$/ do |next_step, name|
Capybara.using_session(name) do
step next_step
end
end
# all future steps will be executed in the alternate session
# you probably want to reset this to :default in an After block
Given /^I switch to (.*) browser$/ do |name|
Capybara.session_name = name
end
这篇关于水豚中的多个会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!