2014-03-03 51 views
3

我有一个基本上可以添加和删除列的表格的报告页面。当你添加一列时,该列的数据被取出并使用角度加载ajax。如何让Behat等待Angular Ajax调用?

考虑这个贝哈特场景:

Given I have a user named "Dillinger Four" 
And I am on "/reports" 
When I add the "User's Name" column 
Then I should see "Dillinger Four" 

我怎样才能让贝哈特等到角的Ajax调用完成?我想避免使用睡眠,因为睡眠会增加不必要的延迟,并且如果通话时间过长,将会失败。

我用下面的等待jQuery代码:

$this->getSession()->wait($duration, '(0 === jQuery.active)'); 

我还没有发现类似的值,以检查具有角。

+0

贝哈特具有旋转功能:http://docs.behat.org/cookbook/using_spin_functions.html ......不正是我一直在寻找,但是这可能是因为它得到不错的。 – bitlather

+0

我没有发现任何东西比旋更好了:( –

回答

3

上面的链接很有帮助,只是为了展开它,并节省一些时间。

/** 
* @Then /^I should see "([^"]*)" if I wait "([^"]*)"$/ 
*/ 
public function iShouldSeeIfIWait($text, $time) 
{ 
    $this->spin(function($context) use ($text) { 
     $this->assertPageContainsText($text); 
     return true; 
    }, intval($time)); 
} 


/** 
* Special function to wait until angular has rendered the page fully, it will keep trying until either 
* the condition is meet or the time runs out. 
* 
* @param function $lambda A anonymous function 
* @param integer $wait Wait this length of time 
*/ 
public function spin ($lambda, $wait = 60) 
{ 
    for ($i = 0; $i < $wait; $i++) 
    { 
     try { 
      if ($lambda($this)) { 
       return true; 
      } 
     } catch (Exception $e) { 
      // do nothing 
     } 

     sleep(1); 
    } 

    $backtrace = debug_backtrace(); 

    throw new Exception(
     "Timeout thrown by " . $backtrace[1]['class'] . "::" . $backtrace[1]['function'] . "()\n" . 
     $backtrace[1]['file'] . ", line " . $backtrace[1]['line'] 
    ); 
} 

然后在您的场景中使用:

然后,我应该看到“的东西在页面上。”如果我等待“5”

1

可以使用代码从角的Protractor库等待装载。在这里你可以找到一个函数waitForAngular()。它只是等待一个client-side function同名 这里是工作的PHP代码。

class WebContext implements Context 
{ 
    /** 
    * @Then the list of products should be: 
    */ 
    public function theListOfProductsShouldBe(TableNode $table) 
    { 
     $this->waitForAngular(); 
     // ... 
    } 

    private function waitForAngular() 
    { 
     // Wait for angular to load 
     $this->getSession()->wait(1000, "typeof angular != 'undefined'"); 
     // Wait for angular to be testable 
     $this->getPage()->evaluateScript(
      'angular.getTestability(document.body).whenStable(function() { 
       window.__testable = true; 
      })' 
     ); 
     $this->getSession()->wait(1000, 'window.__testable == true'); 
    } 
} 
+0

'$这个 - > evaluateScript()'应该是'$这个 - >的getSession() - > evaluateScript()'为了使这项工作时,我们不知道如果角被加载,以及,我用这个:http://ideone.com/JYO7ug - 这有我们的jQuery版本太多 – Jessica

+0

@Jessica好点,添加了这个答案 –

+0

现在,这实际上是导致错误在我的套房。造成一个全新的页面加载 - ,当我的URL在#结束,当这条线被执行,页面重新加载,而不#我已经注意到了。 – Jessica