ruby-on-rails-3 – Capybara无法找到Stripe的表单字段?

我正在学习
Ruby on Rails,我正在开发一个使用stripe来创建高级帐户的应用程序.另外,我正在使用Rspec和Capybara进行集成测试.

require 'spec_helper'

feature "user upgrade account to a premium plan" do
 scenario "user upgrade account", :js => true do
  user = FactoryGirl.create :user
  visit new_user_session_path
  fill_in 'Email', :with => user.email
  fill_in 'Password', :with => user.password
  click_button 'Sign in'

  visit new_charge_path
  click_button "Pay with Card"

  fill_in 'Email', :with => 'persona@example.com'
  fill_in "Card number", :with => "4242424242424242"
  fill_in 'CVC', :with => '123'
  fill_in 'MM/YY', :with => '11/14'

  click_button 'Pay $5.00'

 end

我运行测试,我收到一条错误消息,上面写着:

 Failure/Error: fill_in "Card number", :with => "4242424242424242"
 Capybara::ElementNotFound:
   Unable to find field "Card number"

谁知道,问题可能是什么?
谢谢.

最佳答案 根据您如何集成Stripe,它可能会在iframe中呈现表单.如果是这种情况,您需要使用
Capybara.within_frame将操作定位到正确的帧:

scenario "user upgrade account", :js => true do
  user = FactoryGirl.create :user
  visit new_user_session_path
  fill_in 'Email', :with => user.email
  fill_in 'Password', :with => user.password
  click_button 'Sign in'

  visit new_charge_path
  click_button "Pay with Card"

  Capybara.within_frame 'stripe_checkout_app' do
    fill_in 'Email', :with => 'persona@example.com'
    fill_in "Card number", :with => "4242424242424242"
    fill_in 'CVC', :with => '123'
    fill_in 'MM/YY', :with => '11/14'

    click_button 'Pay $5.00'
  end
end
点赞