0

I'm using Mink together with the Zombie Driver to write acceptance Tests. Now I'm trying to make an ajax call, so I'm using the wait($timeout) method to wait for the response, but it does not work. I'm calling wait like $this->session->wait(20000), but when I timed it using the php function time(), it was apparent that this takes almost nothing time to complete, so I assumed it is a non-blocking call, and it would block, if I tried to access things in $this->session->getPage(), but that's not true either. I initially had a ($.active === 0) condition as the second Argument of wait(), but that didn't work, so I removed the second Argument to isolate the Problem.
Is it necessary to use a spin() function as in behat's docs, or is the wait() function enough. If spin() is necessary then what is wait() for?

Update

Having installed the phantomjs driver for mink, and seeing that it works, I must conclude that the Problem is with the Zombie Driver.

user2268997
  • 1,263
  • 2
  • 14
  • 35

2 Answers2

0

You can use wait() for a JS condition like:

wait(30000, "document.readyState === 'complete'")

Use spin for custom waits with functions as described in documentation.

Here are some other examples for ajax how-to-make-behat-wait-for-an-ajax-call

Community
  • 1
  • 1
lauda
  • 4,153
  • 2
  • 14
  • 28
0

I found the same issue and found this function on the internet, it works fine. Reference.

/**
 * Wait
 *
 * @param integer $time  * @param string  $condition
 *
 * @throws BehaviorException If timeout is reached
 */
 public function wait($time = 10000, $condition = null){
    if (!$this->getSession()->getDriver() instanceof Selenium2Driver) {
        return;
    }
    $start = microtime(true);
    $end = $start + $time / 1000.0;
    if ($condition === null) {
        $defaultCondition = true;
        $conditions = [
            "document.readyState == 'complete'",           // Page is ready
            "typeof $ != 'undefined'",                     // jQuery is loaded
            "!$.active",                                   // No ajax request is active
            "$('#page').css('display') == 'block'",        // Page is displayed (no progress bar)
            "$('.loading-mask').css('display') == 'none'", // Page is not loading (no black mask loading page)
            "$('.jstree-loading').length == 0",            // Jstree has finished loading
        ];
        $condition = implode(' && ', $conditions);
    } else {
        $defaultCondition = false;
    }
    // Make sure the AJAX calls are fired up before checking the condition
    $this->getSession()->wait(100, false);
    $this->getSession()->wait($time, $condition);
    // Check if we reached the timeout unless the condition is false to explicitly wait the specified time
    if ($condition !== false && microtime(true) > $end) {
        if ($defaultCondition) {
            foreach ($conditions as $condition) {
                $result = $this->getSession()->evaluateScript($condition);
                if (!$result) {
                    throw new BehaviorException(
                        sprintf(
                            'Timeout of %d reached when checking on "%s"',
                            $time,
                            $condition
                        )
                    );
                }
            }
        } else {
            throw new BehaviorException(sprintf('Timeout of %d reached when checking on %s', $time, $condition));
        }
    }
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164