Background: I'm using Capybara with Rspec to test a Rails 3 app. Driver used: Selenium
背景:我正在使用Capybara和Rspec来测试Rails 3应用程序。使用的驱动程序:Selenium
Problem: I can't find the "Sign in" button in order to click on from within my test.
问题:我无法找到“登录”按钮,以便在我的测试中点击。
HTML code:
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>
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!
有任何想法吗?谢谢!
10 个解决方案
#1
35
Please try this:
请试试这个:
page.find("#btn_login").click
#2
10
This: https://*.com/a/11348065/1504796
这:https://*.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.
click_on不采用CSS选择器,而是采用链接的文本或id。你想要click_on(“btn_login”)。没有哈希标志或任何东西。
#3
5
try
尝试
find('#id',:visible => true).click
#4
4
Try adding "gem 'launchy'" to your Gemfile and put "save_and_open_page" line before failed line in step file.
尝试将“gem'stuny'”添加到您的Gemfile中,并在步骤文件中的失败行之前添加“save_and_open_page”行。
REFERENCE: http://techiferous.com/2010/04/using-capybara-in-rails-3/
参考:http://techiferous.com/2010/04/using-capybara-in-rails-3/
#5
4
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.
看起来你正试图点击某个模态css中的提交按钮。您需要首先调用模态元素的任何显示。
#6
3
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:
我不认为click_on会采用这样的定位器 - 我认为它可能只需要一个id,名称或值。作为实验,尝试使用以下命令替换click_on(“input#btn_login”):
page.find('#btn_login').click
page.find( '#btn_login')。点击
#7
3
- click_button("Sign in")
- click_button(“登录”)
- find(:xpath, "//*[@id='btn_login']").click (or .trigger('click'))
- find(:xpath,“//* [@ id ='btn_login']”)。click(或.trigger('click'))
If else fails, go through the console and see if you can check the presence of the button: document.getElementById("btn_login")
如果else失败,请通过控制台查看是否可以检查按钮的存在:document.getElementById(“btn_login”)
#8
2
If you know the link text, you can use page.find_link(text).click
. (source)
如果您知道链接文本,则可以使用page.find_link(text).click。 (资源)
#9
1
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.
尝试在相关行之前添加save_and_open_page。这应该允许您查看页面以及可能阻止单击操作的任何错误。
#10
1
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:
并安装chrome驱动程序:
brew install chromedriver
http://collectiveidea.com/blog/archives/2011/09/27/use-chrome-with-cucumber-capybara/
http://collectiveidea.com/blog/archives/2011/09/27/use-chrome-with-cucumber-capybara/
#1
35
Please try this:
请试试这个:
page.find("#btn_login").click
#2
10
This: https://*.com/a/11348065/1504796
这:https://*.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.
click_on不采用CSS选择器,而是采用链接的文本或id。你想要click_on(“btn_login”)。没有哈希标志或任何东西。
#3
5
try
尝试
find('#id',:visible => true).click
#4
4
Try adding "gem 'launchy'" to your Gemfile and put "save_and_open_page" line before failed line in step file.
尝试将“gem'stuny'”添加到您的Gemfile中,并在步骤文件中的失败行之前添加“save_and_open_page”行。
REFERENCE: http://techiferous.com/2010/04/using-capybara-in-rails-3/
参考:http://techiferous.com/2010/04/using-capybara-in-rails-3/
#5
4
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.
看起来你正试图点击某个模态css中的提交按钮。您需要首先调用模态元素的任何显示。
#6
3
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:
我不认为click_on会采用这样的定位器 - 我认为它可能只需要一个id,名称或值。作为实验,尝试使用以下命令替换click_on(“input#btn_login”):
page.find('#btn_login').click
page.find( '#btn_login')。点击
#7
3
- click_button("Sign in")
- click_button(“登录”)
- find(:xpath, "//*[@id='btn_login']").click (or .trigger('click'))
- find(:xpath,“//* [@ id ='btn_login']”)。click(或.trigger('click'))
If else fails, go through the console and see if you can check the presence of the button: document.getElementById("btn_login")
如果else失败,请通过控制台查看是否可以检查按钮的存在:document.getElementById(“btn_login”)
#8
2
If you know the link text, you can use page.find_link(text).click
. (source)
如果您知道链接文本,则可以使用page.find_link(text).click。 (资源)
#9
1
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.
尝试在相关行之前添加save_and_open_page。这应该允许您查看页面以及可能阻止单击操作的任何错误。
#10
1
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:
并安装chrome驱动程序:
brew install chromedriver
http://collectiveidea.com/blog/archives/2011/09/27/use-chrome-with-cucumber-capybara/
http://collectiveidea.com/blog/archives/2011/09/27/use-chrome-with-cucumber-capybara/