Del*_*man 21 testing rspec ruby-on-rails capybara
背景:我正在使用Capybara和Rspec来测试Rails 3应用程序. 使用的驱动程序:Selenium
问题:我无法找到"登录"按钮,以便在我的测试中点击.
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>
Run Code Online (Sandbox Code Playgroud)
测试失败
it "should login correctly if the right credentials are given", :js => true do
Capybara.default_wait_time = 5
Capybara.reset_sessions!
visit '/'
click_link('login_link') #this will bring a modal window with the code posted above using js
within("#login") do
fill_in 'user_email', :with => "my-email@example.com"
fill_in 'user_password', :with => "mypwd"
end
response.should have_selector('input#btn_login') #This passes
click_on("input#btn_login") #Here it fails, saying it can't find an element with that selector
response.should have_selector(:xpath, '//div[@class="alert-message block-message info"]')
end
Run Code Online (Sandbox Code Playgroud)
我的测试文件在里面spec/requests.
有任何想法吗?谢谢!
jni*_*las 10
这:https://stackoverflow.com/a/11348065/1504796
是正确的答案.
click_on不采用CSS选择器,而是采用链接的文本或id.你想要的click_on("btn_login").没有哈希标志或任何东西.
小智 4
尝试将“gem 'launchy'”添加到 Gemfile 中,并将“save_and_open_page”行放在步骤文件中失败行之前。
参考:http://techiferous.com/2010/04/using-capybara-in-rails-3/
| 归档时间: |
|
| 查看次数: |
35488 次 |
| 最近记录: |