我正在尝试使用以下代码访问 Braintree 的托管字段:
within_frame('braintree-hosted-field-number') do
fill_in 'Credit card number', with: '4111-1111-1111-1111'
end
inspect
框架的名称是正确的,我从Chrome的面板中复制了它。但我得到这个错误
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"
咖啡脚本
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
捐赠模块规格.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