【问题标题】:Capybara not finding submit buttonCapybara 找不到提交按钮
【发布时间】:2014-05-25 12:17:37
【问题描述】:

rspec-测试期间,我试图通过填写表格并单击“重置密码”链接来重置我的密码。

Capybara 给我以下错误:

1) ResetPasswords emails user a reset link when resetting password
   Failure/Error: click_link 'Reset Password'
   Capybara::ElementNotFound:
     Unable to find link "Reset Password"

显然它没有找到我希望它点击的链接。但是当我尝试使用puts page.body 打印它正在查看的页面时,这个元素存在:

<div><input name="commit" type="submit" value="Reset Password" /></div>

我尝试使用clickclick_buttonclick_link,但没有使用雪茄。

为什么找不到元素?

更新 1:click_button 输出

1) ResetPasswords emails user a reset link when resetting password
   Failure/Error: click_button 'Reset Password'
   ActionView::Template::Error:
     Missing host to link to! Please provide the :host parameter, set
     default_url_options[:host], or set :only_path to true

更新 2:测试文件

require 'spec_helper'

include Warden::Test::Helpers
Warden.test_mode!

describe "ResetPasswords" do
  it "emails user a reset link when resetting password" do
    visit login_path
    click_link 'password'

    fill_in 'Email', :with => @user.email

    puts page.body
    click_link 'Reset Password'

    page.should have_content "You will receive an email"
  end

  before(:each) do
    @user = create(:user)
    #login_as @user, :scope => :user
  end
end

【问题讨论】:

  • click_button 'Reset Password' 方法给出的输出是什么?
  • 请查看更新后的帖子。
  • 如您所见,它找到了电子邮件字段并正确填写,我已经确认了这一点。但是,它不知道如何提交表单..

标签: ruby-on-rails rspec capybara


【解决方案1】:

将这个 sn-p 放入 config/environments/test.rb 解决了它:

config.action_mailer.default_url_options = {:host => "localhost:3000"}

【讨论】:

    【解决方案2】:

    尝试直接进入重置密码页面

    describe "ResetPasswords" do
      it "emails user a reset link when resetting password" do
        visit reset_password_path
    
        fill_in 'Email', :with => @user.email
    
        click_link 'Reset Password'
    
        page.should have_content "You will receive an email"
      end
    
      before(:each) do
        @user = create(:user)
        #login_as @user, :scope => :user
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 2012-02-04
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      相关资源
      最近更新 更多