水豚歧义匹配错误
Capybara ambiguous match errors
该项目是对原版的重新启动,我正在努力掌握各种精华 - rails 作曲家在所有方面都做得非常出色,但我对正在发生的事情有所了解。
我收到与注册代码相关的 Capybara 错误(我有几个使用调用的规范,但这里只包含其中一个,因为它们都调用同一个帮助程序)。
Capybara::Ambiguous:
Ambiguous match, found 2 elements matching field "Password"
我查看了 Whosebug(数次),并使用了 Messr 的强大功能 Google - 他主要建议我看这里。
起初我以为是
不匹配
fill_in 'Email', with: email
和
fill_in 'Email', :with => email
但两者似乎都没有什么不同。
通过阅读 rubydoc.info capybara fill-in 的文档(我知道:羞愧地低下了头),填充需要一个定位器和一个选项数组。
我的理解是 fill_in 'Password', with: password
会匹配 Password
定位符,即 html 的 id="user_password"
部分。我在页面上没有看到任何其他人,所以我现在寻求帮助。
我所有的代码都在我项目的 SO001 分支上Github project branch to match this question。
sign_up_spec.rb
feature 'Sign Up', :devise do
# Scenario: Visitor can sign up with valid email address and password
# Given I am not signed in
# When I sign up with a valid email address and password
# Then I see a successful sign up message
scenario 'visitor can sign up with valid email address and password' do
sign_up_with('test@example.com', 'please123', 'please123')
txts = [I18n.t( 'devise.registrations.signed_up'), I18n.t( 'devise.registrations.signed_up_but_unconfirmed')]
expect(page).to have_content(/.*#{txts[0]}.*|.*#{txts[1]}.*/)
end
# ... others snipped for space
end
spec/support/helpers/session_helpers.rb
module Features
module SessionHelpers
def sign_up_with(email, password, confirmation)
visit new_user_registration_path
save_and_open_page
fill_in 'Email', with: email
fill_in 'Password', with: password
fill_in 'Password confirmation', :with => confirmation
click_button 'Sign up'
end
def signin(email, password)
visit new_user_session_path
fill_in 'Email', with: email
fill_in 'Password', with: password
click_button 'Sign in'
end
end
end
设计new.html.erb代码是
注册
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :email, required: true, autofocus: true %>
<%= f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length) %>
<%= f.input :password_confirmation, required: true %>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
html的表格部分有
<form novalidate="novalidate" class="simple_form new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓" />
<div class="form-inputs">
<div class="form-group email required user_email">
<label class="control-label email required" for="user_email">
<abbr title="required">*</abbr> Email
</label>
<input class="form-control string email required" autofocus="autofocus" required="required" aria-required="true" type="email" value="" name="user[email]" id="user_email" />
</div>
<div class="form-group password required user_password">
<label class="control-label password required" for="user_password">
<abbr title="required">*</abbr>
Password</label>
<input class="form-control password required" required="required" aria-required="true" type="password" name="user[password]" id="user_password" />
<p class="help-block">6 characters minimum</p>
</div>
<div class="form-group password required user_password_confirmation">
<label class="control-label password required" for="user_password_confirmation">
<abbr title="required">*</abbr>
Password confirmation
</label>
<input class="form-control password required" required="required" aria-required="true" type="password" name="user[password_confirmation]" id="user_password_confirmation" />
</div>
</div>
<div class="form-actions">
<input type="submit" name="commit" value="Sign up" class="btn btn-default" data-disable-with="Sign up" />
</div>
</form>
如文档所述,Capybara 的 fill_in
将根据其 id 或 name 属性或关联标签元素的文本匹配元素。属性匹配需要精确匹配,关联标签元素的文本可能是精确匹配或子字符串匹配,具体取决于几个选项的设置。
在 Capybara 的当前版本中,Capybara.exact
默认为 :smart,当查找关联的标签文本匹配时将首先尝试完全匹配,如果找到 none 将尝试子串匹配。在您的情况下,您的两个字段的标签文本是 *Password
和 *Password confirmation
,当检查与 Password
的完全匹配时失败,当检查子字符串匹配时两者都匹配 - 因此"Ambiguous match" 错误,因为不止一个元素匹配。
要解决此问题(同时仍在使用 fill_in
),您可以执行以下任一操作
fill_in '*Password', with: password # will match exactly on one elements label text
find('div.user_password').fill_in 'Password' with: password # scope fill_in so substring match is unique
fill_in 'user_password', with: password # match on id attribute
fill_in 'user[password]', with: password # match on name attribute
该项目是对原版的重新启动,我正在努力掌握各种精华 - rails 作曲家在所有方面都做得非常出色,但我对正在发生的事情有所了解。
我收到与注册代码相关的 Capybara 错误(我有几个使用调用的规范,但这里只包含其中一个,因为它们都调用同一个帮助程序)。
Capybara::Ambiguous:
Ambiguous match, found 2 elements matching field "Password"
我查看了 Whosebug(数次),并使用了 Messr 的强大功能 Google - 他主要建议我看这里。
起初我以为是
不匹配fill_in 'Email', with: email
和
fill_in 'Email', :with => email
但两者似乎都没有什么不同。
通过阅读 rubydoc.info capybara fill-in 的文档(我知道:羞愧地低下了头),填充需要一个定位器和一个选项数组。
我的理解是 fill_in 'Password', with: password
会匹配 Password
定位符,即 html 的 id="user_password"
部分。我在页面上没有看到任何其他人,所以我现在寻求帮助。
我所有的代码都在我项目的 SO001 分支上Github project branch to match this question。
sign_up_spec.rb
feature 'Sign Up', :devise do
# Scenario: Visitor can sign up with valid email address and password
# Given I am not signed in
# When I sign up with a valid email address and password
# Then I see a successful sign up message
scenario 'visitor can sign up with valid email address and password' do
sign_up_with('test@example.com', 'please123', 'please123')
txts = [I18n.t( 'devise.registrations.signed_up'), I18n.t( 'devise.registrations.signed_up_but_unconfirmed')]
expect(page).to have_content(/.*#{txts[0]}.*|.*#{txts[1]}.*/)
end
# ... others snipped for space
end
spec/support/helpers/session_helpers.rb
module Features
module SessionHelpers
def sign_up_with(email, password, confirmation)
visit new_user_registration_path
save_and_open_page
fill_in 'Email', with: email
fill_in 'Password', with: password
fill_in 'Password confirmation', :with => confirmation
click_button 'Sign up'
end
def signin(email, password)
visit new_user_session_path
fill_in 'Email', with: email
fill_in 'Password', with: password
click_button 'Sign in'
end
end
end
设计new.html.erb代码是
注册
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :email, required: true, autofocus: true %>
<%= f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length) %>
<%= f.input :password_confirmation, required: true %>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
html的表格部分有
<form novalidate="novalidate" class="simple_form new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓" />
<div class="form-inputs">
<div class="form-group email required user_email">
<label class="control-label email required" for="user_email">
<abbr title="required">*</abbr> Email
</label>
<input class="form-control string email required" autofocus="autofocus" required="required" aria-required="true" type="email" value="" name="user[email]" id="user_email" />
</div>
<div class="form-group password required user_password">
<label class="control-label password required" for="user_password">
<abbr title="required">*</abbr>
Password</label>
<input class="form-control password required" required="required" aria-required="true" type="password" name="user[password]" id="user_password" />
<p class="help-block">6 characters minimum</p>
</div>
<div class="form-group password required user_password_confirmation">
<label class="control-label password required" for="user_password_confirmation">
<abbr title="required">*</abbr>
Password confirmation
</label>
<input class="form-control password required" required="required" aria-required="true" type="password" name="user[password_confirmation]" id="user_password_confirmation" />
</div>
</div>
<div class="form-actions">
<input type="submit" name="commit" value="Sign up" class="btn btn-default" data-disable-with="Sign up" />
</div>
</form>
如文档所述,Capybara 的 fill_in
将根据其 id 或 name 属性或关联标签元素的文本匹配元素。属性匹配需要精确匹配,关联标签元素的文本可能是精确匹配或子字符串匹配,具体取决于几个选项的设置。
在 Capybara 的当前版本中,Capybara.exact
默认为 :smart,当查找关联的标签文本匹配时将首先尝试完全匹配,如果找到 none 将尝试子串匹配。在您的情况下,您的两个字段的标签文本是 *Password
和 *Password confirmation
,当检查与 Password
的完全匹配时失败,当检查子字符串匹配时两者都匹配 - 因此"Ambiguous match" 错误,因为不止一个元素匹配。
要解决此问题(同时仍在使用 fill_in
),您可以执行以下任一操作
fill_in '*Password', with: password # will match exactly on one elements label text
find('div.user_password').fill_in 'Password' with: password # scope fill_in so substring match is unique
fill_in 'user_password', with: password # match on id attribute
fill_in 'user[password]', with: password # match on name attribute