问题描述
我想在我的webapp上测试多个用户的登录,我使用黄瓜和capybara来做这个。我找到了如何处理多个会话,但不幸的是,它看起来不像黄瓜可以找到定义的 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代码
Ruby code
#standard_definitions/switching_sessions.rb
When /^(.*) in (.*) browser$/ do |next_step, name|
in_session(name) do
step next_step
end
end
$ b b
#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.using_session
,而不是in_session,所以你不需要features / support / session.rb。请注意,测试的编写方式以及您共享的链接中的代码,只有以在xxxx的浏览器中结尾的步骤实际上会出现在不同的会话中,而不会出现在这些行下。要使每个步骤组都出现在自己的会话中,您需要在每一行上使用在xxx的浏览器中,或者改为使用 Capybara.session_name =< name>
然后将用于未来步骤的步骤。如果直接设置session_name,您可能希望将其重置为:如果您希望以后的方案默认为该会话,则在后面的块中默认。
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
这篇关于在水豚的多个会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!