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) {

            // ...run 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->pause(1000);
                $browser->assertSee('Pay');
                $browser->type('#panInput', '4547781087013329')
                    ->type('#expiryInput', '12/20')
                    ->type('#cvcInput', '749')
                    ->pause(1000)
                    ->click('#px-submit')
                    ->pause(10000)
                    ->waitUntilMissing('iframe');
            });
            $browser->press('Continue');
            $browser->assertSee('Thanks for your payment');
        });

    }
}