Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit + Selenium 2: Action on ajax load

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('[email protected]');
$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
like image 518
Pavel Avatar asked Dec 25 '22 23:12

Pavel


1 Answers

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.

like image 180
Pavel Avatar answered Jan 05 '23 16:01

Pavel