Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testing a new window with laravel dusk

I have this page that, when a particular button is clicked, a new window pops up (eg. an anchor tag with _target = blank). I want to verify that a string of text appears in this new window but, with Laravel Dusk, assertSee appears to be testing the original window and not the newly opened window.

Any ideas as to how I can test the contents of the newly opened window?

like image 710
neubert Avatar asked Aug 13 '17 04:08

neubert


1 Answers

I just ran into this problem today. You can use the browser driver to get all the opened windows, and then switch to the tab that was opened last.

// Get the last opened tab
   $window = collect($browser->driver->getWindowHandles())->last();

// Switch to the tab
   $browser->driver->switchTo()->window($window);

// Check if the path is correct
   $browser->assertPathIs('/path/of/site/in/new/tab');

You might need to add a pause after switching to the new window, since dusk is pretty fast and it may start asserting before the new page is fully loaded.

(I found this answer in a github issue, https://github.com/laravel/dusk/issues/336)

like image 142
Ruben Beeftink Avatar answered Oct 06 '22 03:10

Ruben Beeftink