代码之家  ›  专栏  ›  技术社区  ›  Patrick Kenny

Behat-如何在条带订单结帐表单上测试信用卡号?

  •  1
  • Patrick Kenny  · 技术社区  · 7 年前

    我的测试非常简单,只需填写表格并确保结果符合预期,如下所示:

    And I fill in "Sesame Street" for "Street address"
    And I fill in "FamilyName" for "Last name"
    

    然而 这是我到目前为止所做的尝试。

    And I fill in "4242424242424242" for "The card number"
    And I fill in "4242424242424242" for "edit-payment-information-add-payment-method-payment-details-card-number"
    And I fill in "4242424242424242" for "card-number-element"
    

     Form field with id|name|label|value|placeholder "The card number" not found. (Behat\Mink\Exception\ElementNotFoundException)
    

    注意:此测试站点配置为使用条带测试网关,我正在尝试使用测试信用卡号。没有真实的卡号会接触到这个系统。

    我认为这是因为信用卡付款是使用Stripe库处理的,这使用了一些我不熟悉的特殊HTML结构。以下是HTML:

    <div data-drupal-selector="edit-payment-information-add-payment-method" class="form-group js-form-wrapper form-wrapper" id="edit-payment-information-add-payment-method"><div class="stripe-form form-group js-form-wrapper form-wrapper" data-drupal-selector="edit-payment-information-add-payment-method-payment-details" id="edit-payment-information-add-payment-method-payment-details"><div id="payment-errors"></div><input id="stripe_token" data-drupal-selector="edit-payment-information-add-payment-method-payment-details-stripe-token" type="hidden" name="payment_information[add_payment_method][payment_details][stripe_token]" value="" /><div id="edit-payment-information-add-payment-method-payment-details-card-number" class="form-item js-form-item form-type-item js-form-type-item form-item-payment-information-add-payment-method-payment-details-card-number js-form-item-payment-information-add-payment-method-payment-details-card-number form-group"><label class="js-form-required form-required control-label" for="edit-payment-information-add-payment-method-payment-details-card-number">The card number</label><div id="card-number-element" class="form-text"></div></div>
    

    credit_card_field

    我如何定位信用卡输入并用Behat填写测试卡号?

    And I wait 5 seconds
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   Lajos Hankó    7 年前

    这里有两件事:

      1. 要使用带条纹字段的测试卡,您必须切换到条纹测试模式: Stripe_test_mode
        2
  •  1
  •   Adoni    7 年前

    我在您提供的代码中看到的唯一输入字段是一个隐藏字段。因此,我假设您尝试访问的字段是在 #card-number-element

    在这种情况下,您需要确保浏览器已完成页面加载,然后才能检查是否正常工作。 你可以用水貂的疗程 wait() 如果使用自定义定义,则在上下文文件中使用。或者,在使用selenium进行测试时,您可以尝试使用其他浏览器。

        3
  •  0
  •   Tsounabe    4 年前

    您好,您需要切换到条带iframe:

    /**
     * @When /^I fill stripe credit card informations$/
     */
    public function fillCreditCardInformations(int $card = 0)
    {
         ...
         $this->switchToIFrame('iframe[name^="__privateStripeFrame"]');
         $this->fillField('cardnumber', self::CARDS[$card]);
         ...
    }
    
    /**
     * @Given /^I switch to iframe "([^"]*)"$/
     */
    public function switchToIFrame(string $locator)
    {
        $found = false;
        $selector = '/' === $locator[0] ? 'xpath' : 'css';
        $iframes = $this->getSession()->getPage()->findAll($selector, $locator);
    
        foreach ($iframes as $iframe) {
            try {
                if ($name = $iframe->getAttribute('name')) {
                    $this->getSession()->getDriver()->switchToIFrame($name);
                    $found = true;
                    break;
                }
            } catch (Exception $e) {
                //ignoring
            }
        }
    
        if (!$found) {
            throw new InvalidArgumentException(sprintf('Could not evaluate CSS Selector: "%s"', $locator));
        }
    }