4

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

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This fails into a memory leak.
This works well actually, it works almost the same as a implicitWait.
0

Hmm, I'm not sure if it can be usefull for you, but you can try to use this :

$this->timeouts()->implicitWait(20000);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.