Cypress cannot directly interact with cross-origin iframes by default. To test the Paystack popup, either: (1) use the cypress-iframe plugin (cy.frameLoaded() + cy.iframe()) to interact with iframe content, or (2) stub the Paystack API responses in your backend and mock the checkout callback. The second approach is more reliable for CI pipelines.
// cypress/e2e/checkout.cy.js
// Requires: npm install -D cypress-iframe
// In cypress/support/commands.js: import 'cypress-iframe'
// In cypress.config.js: chromeWebSecurity: false
describe('Paystack checkout flow', () => {
it('completes payment with test card', () => {
cy.visit('/checkout?product=1');
// Click pay button
cy.get('[data-testid="pay-button"]').click();
// Wait for Paystack iframe to load
cy.frameLoaded('iframe[name="checkout"]');
// Interact with inputs inside the iframe
cy.iframe('iframe[name="checkout"]')
.find('input[placeholder="Card Number"]')
.type('4084084084084081');
cy.iframe('iframe[name="checkout"]')
.find('input[placeholder="MM/YY"]')
.type('05/30');
cy.iframe('iframe[name="checkout"]')
.find('input[placeholder="CVV"]')
.type('408');
cy.iframe('iframe[name="checkout"]')
.contains('button', 'Pay')
.click();
// Assert redirect to success page
cy.url({ timeout: 30000 }).should('include', '/payment/success');
cy.contains('Payment Successful').should('be.visible');
});
});
// Alternative: stub approach (more reliable in CI)
describe('Paystack checkout — stubbed approach', () => {
it('handles successful payment callback', () => {
cy.intercept('POST', '/api/checkout', {
statusCode: 200,
body: { authorization_url: 'https://checkout.paystack.com/test', reference: 'ref_001' },
}).as('initCheckout');
cy.visit('/checkout?product=1');
cy.get('[data-testid="pay-button"]').click();
cy.wait('@initCheckout');
// Simulate the Paystack callback being called (as if payment succeeded)
cy.window().then((win) => {
win.PaystackPop.setup({ callback: (response) => win.handlePaystackCallback(response) });
win.handlePaystackCallback({ reference: 'ref_001', status: 'success' });
});
cy.url({ timeout: 10000 }).should('include', '/payment/success');
});
});
This guide is part of the Paystack testing guide.
KES 2,999
The definitive guide to building payment systems with Paystack — from your first transaction to production-grade payment infrastructure across Africa. 9 modules, 47 lessons. Free preview available.
Start Paystack Course