Skip to content

How to work with AJAX (jQuery, Prototype, Dojo)

Ondřej Machulda edited this page Nov 4, 2020 · 3 revisions

Wait unit AJAX request (done using jQuery) is finished:

// Declare own callable function which could be passed to `$driver->wait()->until()`.

function jqueryAjaxFinished(): callable
{
    return static function ($driver): bool {
        return $driver->executeScript('return jQuery.active === 0;');
    };
}
// Somewhere in your script:

$submitButton = $driver->findElement(WebDriverBy::id('Submit'));
$submitButton->click(); // this triggers some AJAX action, which we want to wait until ut is finished

// Here we pass custom callable function to `until()` method. 
// The driver will now periodically call function `jqueryAjaxFinished()` to check whether jQuery.active returns 0.
// It will wait until it happens and next the script continues. 
// If the condition won't happen until timeout (by default 30 sec, could be changed in parameters of `wait()` method),
// it will throw an exception.
$driver->wait()->until(jqueryAjaxFinished());

$anotherButton = $driver->findElement(WebDriverBy::id('secondButton')); // this button is present only after AJAX is loaded

// ...
Clone this wiki locally