【发布时间】:2018-04-16 04:09:45
【问题描述】:
我有一个 rspec 测试,它试图以弹出模式形式填写一个字段。相关行是
fill_in 'Name', with: 'Jo Blo'
错误信息是
Capybara::ElementNotFound:
Unable to find visible field "Name" that is not disabled
这意味着它正在查找该字段但将其视为已禁用。但是,如果我在失败时使用 capybara-screenshot 来查看页面,则该字段是可见的,并且该字段的 html 是
<div class="field">
<label for="user_name">Name</label><sup>*</sup><br>
<input type="text" name="user[name]" id="user_name">
</div>
即它没有被禁用。
该字段位于div#modal_new_user 中。如果我将测试更改为
expect(page).to have_css '#modal_new_user'
fill_in 'Name', with: 'Jo Blo'
错误信息变为
Failure/Error: expect(page).to have_css '#modal_new_user'
expected to find visible css "#modal_new_user" but there were no
matches. Also found "", which matched the selector but not all filters.
这似乎暗示没有找到模态框,但同样,如果我查看水豚屏幕截图,模态框的 html 是可见的。 我该如何解决这个问题?
我正在使用 capybara 3.0.2 和 rspec-rails 3.7.2。
bootstrap modal 的代码是
<div id='modal_new_user' class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Warning</h4>
<p>enter code here Right now there is a 10 day free trial. Just fill in the form below.</p>
</div>
<div class="modal-body">
<form class="edit_user" id="edit_user_12004" action="/users" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓" /><input type="hidden" name="authenticity_token" value="306T/5kB9pqb9sW6mdW+tXyCzfydEF2tf8wQMJXEICO5uQ/YEDVVYqg12l7Qe+lU4uELZy/d1VIhzZObk/FVcQ==" />
<div class="field">
<label for="user_name">Name</label><sup>*</sup><br />
<input type="text" name="user[name]" id="user_name" />
</div>
<div class="field">
<label for="user_email">Email</label><sup>*</sup><br />
<input type="email" name="user[email]" id="user_email" />
</div>
<div class="field">
<label for="user_password">Password</label><sup>*</sup><br />
<input type="password" name="user[password]" id="user_password" />
</div>
<p id="terms">By creating your account, you are agreeing to the site <a target="_blank" href="/legal">Terms and Conditions</a>. You will not be spammed, we dislike spam as much as you do.
</p>
<input type="hidden" value="clubs#edit" name="user[referral]" id="user_referral" />
<div class="actions">
<input type="submit" name="commit" value="Join Now" class="btn btn-cta btn-cta-primary" data-disable-with="Join Now" />
</div>
</form> </div>
<div class="modal-footer right_close_button">
<button type="button" class="btn btn-info" data-dismiss="modal">Close</button>
</div>
</div>
【问题讨论】:
-
包含
#modal_new_user的实际 HTML 是什么?和“无法找到未禁用的可见字段“名称””并不意味着看到字段“名称”而是禁用,这意味着它要么看不到该字段,要么它确实看到了该字段,但它被禁用 - 不要阅读更多错误信息。 -
这可能是时间问题。您必须记住,水豚开始以极快的速度点击东西,这通常是在 JS 完成之前。尝试使用
find('#modal_new_user').fill_in('Name', with: 'Jo Blo'),因为这将等待元素出现。 -
@max
fill_in也将等待匹配元素(最多 Capybara.default_max_wait_time 秒),因为它被实现为find(...).set(..) -
我有 Capybara.default_max_wait_time = 5。
-
我已经添加了模态的代码。
标签: ruby-on-rails rspec capybara