Skip to content

Instantly share code, notes, and snippets.

@pszucs
Created February 1, 2017 12:35
Show Gist options
  • Select an option

  • Save pszucs/eaaa3ebd9da98ad84996e0e43668702c to your computer and use it in GitHub Desktop.

Select an option

Save pszucs/eaaa3ebd9da98ad84996e0e43668702c to your computer and use it in GitHub Desktop.

Revisions

  1. pszucs created this gist Feb 1, 2017.
    60 changes: 60 additions & 0 deletions PayByStripeCept.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    <?php

    /**
    * Codeception acceptance test for testing Stripe payments (Checkout)
    *
    * NB: I'm using lowercase variable for the Actor object ($i) instead of $I
    */

    // replace these values
    $initialPage = '';
    $ccNumber = str_split('4242424242424242'); // convert the string to array, see below why
    $expiry = str_split('1120');
    $cvc = str_split('123');
    $ccLength = count($ccNumber);
    $expiryLength = count($expiry);
    $cvcLength = count($cvc);

    // since the input ids are dynamic and they don't have a 'name' attribute,
    // I'm using the placeholder attribute which uniquely identifies the field
    $fieldCard = 'input[placeholder="Card number"]';
    $fieldExpiry = 'input[placeholder="MM / YY"]';
    $fieldCvc = 'input[placeholder="CVC"]';

    $i = new AcceptanceTester($scenario);
    $i->wantTo('pay by using Stripe');

    $i->amOnPage($initialPage);
    $i->maximizeWindow();
    $i->see('Payment summary');

    // wait until Stripe's 'Pay by Card' button appears and is clickable
    $i->waitForElementVisible('.stripe-button-el');
    $i->click('.stripe-button-el');

    // wait a bit for the popup to appear
    $i->wait(2);

    // it's an iframe, let's switch to it
    $i->switchToIFrame('stripe_checkout_app');
    $i->waitForElementVisible('input[type="email"]');

    // enter the email address
    $i->fillField('input[type="email"]', 'test@example.org');

    // can't use the fillField() method here because it messes up the input value
    // iterate through the arrays containing the characters
    for ( $k = 0; $k < $ccLength; $k++ )
    $i->appendField($fieldCard, $ccNumber[$k]);

    for ( $k = 0; $k < $expiryLength; $k++ )
    $i->appendField($fieldExpiry, $expiry[$k]);

    for ( $k = 0; $k < $cvcLength; $k++ )
    $i->appendField($fieldCvc, $cvc[$k]);

    // submit form
    $i->click('button[type="submit"]');

    // success page or message
    $i->waitForText('Thank you for your order', 20);