PHPUnit + Selenium 2: Action on ajax load

ajax, automated-tests, php, phpunit, selenium

Solution

Ok, there is a kind of solution, I do not really like it, but it is something instead of nothing.

The idea is to use more smart "sleep", there is a method `waitUntil()` which takes an `anonymous function` and `timeout` in milliseconds. What is does - runs this passed function in loop until timeout hits or your function return `True`. So you can run something and wait until context is changed:

$this->waitUntil(function () {
    if ($this->byCssSelector('h1')) {
        return true;
    }
    return null;
}, 5000);

I still will be glad if somebody give better solution.

Problem

During test I need to do following: - Click button which leads to ajax request and redirect after that - Check that user has been redirected to correct page My Code: ``` $this->byId('reg_email')->value('test@example.com'); $this->byId('reg_password')->value('seecret'); // No form here, so i can't just call submit() // This click invokes ajax request $this->byId('reg_submit')->click(); // Check page content (this page should appear after redirect) $msg = $this->byCssSelector('h1')->text(); $this->assertEquals('Welcome!', $msg); ``` Problem - Message check goes right after click, not before ajax request and page redirect Solution, I do not like: - Add `sleep(3);` before content check. I do not like it because: - It is silly - In case of fast responses I am going to lose time, in case of long requests I am going to get content check before ajax request finishes. I wonder, is there any way to track ajax request+refresh and check for content just in time? My setup: - PHP 5.4, 5.5 also available - PHPUnit 3.8 - Selenium RC integration for PHPUnit 1.3.1 - Selenium-server-standalone 2.33.0 - Windows 7 x64 - JRE 7

Original source