5

I´m learning Ruby on Rails and i´m working on an application that use stripe to create premium accounts. Also, i´m using Rspec and Capybara to do the integration tests.

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

I run the test and i get an error message that says:

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

Anyone knows, what can be the problem? Thanks.

3 Answers3

2

Depending on how you're integrating Stripe, it might be rendering the form inside an iframe. If that's the case, you'll need to use Capybara.within_frame to target the actions to the correct 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
Tim Moore
  • 8,958
  • 2
  • 23
  • 34
  • I wonder if the form has changed since this answer. It looks promising, but the form now uses placeholder text, and capybara can't find the field by the `name` attribute. I still haven't figured out what else to try. – nickcoxdotme Mar 10 '14 at 15:27
  • 2
    Placeholder text should be supported by Capybara https://github.com/jnicklas/capybara/issues/854 – Tim Moore Mar 11 '14 at 09:21
  • Indeed it does seem to, but my test still fails whether I use placeholder text or input name attribute. – nickcoxdotme Mar 11 '14 at 20:02
  • OK then there must be something else wrong. Can you open a new question with the details of your code? – Tim Moore Mar 12 '14 at 21:31
  • Sorry, I just saw your IRC message and found your question http://stackoverflow.com/questions/22335808/capybara-trouble-filling-in-js-modal – Tim Moore Mar 12 '14 at 21:32
0

Try to use element id instead of field label.

fill_in 'card_number', :with => '4242424242424242'
fill_in 'cvc', :with => '123'

(Being card_number and cvc the ids of input fields.)

= f.text_field :card_number, :class => 'span12', :id => :card_number
= f.text_field :cvc, :class => 'span12', :id => :cvc

I had the same problem and I used this solution, not sure if it's good practice, but works for me.

adbeel
  • 384
  • 1
  • 9
  • Thanks for the answer! Sorry i´m new in html and i can´t find the elements id. Which element you use? – user3105542 Dec 16 '13 at 02:52
  • In my view file i have credit card field like this -> = f.text_field :card_number, :class => 'span12', :id => :card_number (Here specifying the id) – adbeel Dec 16 '13 at 03:09
0

for capybara with stripe testing:

Capybara.within_frame 'stripe_checkout_app' do
  fill_in "Card number", :with => "4242424242424242"
  fill_in 'CVC', :with => '123'
  fill_in 'Expiry', :with => '11/22'

  click_button 'Pay $5.00'
end

edit the exipry date to a month and year of future always.

vidur punj
  • 5,019
  • 4
  • 46
  • 65