如何使用capybara在HTML文件的两个表单之一中填写_in字段?

如何使用capybara在HTML文件的两个表单之一中填写_in字段?,html,forms,testing,capybara,Html,Forms,Testing,Capybara,我在一个html文件中有两个表单(注册和登录),我正在使用Capybara的fill\u in语法对其进行测试。然而,两者都有一个用户名字段和一个密码字段,我得到了不明确的匹配,找到了两个匹配字段的元素:username。在spec helper方法中,我如何确保填写正确表单的字段。我想我需要id=每个表单,但是我找不到任何有水豚语法的地方 我的html <form id="signUpForm"action='/user' method="post"> <!-- more

我在一个html文件中有两个表单(注册和登录),我正在使用Capybara的
fill\u in
语法对其进行测试。然而,两者都有一个用户名字段和一个密码字段,我得到了不明确的匹配,找到了两个匹配字段的元素:username。在spec helper方法中,我如何确保填写正确表单的字段。我想我需要
id=
每个表单,但是我找不到任何有水豚语法的地方

我的html

<form id="signUpForm"action='/user' method="post">
  <!-- more fields -->
  <input class="entryField" type="text" name="username" required>
  <input class="entryField" type="password" name="password" required>
  <!-- more fields -->
  <input type="submit" value="Sign up">
</form>

<form id="signInForm" action="/session" method="post">
  <input class="entryField" type="text" name="username" placeholder="Username">
  <input class="entryField" type="password" name="password" placeholder="Password">
  <input type="submit" value="Sign in">
</form>

您需要将“填写”说明的范围限定到特定表单。您可以使用Capybara的#在语法中执行此操作,如下所述:

因此,您的#登录帮助程序可能是:

def sign_in user
  visit '/'
  within('form#signInForm') do 
    fill_in :username, with: user.username
    fill_in :password, with: user.password
    click_button 'Sign in'
  end
end
def sign_in user
  visit '/'
  within('form#signInForm') do 
    fill_in :username, with: user.username
    fill_in :password, with: user.password
    click_button 'Sign in'
  end
end