【问题标题】:Capybara Webkit + Braintree—"Unable to locate frame"Capybara Webkit + Braintree—“无法定位框架”
【发布时间】:2016-10-15 20:26:42
【问题描述】:

我正在尝试使用以下代码访问 Braintree 的托管字段:

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

框架的名称是正确的,我是从 Chrome 上的inspect 面板复制的。 但是我得到了这个错误

Capybara::Webkit::InvalidResponseError:
   Unable to locate frame.

我做错了什么?或者,更重要的是,我该如何调试/看看发生了什么?


编辑

根据要求,这是我的代码。请注意,如果我通过手动填写字段并提交表单,整个系统都可以正常工作。

苗条

= f.fields_for :user do |u|
    .control-group.col-24.payment-details
      label.half
        .field-title First Name
        = u.text_field :first_name
      label.half
        .field-title Last Name
        = u.text_field :last_name
      label.full
        .field-title Email for receipt
        = u.email_field :email
      label.full
        .field-title Credit card number
        .bt-field#credit-card
      label.half
        .field-title Expiry date
        .bt-field#exp-card
      label.half
        .field-title CVC
        .bt-field#cvv-card

script src="https://js.braintreegateway.com/js/braintree-2.24.0.min.js"

CoffeeScript

braintree.setup(clientToken, "custom", {
          id: "donate-form",
          onPaymentMethodReceived: (obj)->
            formdata = donationForm.getFormData()
            formdata.set "payment_method_nonce", obj.nonce
            donationForm.send(formdata)
          onError: (obj)->
            errors = []
            switch obj.type
              when "VALIDATION"
                errors.push "Invalid payment details. Please correct your payment details."
              else
                errors.push "Something went wrong. Please check your payment details and try again."
            donationForm.printErrors("donation-errors", errors)
          hostedFields: {
            styles: {
            "input": {
              "font-size": "14px",
              "font-weight": "bold",
              "font-family": "Lato, Helvetica, Arial, Geneva, sans-serif",
              "color": "#464646;",
              "transition": colorTransition,
              "-webkit-transition": colorTransition
              },
            ".invalid": { color: "#DD0000"} },
            number: {selector: "#credit-card"},
            cvv: {selector: "#cvv-card"},
            expirationDate: {selector: "#exp-card", "placeholder": "dd/mm"}
          }
        })

编辑 2

donation_module_spec.rb

require "rails_helper"

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

  scenario "Wrong public token" do
    visit "/donate?t=BAD_URL&frame=1"
    expect(page).to have_content("Are you sure you're installing this on the correct website?")
  end

  scenario "Public visitor creates a new donation", driver: :webkit do

    #load page
    website = create(:website)
    page.driver.header 'Referer', website.website
    visit "/donate?t=#{website.public_token}&frame=1"

    #verify page 1 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'] ~ div").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'

    sleep 5
    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, emailed receipt
  end
end

rails_helper.rb 修改为仅显示相关部分:

require 'capybara/rails'
require 'capybara/rspec'

Capybara::Webkit.configure do |config|
  config.allow_url("js.braintreegateway.com")
  config.allow_url("fonts.googleapis.com")
  config.allow_unknown_urls
  config.debug = true
end

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = false

【问题讨论】:

  • 能否发布您用于设置托管字段的 HTML 和 JavaScript?
  • 完成,但请注意,当我手动通过时系统可以正常工作,只有在我尝试使用 capybara 时才会中断。
  • 您在fill_in 中的信用卡号字段中似乎使用了错误的名称。你试过“信用卡号”或“信用卡号”吗?
  • 我不确定它是否正确,但我什至无法测试它,因为代码在 within_frame 处中断
  • 我可以正常工作,我的猜测是 javascript 没有加载。您能否包含一些配置,例如规范助手中可能相关的任何内容?还要确定的是,您有 :type => feature:js => true 对吗?

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


【解决方案1】:

全面披露:我在 Braintree 工作。如果您还有任何问题,请随时联系support

我不知道为什么会发生这种情况,复制你的设置我可以让它工作。我确实有一些额外的想法可能会有所帮助。

1) 查看调试输出(看起来您已经使用config.debug = true 启用了此功能)并确保没有错误。这是我输出的相关部分 -

"Visit(http:/localhost:3000/hostedfields_payment)" started page load 
Started request to "http://localhost:3000/hostedfields_payment" 
Finished "Visit(http:/localhost:3000/hostedfields_payment)" with response "Success()" 
Started request to "https://js.braintreegateway.com/js/braintree-2.17.5.js" 
Started request to "https://api.sandbox.braintreegateway.com/merchants/-/client_api/v1/configuration?-
Received 200 from "https://api.sandbox.braintreegateway.com/merchants/-/client_api/v1/configuration?- 
Received "FrameFocus(braintree-hosted-field-number)" 
Load started 
Started request to "https://assets.braintreegateway.com/hosted-fields/2.17.5/hosted-fields-frame.html#91586a91-c55f-4e51-80e6-7cd750f9c750" 
Started request to "https://assets.braintreegateway.com/hosted-fields/2.17.5/hosted-fields-frame.html#91586a91-c55f-4e51-80e6-7cd750f9c750" 
Started request to "https://assets.braintreegateway.com/hosted-fields/2.17.5/hosted-fields-frame.html#91586a91-c55f-4e51-80e6-7cd750f9c750" 
"TimeoutCommand" waiting for load to finish  
Started request to "https://assets.braintreegateway.com/hosted-fields/2.17.5/braintree-hosted-fields-internal.min.js" 
Received 200 from "https://assets.braintreegateway.com/hosted-fields/2.17.5/hosted-fields-frame.html#91586a91-c55f-4e51-80e6-7cd750f9c750" 
Received 200 from "https://assets.braintreegateway.com/hosted-fields/2.17.5/hosted-fields-frame.html#91586a91-c55f-4e51-80e6-7cd750f9c750" 
Received 200 from "https://assets.braintreegateway.com/hosted-fields/2.17.5/hosted-fields-frame.html#91586a91-c55f-4e51-80e6-7cd750f9c750" 
Received 200 from "https://assets.braintreegateway.com/hosted-fields/2.17.5/braintree-hosted-fields-internal.min.js" 
Page finished with true 
Load finished 
Started "FrameFocus(braintree-hosted-field-number)"
Finished "FrameFocus(braintree-hosted-field-number)" with response "Success()" 
Wrote response true "" 
Received "FindCss(input#credit-card-number)" 
Started "FindCss(input#credit-card-number)" 
Finished "FindCss(input#credit-card-number)" with response "Success()" 

2)您可以检查的一种粗糙方法是在您的sleep 5 末尾,将正文打印到文件并打开它。您可以检查 iframe 是否存在。

File.open("looking_for_iframes.html", "w") do |file|
  file.print(page.body)
end

否则,请随时联系Braintree Support

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 2012-03-02
    相关资源
    最近更新 更多