ruby-on-rails - 如何为 Rails 上的 Stripe 结账编写集成测试?

标签 ruby-on-rails ruby-on-rails-3 testing capybara stripe-payments

我在尝试为 Stripe 的 checkout.js 编写集成测试时碰壁了 [ https://checkout.stripe.com/checkout.js ] 用于我的 Rails 3.2 应用程序。

在手动测试(使用 Stripe 的测试 key )时,Stripe 结帐对我来说工作正常,但我无法让 Capybara 检测并填写 Stripe 结帐 iframe 模式中的电子邮件字段。

我将 poltergeist 用于 headless javascript,尽管我也用 capybara-webkit 甚至 selenium 测试了它,但遇到了同样的问题。

我要测试的是完整的订阅注册流程,以表明新用户可以在 Stripe 中输入他们的付款详细信息后创建订阅者帐户 - 但我无法通过 Stripe 结帐弹出窗口。

这是我的之前..做:

describe "show Stripe checkout", :js => true do
  before do
    visit pricing_path
    click_button 'plan-illuminated'
    stripe_iframe = all('iframe[name=stripe_checkout_app]').last
    Capybara.within_frame stripe_iframe do        
      fill_in "email", :with => "test-user@example.com"
      fill_in "billing-name", :with => "Mr Name"
      fill_in "billing-street", :with => "test Street"
      fill_in "billing-zip", :with => 10000
      fill_in "billing-city", :with => "Berlin"
      click_button "Payment Info"
    end
  end
  it { should have_selector('button', text: "Subscribe") }
end

哪些错误:

Failure/Error: Capybara.within_frame stripe_iframe do
 Capybara::Poltergeist::TimeoutError:
   Timed out waiting for response to {"name":"push_frame","args":[null]}

如果我换掉尝试选择正确的 iframe(此处建议:Capybara trouble filling in JS modal),如下所示:

# stripe_iframe = all('iframe[name=stripe_checkout_app]').last
# Capybara.within_frame stripe_iframe do  
Capybara.within_frame 'stripe_checkout_app' do

我仍然得到类似的:

Capybara::Poltergeist::TimeoutError:
   Timed out waiting for response to {"name":"push_frame","args":["stripe_checkout_app"]}

看来无论我使用哪个 javascript 测试 gem,rspec/capybara 都找不到 Stripe checkout iframe。当我检查 Selenium 时,我看到按下了 Choose this Plan 按钮并弹出了 Checkout,但是规范在查找要填写的电子邮件字段时超时。

有什么想法吗?

我已经尝试过:

  • 选择或查找电子邮件字段的各种方式。
  • 更新我所有的 gem。
  • 在此之前使用 StripeMock(不是应该涉及,对吧?)。
  • 对 Stripe 自己的站点运行相同的测试,结果出现相同的错误:

测试:

  visit "https://stripe.com/docs/checkout"
  click_button 'Pay with Card'
  stripe_iframe = all('iframe[name=stripe_checkout_app]').last
  Capybara.within_frame stripe_iframe do
    fill_in 'Email', with: 'test@example.com'
    sleep 3
  end

根据我使用哪种方法选择 iframe,我会收到相同的错误。仅使用 Capybara.within_frame 'stripe_checkout_app' 做:

 Failure/Error: Capybara.within_frame stripe_iframe do
 Capybara::Poltergeist::TimeoutError:
   Timed out waiting for response to {"name":"push_frame","args":[null]}

或将 Selenium 与 stripe_iframe = all('iframe[name=stripe_checkout_app]').last 一起使用:

 Failure/Error: Unable to find matching line from backtrace
 SystemStackError:
   stack level too deep

甚至只是:

 Failure/Error: fill_in 'Email', with: 'test@example.com'
 Capybara::ElementNotFound:
   cannot fill in, no text field, text area or password field with id, name, or label 'Email' found

...取决于我使用的测试 javascript gem。

非常感谢任何帮助或智慧!

最佳答案

到目前为止,我在这里找不到任何适合我的解决方案,然后阅读这篇文章:https://gist.github.com/nruth/b2500074749e9f56e0b7我意识到关键是在测试中添加延迟,以便为 Stripe 提供足够的时间来 1) 加载结帐窗口和 2) 处理 token 。

出于这个原因,我唯一可以开始工作的代码是这个(随意玩计时):

Selenium

describe "test stripe" do, js: true, driver: :selenium do

  before do
    ... # fill in order form or whatever
    click_button "checkout_with_stripe"
    sleep(2) # allows stripe_checkout_app frame to load
    stripe_iframe = all('iframe[name=stripe_checkout_app]').last
    Capybara.within_frame stripe_iframe do
      page.execute_script(%Q{ $('input#email').val('jamesdd9302@yahoo.com'); })
        page.execute_script(%Q{ $('input#card_number').val('4242424242424242'); })
        page.execute_script(%Q{ $('input#cc-exp').val('08/44'); })
        page.execute_script(%Q{ $('input#cc-csc').val('999'); })
        page.execute_script(%Q{ $('#submitButton').click(); })
      sleep(3) # allows stripe_checkout_app to submit
    end
  end

  it "should successfully process the request" do
     ... # test should pass
  end 
end

对于 Capybara-webkitsleep 技巧不起作用,很遗憾,@Ryan 的解决方案也不起作用,我已经厌倦了尝试解决这个问题,所以我只是停止了,但希望其他人会得到它,因为出于速度原因我宁愿使用 webkit! (我使用的是 capybara-webkit 1.3.0)

如果有帮助,这里是我的相关版本:

selenium-webdriver 2.35.1
rspec-rails 2.14.2
capybara 2.1.0
stripe 1.16.0 da216fd
rails 4.1.1
ruby 2.1.2

关于ruby-on-rails - 如何为 Rails 上的 Stripe 结账编写集成测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24081305/

相关文章:

ruby-on-rails - 在 Rails 中,是否会在每个新请求时重置任意类的类属性?

ruby-on-rails - 有什么方法可以暂时禁用 Heroku 托管的 Ruby on Rails 应用程序以停止每月付款?

ruby-on-rails - 诺科切替换

testing - 代码覆盖率/分支覆盖率推荐值

ruby-on-rails - Dockerized 应用程序适用于带有 PSQL 的 Rails,但数据库容器不工作

ruby-on-rails - 子类的循环依赖

testing - 使用 JUnitCore 运行 GWT 2.4 测试

unit-testing - CakePHP 核心测试不适用于 IIS

sql - rails order through count 在其他表上

ruby-on-rails - PostgreSQL 用户的身份验证错误?