2014-03-03 62 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