PHPUnit + Selenium 2: action when loading ajax

During the test, I need to do the following:

  • Click the button which will lead to ajax request and redirect after that
  • Make sure the user is redirected to the correct page.

My code is:

$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

  • Messages are checked right after clicking, and not before ajax request and page redirection.

Solution, I do not like:

  • Add sleep(3); before checking the contents.

I do not like this because:

  • This is stupid
  • In the case of quick responses, I am going to lose time; in the case of long requests, I am going to get a content check before the ajax request is completed.

I wonder if there is a way to track the ajax + request to update and check the content just in time?

My setup:

  • PHP 5.4, 5.5 is also available
  • PHPUnit 3.8
  • Selenium RC Integration for PHPUnit 1.3.1
  • Selenium-server-standalone 2.33.0
  • Windows 7 x64
  • Jre 7
+4
source share
2 answers

Well, there is a kind of solution, I really don't like it, but it's something instead of nothing.

The idea is to use a smarter "sleep", there is a waitUntil() method that takes anonymous function and timeout values ​​in milliseconds. What it does is it starts this passed function in a loop until a timeout arrives, or your function returns True . This way you can run something and wait for a context change:

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

Anyway, I will be glad if someone gives a better solution.

+10
source

Hmm, I'm not sure if it can be useful for you, but you can try using this:

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

Source: https://habr.com/ru/post/1492887/


All Articles