Skip to content

How to work with jQuery AJAX

darkwish121 edited this page Oct 30, 2013 · 3 revisions

Updated Page

$submitButton = $driver->findElement(webdriverby::id('Submit'));
$submitButton->click();
waitForAjax($driver);
$anotherButton = $driver->findElement(webdriverby::id('secondButton'));
  • Where waitForAjax method is defined as
  • Two methods are defined use one of these
function waitForAjax($driver) {
	do {
	sleep(2);	
	} while($driver->executeScript('return jQuery.active'));
}
function waitForAjax($driver) {
 // wait for at most 30s, retry every 2000ms (2s)
$driver->wait(30, 2000)->until(function ($driver) {
	return !$driver->executeScript('return jQuery.active');
     });
}

Old Code is below

$session->element('xpath', "//button[@class='button main-button add-to-cart-button']")->click();
$this->waitForAjax();
$session->element('link text', 'View cart')->click();

Where waitForAjax method is defined as

protected function waitForAjax() {
    do {
        sleep(2);
    } while($this->session->execute(array('script' => 'return jQuery.active', 'args' => array())));
}

This code works only for scripts with jQuery framework.

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

Clone this wiki locally