Rails + Capybara + Braintree—如何对 BT 的 Hosted Fields 进行功能测试?

Rails + Capybara + Braintree—how to feature test BT's Hosted Fields?

TL;DR—如何访问 Braintree 的托管字段的 iframe 中的字段?

我想测试通过 Braintree 支付捐款的 UX 流程。到目前为止,这是我的代码:

require "rails_helper"

RSpec.feature "Donation Module", type: :feature do

  scenario "Public visitor creates a new donation" do
    #load page
    website = create(:website)
    Capybara.current_session.driver.header 'Referer', website.website
    visit "/donate?t=#{website.public_token}&frame=1"
    #verify page loaded
    expect(page).not_to have_content("Are you sure you're installing this on the correct website?")

    #fill page 1
    find("input[value='20']").click

    #go to page 2
    find("#credit-details").click

    #verify page 2 content is loaded
    expect(find(".total-cost-text")).to be_visible

    #fill page 2
    fill_in 'First Name', with: 'Leeroy'
    fill_in 'Last Name', with: 'Jenkins'
    fill_in 'Email for receipt', with: 'new_donor@email.com'

    within_frame('#braintree-hosted-field-number') do
      fill_in '#credit-card-number', with: '4111-1111-1111-1111'
    end

    within_frame('#braintree-hosted-field-expirationDate') do
      fill_in '#expiration', with: '09/19'
    end

    within_frame('#braintree-hosted-field-cvv') do
      fill_in '#cvv', with: '123'
    end
    find('Make payment').click


    # expect to make a new user, new donation, new receipt, email receipt
  end
end

目前,它在第一个 within_frameCapybara::NotSupportedByDriverError: Capybara::Driver::Base#within_frame

如何访问 BT 的 iframe 中的字段?

您似乎在使用 rack_test 驱动程序?它不支持 JS 或框架,因此 braintree 无法使用它。您需要切换到真正的浏览器驱动程序之一,例如 selenium、capybara-webkit 或 poltergeist。

好吧,我在这里写的并不是这个问题的答案,而是对问题的更正,因为我处于类似的情况并且面临类似的错误,例如 Selenium::WebDriver::Error::NoSuchFrameError: Unable to locate frame: #braintree-hosted-field-numberTest::Unit::Capybara::ElementNotFound: Unable to find field "#credit-card-number".

within_frame 应具有以下格式(应从两者中删除 ID 的 # 符号):

within_frame('braintree-hosted-field-number') do
  fill_in 'credit-card-number', :with => number
end

并且为了在 Test::Unit 中使用 selenium 驱动程序,我使用了以下帮助程序:

def js
  Capybara.current_driver = Capybara.javascript_driver
  yield
  Capybara.current_driver = Capybara.use_default_driver
end

然后将我的测试包在其中:

class SomeTest < ActionDispatch::IntegrationTest
  test "should ..." do
    js do
      within_frame('braintree-hosted-field-number') do
        fill_in 'credit-card-number', :with => number
      end
      # ...
    end
  end

希望有人会发现它在使用单元测试时有用。