我正在使用cocoon来生成表单中的嵌套字段,但是我很难选择动态生成的输入字段进行测试。我假设id和name的数字部分(默认情况下是相同的)基于时间戳,所以我推断它需要正确选择正则表达式。 提前感谢帮助。
_form.html.erb
<% if question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(question.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% question.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
Body: <%= f.text_field :body %>
</p>
<p>
Notes: <%= f.text_field :notes %>
</p>
<p>
Distractors
</p>
<hr>
<div class="distractors">
</div>
<div class="actions">
<%= link_to_add_association('Add distractor', f, :distractors) %>
<%= f.submit %>
</div>
<% end %>
</div>
_distractor_fields.html.erb(根据茧约定)
<div class="nested-fields form-control">
<%= f.text_field :body, class: "distractor_body" %>
<%= f.radio_button :correct, 'true', label: true%>
<%= link_to_remove_association "Delete distractor", f%>
</div>
相关黄瓜步骤
Then(/^I fill the distractor field with "([^"]*)"$/) do |distractor_body|
expect(page).to have_css('.distractor_body')#Just to see if it works
page.should have_css("input", id: 'question[distractors_attributes][[0-9]*][body]')#Fails here
page.find('#/question\[distractors_attributes\]\[[0-9]*\]\[body\]/', class: '.distractor_body')#Another failing attempt
fill_in('.distractor_body', with: distractor_body)
# Cocoon generated id and name looks like this:
# question[distractors_attributes][1494262782667][body]
page.choose('correct')
end
最终生成的HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Untitled</title>
<link rel="stylesheet" media="screen" href="/assets/_settings.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" />
<link rel="stylesheet" media="screen" href="/assets/bootstrap_and_overrides.self-78b811a59aab8664061acba3353fae061b154dec619ec9a7b47714ce5f737bc2.css?body=1" />
<link rel="stylesheet" media="screen" href="/assets/foundation_and_overrides.self-ec376498c201e4ea8c1b76053444700483ab234ced1658f9a5e717f9a93fbe32.css?body=1" />
<link rel="stylesheet" media="screen" href="/assets/questions.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" />
<link rel="stylesheet" media="screen" href="/assets/welcome.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" />
<link rel="stylesheet" media="screen" href="/assets/application.self-0e31f1d30f689436ae56e463f6474eb99704af8bbed5fc320a66ac33afb59c2b.css?body=1" />
<script src="/assets/jquery.self-bd7ddd393353a8d2480a622e80342adf488fb6006d667e8b42e4c0073393abee.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/jquery_ujs.self-784a997f6726036b1993eb2217c9cb558e1cbb801c6da88105588c56f13b466a.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.core.self-4f457971ba9c2597b09f6a22a24331c38e7279daa591bd61b5bbf28876aa8fc3.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.abide.self-679179bf3ee1902bc3fe7da64bbb2b731fa4df4a75901538ba55e7d25286313e.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.accordion.self-8d244ceae0d451fcc15789a5e9e36ebed60650c8a9abbf5f9a126b6f327b6154.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.accordionMenu.self-d9a7c97eb11b52fe305e71d2becbdfaf772722f9da6bd56ad24121919d5e43ca.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.drilldown.self-1197ab1398f5a6e188d25902ca65cb079c5c769583801244c456e39aeb484876.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.dropdown.self-c0e613ea4f5cf333f28aa6e97d54dcf6f739b7b95beed9175870525fbb190200.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.dropdownMenu.self-386455a3346e601e0a524f9de7e5146e73ef63055ba5394290f5f9a3a2cb74d7.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.equalizer.self-7221ea2a8fba1b3495851e00b58ade3f76ce11a8230ab5cf9ca81a39e7eadd4e.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.interchange.self-be09943d6d46443136cd84ffda8b5b8a008db8355c144097949fcaf7d5289a56.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.magellan.self-789eaceb55bd54bd0ed8fea6adf2b18c21464581b0e95eaa8a5aff1130208b2d.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.offcanvas.self-9ce0609e0e4404d19bde3425bf63955c74836aa9f0772d9b7e26ab731c1f2e8c.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.orbit.self-a127e190c8224d789844ee10a7c4123fe5aef28bc5bf75e9e185b3e098651078.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.responsiveMenu.self-c16f1dc4c3fa8e4881e116d1072642a8f0d53db8725994fd5a32f5cab1a30991.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.responsiveToggle.self-e997867128d1cc2b0268d6d8a0b3d1d4acdd8b18481dbcc50e8c41b06dea77d1.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.reveal.self-958c10cf13f0fa316d5753a6c03b7e1c6c76819b5978cefa85df960856744a98.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.slider.self-130c3657134bb131ceec80b3553bfe98731d170c5b6b89506f7aeca0934bf3ab.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.sticky.self-6132d9675d96d8eb9d6426a329ba49a9c70756620079fcf1e9dc0509d710ff3b.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.tabs.self-76b83dbeb1a526b713949431c11539c4f15ea95620a32b357452afb4e969778d.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.toggler.self-32c8ba5ef7c4612d3cf417a6fb5ff296ee2fc8a1778995f4571c5ca3aed7c1a5.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.tooltip.self-34253627e75f9473117d9e2f269e29eb8871e50679a25ee82736fb52acf0f65a.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.util.box.self-29f7cbc36d64afedbf3295384f579c067ee4a6107059d25041cf9c2ffbdac287.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.util.keyboard.self-4a273ab1fd2edf1b86d3e772465bff74a28c721021c719eb48de76cf9a220d9c.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.util.mediaQuery.self-f587619a109ff8be49d8eacb95b733d75cbdcc9819a19ce8c2af1be48e65524b.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.util.motion.self-65f3e3cc6f7990ae5a0d964eb90d626e0a5486508a11487145ba14aa4b2a2e78.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.util.nest.self-10050c8cffe586f45b7cb4570a5aa0d498e566ac981f4e494bd42246b4daafc7.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.util.timerAndImageLoader.self-0cb4d35725065a9a79f69d241210298190b5f1e134522d565a872f984e1e26b8.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.util.touch.self-95e098450fe69653246bdfa7f1e7fe7dba15348524771d7b2c54c7c4298fccb5.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.util.triggers.self-4fef1a2bcee50931073341abc76d0747ace3559d818342a079c07c54e27f8add.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.zf.responsiveAccordionTabs.self-1cd003cd23ec70291aa4b3ee3c46097d1fddb74afe524d057af1339b0251bdc9.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/foundation.self-5ecf2f4d83e6260dabd6ec48e76d8ddebccf956563f34072221bf960d3b8c255.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/turbolinks.self-c5acd7a204f5f25ce7a1d8a0e4d92e28d34c9e2df2c7371cd7af88e147e4ad82.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/cocoon.self-6874ad698cefd21cc1119b773550f61b5c1a60396460f015222af59293affe80.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/bootstrap.self-fdc98dee79ee88255e10cac6caa91338165cb76cf0d263744d8d90011fc2ef8f.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/action_cable.self-5454023407ffec0d29137c7110917e1e745525ae9afbc05f52104c4cd6597429.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/cable.self-6e0514260c1aa76eaf252412ce74e63f68819fd19bf740595f592c5ba4c36537.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/questions.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/welcome.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/application.self-c9d32bb43ab90153f07e848d3f326b7933d8ef0585cb9b8e9da6cb92ecb052a9.js?body=1" data-turbolinks-track="true"></script>
<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="qqzhq9qIk6xYZ24ic8AyIk5TTJYL2zI6pfhPKlGafE8cYveT4ITi1vodAxQxco5h06gg2M6N/F1opEvswl0xwQ==" />
</head>
<body>
<h1>New Question</h1>
<div class="row">
<form class="new_question" id="new_question" action="/questions" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓" /><input type="hidden" name="authenticity_token" value="5zxWU69klqpU0gKvE3hnIDdNuyB7wrY1nmoYUn5ojABXKR3IroJ4UpiD/P167VcyOKYg2wHiAGT6dGfh+N7ndg==" />
<p>
Body: <input type="text" name="question[body]" id="question_body" />
</p>
<p>
Notes: <input type="text" name="question[notes]" id="question_notes" />
</p>
<p>
Distractors
</p>
<hr>
<div class="distractors">
</div>
<div class="actions">
<a class="add_fields" data-association="distractor" data-associations="distractors" data-association-insertion-template=" <div class="nested-fields form-control">
<input class="distractor_body" type="text" name="question[distractors_attributes][new_distractors][body]" id="question_distractors_attributes_new_distractors_body" />
<input label="true" type="radio" value="true" name="question[distractors_attributes][new_distractors][correct]" id="question_distractors_attributes_new_distractors_correct_true" />
<input type="hidden" name="question[distractors_attributes][new_distractors][_destroy]" id="question_distractors_attributes_new_distractors__destroy" value="false" /><a class="remove_fields dynamic" href="#">Delete distractor</a>
</div>
" href="#">Add distractor</a>
<input type="submit" name="commit" value="Create Question" data-disable-with="Create Question" />
</div>
</form></div>
<a href="/questions">Back</a>
</body>
</html>
答案 0 :(得分:1)
Capybara不支持id或name属性的正则表达式(并且你的示例尝试实际上显示你传递字符串而不是正则表达式)。处理动态生成的元素时,关键通常是通过已知元素来定位元素,这些元素允许您通过类或其他已知属性选择单个元素。在您的情况下,示例HTML实际上并未显示您尝试填写的输入实例,但假设嵌套字段模板的实例动态插入到.distractors
div中以下几行应该有效
find('.distractors .nested-fields:first-child .distractor_body').set(distractor_body) # use last-child, nth-child to select the correct section if multiple
在您需要的步骤中使用此功能时
选择单选按钮也可能最好使用within
Then(/^I fill the distractor field with "([^"]*)"$/) do |distractor_body|
within('.distractors .nested-fields:first-child') do
find('.distractor_body').set(distractor_body)
choose('correct')
end
end