如何解决在Mink中测试Stripe.js时,信用卡号显示为乱码
我正在尝试使用Behat / Mink在我的Drupal网站上测试Stripe。
我已配置了Stripe测试付款网关。我的
我的FeatureContext.PHP
看起来像这样:
/**
* @Then I enter my US JCB credit card info
*/
public function iEnterMyUsJcbCreditCardInfo() {
$this->enterCardDetails('3566 0020 2036 0505','11 / 21','777');
}
private function enterCardDetails($card_number,$exp_date,$cvc) {
$this->getSession()->wait(2000);
// Switch to the payment iframe.
$this->getSession()->switchToIFrame(self::STRIPE_CARDNO_IFRAME);
$this->getSession()->wait(1000);
$this->fillField('cardnumber',"$card_number");
$this->getSession()->wait(2000);
我添加了wait()
,因为信用卡号填写不正确。
例如,当我执行步骤And I enter my US JCB credit card info
时,我得到的是3566 0000 3605 5022
,而不是正确的测试卡号(3566 0020 2036 0505)。
卡号不正确;当我重新运行测试三次时,我得到了这些数字:
- 3566 0022 3005 5600
- 3566 0002 0360 5502
- 3566 0006 5500 3220
因此,似乎stripe.js出现了一些问题,干扰了我的信用卡号输入。
信用卡有效期和CVC /安全码输入不存在此问题。
当我消除信用卡号中的空格时,我仍然遇到相同的问题(输入时数字随机乱码)。
即使我将输入卡号前后的等待时间分别设置为5秒,卡号仍然会出现乱码。
如何在不破坏信用卡号的情况下以behat / mink输入信用卡号?
解决方法
我对贝卡或水貂一无所知,但我建议您删除空格。这些看起来都包含相同的数字,只是顺序不同,所以空格可能会引起问题,因为光标可能会稍微移动一点。
,摘要:您必须一次输入一位数字,因为如果您尝试一次全部输入,Stripe.js会增加空格。
以下是我过去两周一直在使用的相关代码:
// Handle randomized iframe numbers by targeting the div above them.
const STRIPE_CARDNO_IFRAME = 'card-number-element';
const STRIPE_EXP_IFRAME = 'expiration-element';
const STRIPE_CVC_IFRAME = 'security-code-element';
/**
* @Then I enter my credit card number :cc_number
*/
public function iEnterMyCreditCardNumber($cc_number) {
$payment_errors_element = $this->getSession()->getPage()->find('css','div#payment-errors');
if ($payment_errors_element->getText()) {
throw new Exception($payment_errors_element->getText());
}
echo "Test credit card number: $cc_number\n";
$this->switchToMyIframe(self::STRIPE_CARDNO_IFRAME);
$this->getSession()->wait(5000);
$credit_card_field = $this->getSession()->getPage()->findField('cardnumber');
$cc_number_nospaces = str_replace(' ','',"$cc_number");
$creditcard_singledigits = str_split("$cc_number_nospaces",1);
foreach ($creditcard_singledigits as $creditcard_singledigit) {
$this->getSession()->wait(2000);
$credit_card_field->sendKeys("$creditcard_singledigit");
}
// Take a screenshot for debugging when the card number is entered incorrectly.
$this->saveScreenshot();
$this->getSession()->switchToIFrame(null);
}
/*
* Helper function to find the iframe.
*/
private function switchToMyIframe($selector) {
$iframe_selector = "div#$selector iframe";
$iframe = $this->getSession()->getPage()->find('css',$iframe_selector);
$iframe_name = $iframe->getAttribute('name');
echo "Switching to iframe $iframe_name\n";
$this->getSession()->switchToIFrame("$iframe_name");
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。