← Posts

Testing with Stripe Elements in Laravel Dusk

February 20th, 2021 - 1 min read

Heads up! This post was published a while back, so while some of it might still be good, I can't say for sure it's all accurate. Take it with a pinch of salt!

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 });

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! 🙂