Duncan McClean

Testing with Stripe Elements in Laravel Dusk

Published 9 months ago

Today I've been playing around with using Laravel Dusk to test a payment form on a small website.

The payment form is a key part of the owner's business so it's crucial it works all the time, whenever the customer makes a purchase.

The site uses Stripe Elements to let the customer enter their payment information securely.

Elements creates its own iframe element which makes it a little tricky to target and test. However, there's a handy method in Dusk which allows you to type inside of an iframe.

1$this->browse(function ($browser) {
2 $browser->type('@input', 'whatever')
3 ->withinFrame('.__PrivateStripeElement iframe', function ($browser) {
4 $browser
5 ->type('[placeholder="Card number"]', '4242424242424242')
6 ->type('[placeholder="MM / YY"]', '0923')
7 ->type('[placeholder="CVC"]', '123');
8 });
9});

Anything you need to type inside of Stripe's iframe you can do inside the withinFrame method and target the relevant inputs via their placeholders.

Hopefully someone finds that helpful! 🙂

← Back home