Mic*_* Ef 5 rspec capybara ruby-on-rails-3 phantomjs twitter-bootstrap-rails
我使用使用poltergeist和phantomjs作为javascript驱动程序的请求测试使用capybara编写了一些测试.
填写登录表单的以下步骤在没有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
Run Code Online (Sandbox Code Playgroud)
如果我用js定义我的测试,it "signs in", js: true do我的测试失败并出现错误:
Capybara::ElementNotFound: Unable to find field "Email"
Run Code Online (Sandbox Code Playgroud)
登录表单本身是使用simple_form作为表单生成器和前端的bootstrap构建的.这些字段没有标签.搜索文本仅包含在占位符属性中.
= 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'
Run Code Online (Sandbox Code Playgroud)
此代码生成以下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>
Run Code Online (Sandbox Code Playgroud)
您是否有任何想法如何确保即使js被激活也能找到字段?
我不知道你的Webrat测试是如何通过的。根据我的经验,如果没有匹配的标签或 ID,水豚就找不到“电子邮件”。
在你的情况下,由于你不使用标签,我建议你找到带有 id 的字段
fill_in "user_email", with user.email
# user_email is the id created by simple_form in general case. Verify yours.
# Don't need "#" before id.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2611 次 |
| 最近记录: |