好的,所以我正在使用capybara + rails 3 + rspect
我正在尝试登录我的网站,但即使我在使用fill_in
方法时没有收到错误,我也会收到click_on
方法的错误,因为它可以找不到我想点击的元素。
这是我的HTML代码:
<form accept-charset="UTF-8" action="/" class="filter_form" id="login" method="post">
<fieldset>
<div class="modal-body">
<div class="clearfix login-fields">
<label for="user_email">Email</label>
<div class="input login-inputs">
<input class="input-text" id="user_email" name="user[email]" placeholder="email" size="30" type="email" value="">
</div>
</div>
<div class="clearfix login-fields">
<label for="user_password">Password</label>
<div class="input login-inputs">
<input class="input-text" id="user_password" name="user[password]" placeholder="password" size="30" type="password">
</div>
</div>
</div>
<div class="modal-footer">
<input class="btn btn-primary login_btn" id="btn_login" name="commit" type="submit" value="Sign in">
<a href="/lms/forgot_password" class="btn">Forgot password...</a>
<a href="#" class="btn close cancel" data-dismiss="modal">Cancel</a>
</div>
</fieldset>
</form>
这是测试:
describe "Login ", js: true, type: :request do
it "should login correctly if the right credentials are given" do
Capybara.default_wait_time = 15
Capybara.ignore_hidden_elements = false
visit new_user_session_path
fill_in 'user_email', :with => 'example@test.com'
fill_in 'user_password', :with => 'pwd123'
p page.body
click_on 'Sign in'
visit '/'
save_and_open_page
response.should have_content('Welcome')
end
end
最糟糕的是,当我在p page.body
之前click_on
时,我看到以下代码:
<html xmlns=\\\"http://www.w3.org/1999/xhtml\\\"><head></head><body></body></html>\
但是如果我为p response.body.inspect
更改了那个,那么我会得到页面的完整html代码......,visit
方法不应该在page
上加载响应的内容}?
任何帮助将不胜感激,这让我发疯:)
答案 0 :(得分:1)
添加该行:
config.include Capybara::DSL
to spec / spec_helper.rb:
RSpec.configure do |config|
.....
config.include Capybara::DSL
.....
end