2012-09-25 12 views
9

シナリオ:キャンペーンに不完全な変更を変更し、保存しBehatにAJAXコールを待つ方法を教えてください。

Given I click on the Campaign section folder 
And I press Save in the selected Campaign 
Then I should see an error balloon informing the changes cannot be saved 

ポイントは最終段階でこの「エラーバルーン」は、その後の成功に応じて、緑や赤の風船をもたらすでしょうAJAX呼び出しであるということです操作。現在、私がやっていることは、 の後です。「保存してください...」私はこのバルーンが現れる時間を与えるために睡眠(3)を行います。これはあなたが時間を無駄にしていることはあまり賢明ではないようです。また、この呼び出しが処理されるまでには時間がかかることもあります。

あなたはビートを寝かせるのではなく、どのようにあなたの行動テストを待ってAjaxを行うのですか?

フィードバックに感謝いたします。

+0

いくつかのコードを表示しますか? – StaticVariable

答えて

26

これは、あなたの未処理のajax呼び出しが0になるのを待つことによって行われます.jQuery.activeがそれをチェックします。

FeatureContext.phpでは、次のようなことができます。

public function iShouldSeeAnErrorBalloon($title) 
{ 
    $time = 5000; // time should be in milliseconds 
    $this->getSession()->wait($time, '(0 === jQuery.active)'); 
    // asserts below 
} 

また、javascriptとajaxを実行するMinkドライバを使用していることを確認してください(デフォルトはありません)。

1

あなたはPrototypejs(例えばMagentoの)を使用している場合は、同等のコードは次のとおりです。

public function iShouldSeeAnErrorBalloon($title) 
{ 
    $this->getSession()->wait($duration, '(0 === Ajax.activeRequestCount)'); 
    // asserts below 
} 
+1

ここで$ duration変数はどこから来ますか? –

+0

http://mink.behat.org/api/behat/mink/session.html#wait() 'public void wait(整数時間、文字列条件) JS条件が真になるまでしばらく待ってください.' – Steff

+0

私は回線番号を使用したので、間違いなく間もなく期限切れとなる予定ですが、そのリンクを更新するだけです:https://github.com/Behat/Mink/blob/master/src/Behat/Mink/Session.php #L318-L329 – DanielM

2

私はAjaxの呼び出しの結果として変更するDOMを待つことによってそれを行います。

public function findAll($selector, $locator, $waitms=5000) 
{ 
    $xpath = $this->getSession()->getSelectorsHandler()->selectorToXpath($selector, $locator); 

    // add parent xpath before element selector 
    if (0 === strpos($xpath, '/')) { 
     $xpath = $this->getXpath().$xpath; 
    } else { 
     $xpath = $this->getXpath().'/'.$xpath; 
    } 

    $page = $this->getSession()->getPage(); 

    // my code to wait until the xpath expression provides an element 
    if ($waitms && !($this->getSession()->getDriver() instanceof \Behat\Symfony2Extension\Driver\KernelDriver)) { 
     $templ = 'document.evaluate("%s", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null).snapshotLength > 0;'; 

     $waitJs = sprintf($templ, $xpath); 

     $this->getSession()->wait($waitms, $waitJs); 
    } 

    return $this->getSession()->getDriver()->find($xpath); 
} 

が続い\ Behat \ミンク\セッションで、私はそのクラスを使用するようにコンストラクタを変更:私はAsyncDocumentElementそれを呼び出し、findAllメソッドをオーバーライドし、のdocumentElementのサブクラスを作りました。

public function __construct(DriverInterface $driver, SelectorsHandler $selectorsHandler = null) 
{ 
    $driver->setSession($this); 

    if (null === $selectorsHandler) { 
     $selectorsHandler = new SelectorsHandler(); 
    } 

    $this->driver   = $driver; 
    $this->page    = new AsyncDocumentElement($this); 
    $this->selectorsHandler = $selectorsHandler; 
} 

このようにしてしまえば、AngularJSテストが機能していることがわかりました。これまでのところ、私はFirefoxでのみテストしました。

関連する問題