Skip to content

How to work with jQuery, Prototype, Dojo AJAX

hgati edited this page Jun 15, 2015 · 5 revisions
$submitButton = $driver->findElement(WebDriverBy::id('Submit'));
$submitButton->click();
waitForAjax($driver); // Default is jquery same with waitForAjax($driver, 'jquery');
//waitForAjax($driver, 'prototype');
//waitForAjax($driver, 'dojo');
$anotherButton = $driver->findElement(WebDriverBy::id('secondButton'));
  • Where waitForAjax method is defined as
  • Two methods are defined use one of these
function waitForAjax($driver, $framework='jquery')
{
    // javascript framework
    switch($framework){
        case 'jquery':
            $code = "return jQuery.active;"; break;
        case 'prototype':
            $code = "return Ajax.activeRequestCount;"; break;
        case 'dojo':
            $code = "return dojo.io.XMLHTTPTransport.inFlight.length;"; break;
        default:
            throw new Exception('Not supported framework');
    }

    do {
        sleep(2);
    } while ($driver->executeScript($code));
}
function waitForAjax($driver, $framework='jquery')
{
    // javascript framework
    switch($framework){
        case 'jquery':
            $code = "return jQuery.active;"; break;
        case 'prototype':
            $code = "return Ajax.activeRequestCount;"; break;
        case 'dojo':
            $code = "return dojo.io.XMLHTTPTransport.inFlight.length;"; break;
        default:
            throw new Exception('Not supported framework');
    }

    // wait for at most 30s, retry every 2000ms (2s)
    $driver->wait(30, 2000)->until(
        function ($driver, $code) {
            return !$driver->executeScript($code);
        }
    );
}

Original article/Other solutions http://agilesoftwaretesting.com/?p=111

Clone this wiki locally