Capybara Action 整数 (id) 与字符串 (name) 混淆
Capybara Action integer (id) vs. string (name) confusion
我的部分测试需要select从这个表单字段中进入大学:
<div class="js-dependent-fields" data-select-id="user_role" data-option-value="staff">
<div class="form-group">
<%= form.label :university %>
<%= form.select :university_id, options_for_select(University.all.map{ |uni| [uni.name, uni.id] }), {prompt: 'Select Your University'}, { class: "form-control" } %>
</div>
</div>
这是实际的 html:
<div class="js-dependent-fields" data-select-id="user_role" data-option-value="staff">
<div class="form-group">
<label for="user_university">University</label>
<select class="form-control" name="user[university_id]" id="user_university_id"><option value="">Select Your University</option>
<option value="1">Harvard</option></select>
</div>
这是我遇到的错误:
test_signup_valid_user ERROR (0.05s)
Capybara::ElementNotFound: Capybara::ElementNotFound: Unable to find visible option "Harvard University"
test/integration/boarding_flow_test.rb:63:in `block in <class:BoardingFlowTest>'
提交表单时,这是作为参数的一部分传递的示例
"university_id"=>"4"
在下面的测试中,我想select一所大学,我已经尝试了我能想到的所有组合,例如:
page.select "3", from: "university_id"
page.select "Harvard University", from: "University"
但我似乎无法让它工作。这样做的正确方法是什么?谢谢!
test 'signup valid user' do
visit sign_up_path
fill_in "First name", with: "John"
fill_in "Last name", with: "Doe"
fill_in "Email", with: "approved@doe.com"
fill_in "Password", with: "foobar"
page.select "Staff", from: "Role"
page.select "3", from: "university_id"
click_on "Signup"
assert_equal sign_in_path, current_path
assert page.has_content?("confirm your email")
end
这里是users/new.html.erb
<div class="row">
<div class="col-sm-8 col-md-4 col-sm-offset-2 col-md-offset-4">
<div class="login-panel panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Signup</h3>
</div>
<div class="panel-body">
<%= form_for @user do |form| %>
<fieldset>
<%= render partial: '/users/form', object: form %>
<%= form.submit t(".submit"), class: "btn btn-md btn-success btn-block" %>
<hr>
<h5 class="text-center">Already have an account?</h5>
<%= link_to t(".login"), sign_in_path, class: "btn btn-md btn-info btn-block" %>
</fieldset>
<% end %>
</div>
</div>
</div>
</div>
这是部分形式
<div class="form-group">
<%= form.label :first_name %>
<%= form.text_field :first_name, class: "form-control" %>
</div>
<div class="form-group">
<%= form.label :last_name %>
<%= form.text_field :last_name, class: "form-control" %>
</div>
<div class="form-group">
<%= form.label :email %>
<%= form.text_field :email, type: 'email', class: "form-control" %>
</div>
<div class="form-group">
<%= form.label :password %>
<%= form.password_field :password, class: "form-control" %>
</div>
<div class="form-group">
<%= form.label :role %>
<%= form.select :role, options_for_select(User.roles.keys.map{ |role| [User.human_attribute_name("role.#{role}"), role] }), {}, { class: "form-control" } %>
</div>
<div class="js-dependent-fields" data-select-id="user_role" data-option-value="staff">
<div class="form-group">
<%= form.label :university %>
<%= form.select :university_id, options_for_select(University.all.map{ |uni| [uni.name, uni.id] }), {prompt: 'Select Your University'}, { class: "form-control" } %>
</div>
</div>
使用 page.select
时 - 使用 'option' select 或类型 - https://github.com/teamcapybara/capybara/blob/master/lib/capybara/selector.rb#L413 - and the :from
option is matched using the 'select' selector type - https://github.com/teamcapybara/capybara/blob/master/lib/capybara/selector.rb#L358 匹配主要参数。这意味着 :from
选项将匹配 HTML <select>
元素的名称、ID、占位符或相关标签文本,而主要参数将匹配 select 的文本能的选择。您没有显示页面的实际 HTML 或您调用 'form.select' 的 form
对象的详细信息 - 但如果它是用户对象,它可能类似于
page.select("Harvard", from: 'user_university_id') # assuming the id of the actual select is 'user_university_id'
如果页面上只有一个select选项"Harvard University",你也可以直接
page.select "Harvard"
您的尝试失败的原因是 -
# Doesn't work because "3" isn't the readable text of the option and
# 'university_id' is not the id of the select element
page.select "3", from: "university_id"
# Doesn't work because "University" is not the label text associated
# with the select. This is because your partials label line would need to be
# form.label :university_id, "University"
# to link the label with the university_id select element. Additionally
# "Harvard University" isn't actually the text shown in your HTML - it's just "Harvard"
page.select "Harvard University", from: "University"
这一切都假设页面上的可见元素实际上是一个 <select>
元素,而不是一个 JS 驱动的小部件(包装 js-dependent-fields
的 class div 往往表明它可能是)。如果可见元素 is/are 一个 JS 小部件,那么这一切都会消失 window 因为 page.select
只适用于 HTML <select>
元素,你将需要显示页面上可见 select 小部件的实际 HTML。
我的部分测试需要select从这个表单字段中进入大学:
<div class="js-dependent-fields" data-select-id="user_role" data-option-value="staff">
<div class="form-group">
<%= form.label :university %>
<%= form.select :university_id, options_for_select(University.all.map{ |uni| [uni.name, uni.id] }), {prompt: 'Select Your University'}, { class: "form-control" } %>
</div>
</div>
这是实际的 html:
<div class="js-dependent-fields" data-select-id="user_role" data-option-value="staff">
<div class="form-group">
<label for="user_university">University</label>
<select class="form-control" name="user[university_id]" id="user_university_id"><option value="">Select Your University</option>
<option value="1">Harvard</option></select>
</div>
这是我遇到的错误:
test_signup_valid_user ERROR (0.05s)
Capybara::ElementNotFound: Capybara::ElementNotFound: Unable to find visible option "Harvard University"
test/integration/boarding_flow_test.rb:63:in `block in <class:BoardingFlowTest>'
提交表单时,这是作为参数的一部分传递的示例
"university_id"=>"4"
在下面的测试中,我想select一所大学,我已经尝试了我能想到的所有组合,例如:
page.select "3", from: "university_id"
page.select "Harvard University", from: "University"
但我似乎无法让它工作。这样做的正确方法是什么?谢谢!
test 'signup valid user' do
visit sign_up_path
fill_in "First name", with: "John"
fill_in "Last name", with: "Doe"
fill_in "Email", with: "approved@doe.com"
fill_in "Password", with: "foobar"
page.select "Staff", from: "Role"
page.select "3", from: "university_id"
click_on "Signup"
assert_equal sign_in_path, current_path
assert page.has_content?("confirm your email")
end
这里是users/new.html.erb
<div class="row">
<div class="col-sm-8 col-md-4 col-sm-offset-2 col-md-offset-4">
<div class="login-panel panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Signup</h3>
</div>
<div class="panel-body">
<%= form_for @user do |form| %>
<fieldset>
<%= render partial: '/users/form', object: form %>
<%= form.submit t(".submit"), class: "btn btn-md btn-success btn-block" %>
<hr>
<h5 class="text-center">Already have an account?</h5>
<%= link_to t(".login"), sign_in_path, class: "btn btn-md btn-info btn-block" %>
</fieldset>
<% end %>
</div>
</div>
</div>
</div>
这是部分形式
<div class="form-group">
<%= form.label :first_name %>
<%= form.text_field :first_name, class: "form-control" %>
</div>
<div class="form-group">
<%= form.label :last_name %>
<%= form.text_field :last_name, class: "form-control" %>
</div>
<div class="form-group">
<%= form.label :email %>
<%= form.text_field :email, type: 'email', class: "form-control" %>
</div>
<div class="form-group">
<%= form.label :password %>
<%= form.password_field :password, class: "form-control" %>
</div>
<div class="form-group">
<%= form.label :role %>
<%= form.select :role, options_for_select(User.roles.keys.map{ |role| [User.human_attribute_name("role.#{role}"), role] }), {}, { class: "form-control" } %>
</div>
<div class="js-dependent-fields" data-select-id="user_role" data-option-value="staff">
<div class="form-group">
<%= form.label :university %>
<%= form.select :university_id, options_for_select(University.all.map{ |uni| [uni.name, uni.id] }), {prompt: 'Select Your University'}, { class: "form-control" } %>
</div>
</div>
使用 page.select
时 - 使用 'option' select 或类型 - https://github.com/teamcapybara/capybara/blob/master/lib/capybara/selector.rb#L413 - and the :from
option is matched using the 'select' selector type - https://github.com/teamcapybara/capybara/blob/master/lib/capybara/selector.rb#L358 匹配主要参数。这意味着 :from
选项将匹配 HTML <select>
元素的名称、ID、占位符或相关标签文本,而主要参数将匹配 select 的文本能的选择。您没有显示页面的实际 HTML 或您调用 'form.select' 的 form
对象的详细信息 - 但如果它是用户对象,它可能类似于
page.select("Harvard", from: 'user_university_id') # assuming the id of the actual select is 'user_university_id'
如果页面上只有一个select选项"Harvard University",你也可以直接
page.select "Harvard"
您的尝试失败的原因是 -
# Doesn't work because "3" isn't the readable text of the option and
# 'university_id' is not the id of the select element
page.select "3", from: "university_id"
# Doesn't work because "University" is not the label text associated
# with the select. This is because your partials label line would need to be
# form.label :university_id, "University"
# to link the label with the university_id select element. Additionally
# "Harvard University" isn't actually the text shown in your HTML - it's just "Harvard"
page.select "Harvard University", from: "University"
这一切都假设页面上的可见元素实际上是一个 <select>
元素,而不是一个 JS 驱动的小部件(包装 js-dependent-fields
的 class div 往往表明它可能是)。如果可见元素 is/are 一个 JS 小部件,那么这一切都会消失 window 因为 page.select
只适用于 HTML <select>
元素,你将需要显示页面上可见 select 小部件的实际 HTML。