在 Mink 中测试 Stripe.js 时信用卡号出现乱码

Credit card number is garbled when testing Stripe.js in Mink

我正在尝试使用 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 0020 2036 0505),而是:3566 0000 3605 5022.

错误的卡号不一致;当我重新 运行 测试三次时,我得到了这些数字:

所以 stripe.js 似乎干扰了我的信用卡号输入。

信用卡有效期和CVC/security密码输入没有这个问题。

我把信用卡号码的空格去掉,还是一样的问题(号码输入时运行domly 乱码)

即使我把输入卡号前后的等待时间都设置为5秒,卡号还是乱码

如何在behat/mink中输入信用卡号而不乱码?

我对 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");
  }