Arn*_*tta 1 ruby cucumber capybara
我想测试以多个用户的身份登录我的Web应用程序,并且正在使用黄瓜和水豚进行此操作。我找到了有关如何处理多个会话的链接,但不幸的是,它看起来不像黄瓜可以找到所in_session定义的方法。我该如何访问?
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
Run Code Online (Sandbox Code Playgroud)
#standard_definitions/switching_sessions.rb
When /^(.*) in (.*) browser$/ do |next_step, name|
in_session(name) do
step next_step
end
end
Run Code Online (Sandbox Code Playgroud)
#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
Run Code Online (Sandbox Code Playgroud)
运行此命令时,将得到以下信息:
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'
Run Code Online (Sandbox Code Playgroud)
如文章顶部所述,您已链接“现在已包含在Capybara库中。” -除了方法的名称Capybara.using_session而不是in_session之外,因此您不需要features / support / session.rb。请注意,测试的编写方式以及共享链接中的代码,仅以“在xxxx的浏览器中”结尾的步骤实际上将在不同的会话中发生,而不是您在这些行下选择的那些步骤。要使这些步骤组中的每一个都在各自的会话中发生,您可能需要在每一行上使用“在xxx的浏览器中”,或者使用Capybara.session_name = <name>一个步骤,然后再将其用于以后的步骤。如果直接设置session_name,如果您希望将来的场景默认使用该会话,则可能希望在After块中将其重置为:default。
# 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2683 次 |
| 最近记录: |