Rails + Capybara + Braintree - 如何测试BT的托管领域?

时间:2016-06-13 23:25:23

标签: ruby-on-rails ruby-on-rails-4 iframe capybara braintree

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

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

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_frame Capybara::NotSupportedByDriverError: Capybara::Driver::Base#within_frame说{{1}}

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

within_frame应具有以下格式(应从两者中删除#-sign for 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

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