Capybara没有点击链接

时间:2014-04-10 18:48:43

标签: ruby-on-rails testing capybara

我正在尝试运行这些测试:

describe "Selecting Assets Tour" do
    context "when logged in as client" do
        it "should run through all steps" do
            visit tours_path
            click_link("Selecting Assets")
            binding.pry
            expect(page).to have_content 'Selecting an Asset'

        end
    end
end

但它失败了,但是当我插入binding.pry并手动输入时 click_link("Selecting Assets")

然后退出binding.pry然后测试通过,否则..它不会单独点击链接..

我尝试了其他变体,例如:find('#start-assets-tour').clickclick_on但效果相同。真的不确定此时为什么它不会在我正常运行测试时点击链接,任何帮助都将不胜感激

使用rails 4

如果我提供了足够的信息,请告诉我。

编辑:我的观点如下:

<table class="table table-hover table-tour">
<thead>
  <tr>
    <th class="tour-header">Site Tours</th>
  </tr>
</thead>
<tbody>
<% if current_user.client_limited? || current_user.client_full? %>
  <tr>
    <td>
        <%= link_to "Changing Focus", "/", :id => "start-focus-tour" %>
    </td>
  </tr>
<% end %>
  <tr>
    <td>
      <%= link_to "Selecting Assets", "/", :id => "start-assets-tour" %>
    </td>
  </tr>
  <tr>
    <td>
      <%= link_to "Viewing Asset Details", "/", :id => "start-view-asset-tour" %>
    </td>
  </tr>
  <tr>
    <td>
      <%= link_to "Lightbox", "/tours", :id => "start-lightbox-tour" %>
    </td>
  </tr>
  <tr>
    <td>
      <%= link_to "Changing Your Viewing Options", "/", :id => "start-navigation-display-tour" %>
    </td>
  </tr>
</tbody>

id#start-assets-tour会自动启动引导程序游览的开始,即未发生的单击。

2 个答案:

答案 0 :(得分:0)

find('#start-assets-tour').click_link('Selecting Assets')  

如果您在视图中有这样的内容(我使用了HAML):

=link_to 'Selecting Assets', :id => 'start-assets-tour'  

如果您的链接位于其他元素下,请说“div”

%div#other_id
 =link_to 'Selecting Assets', :id => 'start-assets-tour'  

你可以这样写:

within('#other_id') do
 find('#start-assets-tour').click_link('Selecting Assets')
end

如果这没有帮助,也许您应该发布您的观看内容,以便我可以提供更具体的答案。

答案 1 :(得分:0)

我以前见过你遇到过这种问题。在我要求Capybara点击我想要的元素之前,我偶然通过最大化浏览器窗口来修复我的问题。只需像这样添加page.driver.browser.manage.window.maximize

describe "Selecting Assets Tour" do
  context "when logged in as client" do
    it "should run through all steps" do
      visit tours_path
      page.driver.browser.manage.window.maximize
      click_link("Selecting Assets")
      expect(page).to have_content 'Selecting an Asset'
    end
  end
end