Randomizing Stripe test credit cards in Cypress

According to Stripe, all of these are valid test credit cards (not usable in real-world scenarios). These are from http://stripe.com/docs/testing

Create ccUtil.js and paste this into that:

const getTestCreditCard = (TestCreditCard = ['4000 0566 5566 5556',
'4242 4242 4242 4242', '5555 5555 5555 4444',
'2223 0031 2200 3222', '5200 8282 8282 8210',
'5105 1051 0510 5100', '6011 1111 1111 1117',
'3056 9300 0902 0004', '3622 720627 1667',
'3566 0020 2036 0505', '6200 0000 0000 0005',
'3782 822463 10005', '3714 496353 98431'])
{const cards = TestCreditCard[Math.floor(Math.random() * TestCreditCard.length)]
return cards}
export { getTestCreditCard }

Add to the top of the script where this is going to be used -

import { getTestCreditCard } from ‘../../utils/ccUtil’

Usage:

cy.getWithinIframe('[name="cardnumber"]').type(getTestCreditCard()) 
cy.get('[name="ccInformation.nameOnCard"]') 
   .focus()
    .type('[REDACTED ] ') //FName
    .type('[REDACTED]') //LName
cy.get('[class="__PrivateStripeElement"]')
    .eq(1)
    .within(()  {
cy.getWithinIframe('[name="exp-date"]').type('1232') 
     })
cy.get('[class="__PrivateStripeElement"]')
    .eq(2)
    .within(()  {
cy.getWithinIframe('[name="cvc"]').type('9871') 
     })
cy.get('[name="ccInformation.zipCode"]').type('78701') 

What this does:
Randomly picks a test credit card using ccUtil.js and types this into the appropriate iFrame.


Note that this also uses the code from this post.


Previous
Previous

Copying and Pasting text in Cypress

Next
Next

Wildcards on Classes in Cypress