【问题标题】:Capybara::ElementNotFound, but it is thereCapybara::ElementNotFound,但它就在那里
【发布时间】:2013-08-13 09:19:18
【问题描述】:

我收到以下错误:

Capybara::ElementNotFound: Unable to find field "username"
./spec/controllers/sessions_controller_spec.rb:10:in `block (3 levels) in <top (required)>'

规格:

require 'spec_helper'

describe SessionsController do
  before :each do
    @user = FactoryGirl.create(:user)
  end
  context 'creating a new session' do
    it 'can set the current_user variable to the logged user' do
      visit '/login'
      fill_in 'username', with: 'gabrielhilal' #I have tried `Username` as well
      fill_in 'password', with: 'secret'
      click_button 'Login'
      current_user.should == @user
      current_user.username.should == 'gabrielhilal'
    end
  end
  context 'destroying existing session' do
    xit 'can destroy the current_user' do
    end
  end
end

但我的表单中有 username 字段:

<form accept-charset="UTF-8" action="/sessions" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" / <input name="authenticity_token" type="hidden" value="x7ORLDIvq1BXr8SOkd/Zla9Pl5R5tBXAtyflCpTGCtY=" /></div>
  <div class="field">
    <label for="username">Username</label>
    <input id="username" name="username" type="text" />
  </div>
  <div class="field">
    <label for="password">Password</label>
    <input id="password" name="password" type="password" />
  </div>
  <div class="actions">
    <input name="commit" type="submit" value="Login" />
  </div>
</form>

我对@9​​87654326@ 进行了类似的测试,它通过了,这证实了username 字段存在:When to switch from cucumber to rspec in the BDD cycle for a login procedure

有什么想法吗?

编辑 - 我添加了save_and_open_page,它给了我一个空白页。 puts "#{page.html.inspect}" 也返回空。

""

Capybara::ElementNotFound: Unable to find field "username"
./spec/controllers/sessions_controller_spec.rb:12:in `block (3 levels) in <top (required)>'

【问题讨论】:

  • 尝试使用 save_and_open_page 和启动 gem
  • 谢谢@hypee...我添加了save_and_open_page,但由于某种原因我的页面是空白的。

标签: ruby-on-rails rspec capybara bdd ruby-on-rails-4


【解决方案1】:

您可以使用以下终端对其进行调试:

it {puts page.body}

并查看是否有“用户名”输入。 但我认为第一步就有问题visit '/login'

【讨论】:

  • 我发现这非常有用。它可以准确地告诉您哪个元素是哪个元素,并使事情变得非常容易找到。感谢您的分享(即使是几年前的事了!)
【解决方案2】:

我知道您已经回答了您自己的问题(有点),但我觉得仍然有必要让您知道您提供的示例是控制器和请求规范的混合...

逐字引用RSpec - Controller SpecsNote: To encourage more isolated testing, views are not rendered by default in controller specs.

主要区别(RSpec 解释它是哪种规范的方式)是行:describe SessionsController do

使其与 RSpec 和测试最佳实践保持一致:

  1. 将规范移至spec/requests
  2. 将描述更改为describe "SessionsController" do
  3. 删除render_views

您的规范应该可以正常运行...

查看RSpec - Request Specs 了解我的意思。我还会查看betterspecs.org,了解可以改进测试的方法。

【讨论】:

    【解决方案3】:

    我意识到我必须明确告诉我的控制器渲染视图。我在rspec readme找到了,所以我解决了添加render_views的问题。

    describe SessionsController do
      render_views #this line solved the problem
      before :each do
        user = FactoryGirl.create(:user)
      end
    ...
    

    【讨论】:

      【解决方案4】:

      就我而言,只需删除 features/support/env.rb 中的 block_unknow_urls

      Capybara::Webkit.configure do |config|
        config.allow_unknown_urls
      end
      

      【讨论】:

      • 我看不出这与问题有什么关系,请尝试详细说明为什么会这样
      • 嗨@Tobyzhen,欢迎来到SO!请添加更多详细信息并查看stackoverflow.com/help/how-to-answer
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多