21

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!

4

10 回答 10

35

请试试这个:

page.find("#btn_login").click
于 2012-12-10T14:10:52.600 回答
10

这个:https ://stackoverflow.com/a/11348065/1504796

是正确答案。

click_on不采用 CSS 选择器,而是采用链接的文本或 id。你想要click_on("btn_login")。没有哈希符号或任何东西。

于 2012-07-05T17:49:38.450 回答
7

尝试

find('#id',:visible => true).click
于 2013-06-10T09:30:06.677 回答
4

看起来您正在尝试单击某个模态 css 中的提交按钮。您需要首先调用显示该模态元素的任何内容。

于 2012-07-05T15:21:27.863 回答
4

尝试将“gem 'launchy'”添加到您的 Gemfile 并在步骤文件中的失败行之前放置“save_and_open_page”行。

参考:http ://techiferous.com/2010/04/using-capybara-in-rails-3/

于 2012-07-05T14:40:14.153 回答
3

我认为 click_on 不会采用这样的定位器——我认为它可能只需要一个 id、名称或值。作为实验,尝试将 click_on("input#btn_login") 替换为:

page.find('#btn_login').click

于 2012-07-05T15:52:34.093 回答
3
  • click_button("登录")
  • find(:xpath, "//*[@id='btn_login']").click (或 .trigger('click'))

如果其他失败,请通过控制台查看是否可以检查按钮的存在: document.getElementById("btn_login")

于 2014-03-18T15:52:33.317 回答
2

如果您知道链接文本,则可以使用page.find_link(text).click. (来源

于 2014-09-08T15:36:38.130 回答
1

我用镀铬的水豚

Capybara.register_driver :chrome do |app|   Capybara::Selenium::Driver.new(app, 
                                                           :browser => :chrome) 
end

Capybara.javascript_driver = :chrome

并安装 chrome 驱动程序:

brew install chromedriver

http://collectiveidea.com/blog/archives/2011/09/27/use-chrome-with-cucumber-capybara/

于 2015-01-16T02:04:31.043 回答
1

尝试在相关行之前添加 save_and_open_page。这应该允许您查看页面和任何可能阻止单击操作的错误。

于 2012-07-05T14:42:04.583 回答