Search code examples
laravellaravel-5laravel-dusk

Can Laravel Dusk Assert a JavaScript Dynamic Element?


I have a form created by JavaScript and I can see it in the Chrome developer tool: CTRL + Shift + I, but I can't see it in view-source: Ctrl + U.

I want to test, but Laravel Dusk can't see it. Is it possible that Laravel Dusk asserts (see, press, click, input, etc.) JavaScript dynamic element?


Solution

  • You can let dusk wait until your dynamic elements are available:

    Waiting For Elements:

    If you need to pause the test for a given number of milliseconds, use the pause method:

    $browser->pause(1000);
    

    The waitFor method may be used to pause the execution of the test until the element matching the given CSS selector is displayed on the page. By default, this will pause the test for a maximum of five seconds before throwing an exception. If necessary, you may pass a custom timeout threshold as the second argument to the method:

    // Wait a maximum of five seconds for the selector...
    $browser->waitFor('.selector');
    
    // Wait a maximum of one second for the selector...
    $browser->waitFor('.selector', 1);