我使用水豚编写了一些测试,以使用 poltergeist 和 phantomjs 作为 javascript 驱动程序进行请求测试。
以下填写登录表单的步骤在没有 js 的情况下效果很好:
it "signs in" do
visit new_user_session_path
fill_in "Email", with: user
fill_in "Password", with: password||"foobar"
click_button "Login"
end
如果我用 js 定义我的测试,it "signs in", js: true do
我的测试将失败并出现错误:
Capybara::ElementNotFound: Unable to find field "Email"
登录表单本身是使用 simple_form 作为表单生成器和前端的引导程序构建的。这些字段没有标签。搜索文本仅包含在占位符属性中。
= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f|
= f.input :email, :placeholder => User.human_attribute_name(:email), :label => false, :input_html => {:class => 'input-xlarge'}
= f.input :password, :placeholder => User.human_attribute_name(:password), :label => false, :input_html => {:class => 'input-xlarge'}
%div
= f.button :submit, "Login", :class => 'btn btn-large btn-primary'
此代码生成以下 html 代码
<form accept-charset="UTF-8" action="/users/login" class="simple_form new_user" id="new_user" method="post" novalidate="novalidate">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓">
</div>
<div class="control-group email required user_email">
<div class="controls">
<input class="string email required input-xlarge" id="user_email" name="user[email]" placeholder="Email" size="50" type="email" value="">
</div>
</div>
<div class="control-group password required user_password">
<div class="controls">
<input class="password required input-xlarge" id="user_password" name="user[password]" placeholder="Password" size="50" type="password">
</div>
</div>
<div>
<input class="btn btn btn-large btn-primary" name="commit" type="submit" value="Login">
</div>
</form>
即使激活了js,您是否有任何想法如何确保找到字段?