Laravel Dusk and iframes

Testing payments has been a big headache for me lately. Yesterday I found this little nugget that made it possible to test the entire flow from A-Z.

This is how you make Laravel Dusk to click inside an external iframe. The key is to use withinFrame together with a clousure.

class TestDonation extends Controller 
    public function testCreateCardAndPay()

        $this->browse(function (Browser $browser) {

            // some code here    
            // execute code within the iframe. 
            // don't forget a good selector, just iframe will no do it
            $browser->withinFrame('#pxhv-instrument > iframe', function($browser){
                $browser->type('#panInput', '4547781087013329')
                    ->type('#expiryInput', '12/20')
                    ->type('#cvcInput', '749')
            $browser->assertSee('Thanks for your payment');