问题
Background: I'm using Capybara with Rspec to test a Rails 3 app. Driver used: Selenium
Problem: I can't find the "Sign in" button in order to click on from within my test.
HTML code:
<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>
Failing test
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
My test file is inside spec/requests
.
Any ideas? Thanks!
回答1:
Please try this:
page.find("#btn_login").click
回答2:
This: https://stackoverflow.com/a/11348065/1504796
Is the right answer.
click_on
does not take a CSS selector, but instead the text or id of a link. You want click_on("btn_login")
. No hash sign or anything.
回答3:
try
find('#id',:visible => true).click
回答4:
Try adding "gem 'launchy'" to your Gemfile and put "save_and_open_page" line before failed line in step file.
REFERENCE: http://techiferous.com/2010/04/using-capybara-in-rails-3/
回答5:
It looks like you're trying to click on a submit button inside some modal css. You'll need to invoke whatever displays that modal element first.
回答6:
I don't think click_on will take a locator like that--I think it may want just an id, name or value. As an experiment, try replacing click_on("input#btn_login") with:
page.find('#btn_login').click
回答7:
- click_button("Sign in")
- find(:xpath, "//*[@id='btn_login']").click (or .trigger('click'))
If else fails, go through the console and see if you can check the presence of the button: document.getElementById("btn_login")
回答8:
If you know the link text, you can use page.find_link(text).click
. (source)
回答9:
Try adding a save_and_open_page before the line in question. This should allow you to see the page and any errors that may prevent the clicking action.
回答10:
I use capybara with chrome
Capybara.register_driver :chrome do |app| Capybara::Selenium::Driver.new(app,
:browser => :chrome)
end
Capybara.javascript_driver = :chrome
and install chrome driver:
brew install chromedriver
http://collectiveidea.com/blog/archives/2011/09/27/use-chrome-with-cucumber-capybara/
来源:https://stackoverflow.com/questions/11335303/cant-find-element-to-click-on-using-capybara-rails3