2015-03-02 56 views

回答

62

簡單的答案是,它使量角器等不到角的承諾,如從$http$timeout解決,你可能想,如果你在$http$timeout(例如,「負載測試行爲做「消息),或測試非Angular站點或頁面(例如單獨的登錄頁面)。

例如,測試一個按鈕,一個請求期間設置一個加載消息可以獲取一個元素+時檢查它的內容

element(by.css('button[type="submit"]')).click(); 
browser.ignoreSynchronization = true; 
expect(element(by.css('.message')).getText().toBe('Loading...');  
browser.ignoreSynchronization = false; 
expect(element(by.css('.message')).getText().toBe('Loaded'); 

一個更復雜的答案將其設置爲true是,設置它到true表示後續添加/注入到控制流程中也不會添加browser.waitForAngular。有些情況下,理解控制流程,以及何時/如何添加/注入事物很重要。例如,如果您使用browser.wait來測試多階段過程,那麼在之後,將傳遞到wait的函數注入到控制流中,其餘的測試功能已添加到控制流中。

element(by.css('button[type="submit"]')).click(); 
browser.ignoreSynchronization = true; 
expect(element(by.css('.message')).getText().toBe('Stage 1'); 
browser.wait(function() { 
    // This function is added to the control flow after the final 
    // browser.ignoreSynchronization = false in the test 
    // so we need to set it again here 
    browser.ignoreSynchronization = true; 
    return element(by.cssContainingText('.message', 'Stage 2')).isPresent().then(function(isPresent) { 
    // Cleanup so later tests have the default value of false 
    browser.ignoreSynchronization = false; 
    return !isPresent; 
    }); 
}); 
expect(element(by.css('.message')).getText().toBe('Stage 2'); 
browser.ignoreSynchronization = false; 
expect(element(by.css('.message')).getText().toBe('Stage 3'); 

使用browser.ignoreSynchronization的替代方法是訪問標準的webdriver API直接

element(by.css('button[type="submit"]')).click(); 
expect(browser.driver.findElement(by.css('.message')).getText().toBe('Loading...');  
expect(element(by.css('.message')).getText().toBe('Loaded'); 

使用驅動程序的方法直接找到的元素意味着該系統將嘗試找到他們無需等待任何正在進行$http請求完成,很像設置browser.ignoreSynchronization = true

15

此設置控制量角器是否應等待頁面上的角度。它沒有正確記載,但這裏是documentation string from the code

/** 
    * If true, Protractor will not attempt to synchronize with the page before 
    * performing actions. This can be harmful because Protractor will not wait 
    * until $timeouts and $http calls have been processed, which can cause 
    * tests to become flaky. This should be used only when necessary, such as 
    * when a page continuously polls an API using $timeout. 
    * 
    * @type {boolean} 
    */ 

換句話說,如果對非現場角測試 - 設置ignoreSynchronization設置true。作爲一個真實世界的例子,請參閱從角度頁面打開無角頁面時遇到的挑戰之一:Non-angular page opened after a click