【问题标题】:Capybara::ElementNotFound issue with remote trueCapybara::ElementNotFound 问题与远程 true
【发布时间】:2017-09-04 15:28:44
【问题描述】:

我有一个使用 remote=>true 提交的表单

<%= form_tag fetch_data_path, :remote => true do %> 
    Date: <%= text_field_tag :date %>
    Amount:<%= text_field_tag :amount %>
    From:<%= text_field_tag :from %>
    To:<%= text_field_tag :to %>

 <%= submit_tag "Convert"%> &nbsp;

<% end %>


<span style="font-weight: bold;">Converted Amount:</span> 
<div id="converted_amount"></div>

在提交表单时,转换后的值会显示在带有 id 的 div 中

#converted_amount

这是我的 rspec:

RSpec.feature "User performs exchange", :type => :feature do
  scenario "with valid input" do
    visit exchanges_path
    fill_in 'date', :with => "2017-08-24"
    fill_in 'amount', :with => "100"
    fill_in 'from', :with => "pound"
    fill_in 'to', :with => "dollar"
    click_button('Convert')

    expect(page).to have_text("Converted Amount:")
  end
end

问题是当我运行它时,我得到了

Capybara::ElementNotFound: Unable to find xpath "/html" 但是当我注释掉 click_button 时,它可以工作。

我做错了什么?

【问题讨论】:

  • 您在 Capybara 上使用什么驱动程序?另外,您使用的是什么版本的 Capybara 和具体的驱动程序?
  • 水豚 (2.15),水豚 webkit (1.14.0)
  • 我应该提到我正在使用 Ruby on Rails 3.2

标签: ruby-on-rails rspec capybara


【解决方案1】:

这是我的解决方案:

我将以下宝石添加到我的 Gemfile 中

  gem 'capybara-webkit'
  gem 'selenium-webdriver'

然后将:js =&gt; true 添加到我的规范文件中。它变成了:

RSpec.feature "User performs exchange", :type => :feature do
  scenario "with valid input", :js => true do
    visit exchanges_path
    fill_in 'date', :with => "2017-08-24"
    fill_in 'amount', :with => "100"
    fill_in 'from', :with => "pound"
    fill_in 'to', :with => "dollar"
    click_button('Convert')

    expect(page).to have_text("Converted Amount:")
  end
end

【讨论】:

  • 您还必须指定 Capybara.javascript_driver = &lt;something&gt; ,并且您不需要同时使用 capybara-webkitselenium-webdriver - 您可以使用其中一个。使用RSpec.feature时也不需要指定:type =&gt; :feature
  • 另外,由于您之前在删除click_button 时说过它通过了,这意味着在单击按钮之前页面上已经存在文本“Converted Amount:”,所以这不是一件好事期望表明click_button 已完成。
  • 感谢您提供所有这些信息。我不知道所有这些。我正在使用 execjs,但没有指定“Capybara.javascript_driver”。
  • execjs 与水豚无关。如果您没有指定特定的 javascript_driver,那么它将默认为 :selenium,它将在 Firefox 中使用 selenium-webdriver 进行 JS 测试。你不需要capybara-webkit gem。
  • 这很有帮助。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-11
相关资源
最近更新 更多