2012-09-25 3 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 

포인트는 마지막 단계에서이 '오류 풍선'의 성공에 따라 다음 녹색 또는 빨간색 풍선을 가져올 것이다 아약스 호출 있다는 것이다 캠페인에 완료되지 않은 변경 사항을 저장 수술. 현재 내가하는 일은 이후입니다. '그리고 저장을 누릅니다 ...'이 풍선이 나타날 시간을주기 위해 잠을 자겠습니다 (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 
} 

자바 스크립트와 아약스를 실행하는 밍크 드라이버를 사용해야합니다 (기본값은 아닙니다). 경우

1

당신이 Prototypejs (예를 들어, 마 젠토)를 사용하고, 해당하는 코드는 다음과 같습니다

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

나는 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의 서브 클래스를했다.

내가 이것을 한 후에 AngularJS 테스트가 작동하고 있음을 발견했습니다. 지금까지는 Firefox에서만 테스트했습니다.