2015-01-21 29 views
6

我是新來量角器,我試圖測試酥料餅的事件來解釋, 這裏是我的代碼:如何使用browser.sleep在量角器

describe('popover directive', function() { 
     var buttons= element.all(by.tagName('button')); 

     it('should show the popover title by clicking', function() { 
      var popTitle="testTitle";    
      var popContent="testContent";     

      element(by.model('title')).clear().sendKeys(popTitle);  
      element(by.model('content')).clear().sendKeys(popContent).then(function(){ 
       buttons.get(0).click().then(function(){ 
        browser.sleep(1000); 
        expect(element(by.className('popover-title')).getText()).toMatch(popTitle);     
        expect(element(by.className('popover-content')).getText()).toMatch(popContent); 
       });  
      });      
      buttons.get(0).click(); 
      browser.sleep(1000);  
      expect(element(by.css('[class="popover fade top in"]')).isPresent()).toBe(false);  
     }); 
    }); 

1.如果我不添加像browser.sleep()延遲時間代碼,測試將失敗,並顯示一條消息:

NoSuchElementError: No element found using locator: By.className('popover-title') 

這有可能不加延遲時間碼......像browser.sleep()?如果這是不可能的,如何有意義地設置睡眠時間?這與CSS動畫有一些聯繫嗎?

2.使用browser.waitForAngular()click().then(function(){...})而不是browser.sleep()似乎沒有用,會得到相同的錯誤信息。

如果有人能夠回答這些問題,這將非常棒,非常感謝。

回答

8

您必須添加睡眠的原因可能是因爲您的動畫。許多動畫使用setTimeout,Angular(因此量角器)不知道和不等待。相當於setTimeout的角度是其$timeout服務。

但是,您經常無法更改動畫庫以停止使用setTimeout。要在量角器中使用此功能,請使用browser.wait,並使用超時(不睡眠)。

buttons.get(0).click(); 

browser.wait(function() { 
    return element(by.css('[class="popover fade top in"]')).isPresent().then(function(present) { 
    return !present; 
    }); 
    // or by checking any other element to know that the animation completed 
}, 1000); 

expect(element(by.css('[class="popover fade top in"]')).isPresent()).toBe(false);  

用量角器1.7,你將能夠使用expectedConditions庫,以便你可以這樣做:

var EC = protractor.ExpectedConditions 
buttons.get(0).click(); 

var e = element(by.css('[class="popover fade top in"]')); 
browser.wait(EC.not(EC.presenceOf(e)), 1000); 

expect(e.isPresent()).toBe(false); 
+0

謝謝@hankduan。這個答案幫助我終於弄清楚了,只是等不及看到量角器1.7! – Kiry 2015-01-22 02:51:07

+0

我已經使用上面的一個,但注意到該元素不存在於代碼的等待塊中。如何檢查動畫是否加載? – EnugulaS 2015-03-27 01:08:58

0

,而不是使用瀏覽器,宣告量角器實例:

ptor = protractor.getInstance(); 
    ptor.ignoreSynchronization = true; 

然後使用:

ptor.get 

,而不是瀏覽器。

+0

感謝,但我不只是想取代「瀏覽器」,由ptor,我真的想從我的代碼中移除「browser.sleep()」。 – Kiry 2015-01-21 11:04:03

+0

ptor.ignoreSynchronization = true;將允許你這樣做 – Ziwdigforbugs 2015-01-21 13:31:32

+2

使用'browser.ignoreSynchronization = true;'不會讓你奇蹟般地移除睡眠語句。該聲明的唯一目的是,如果您使用的是非角度應用程序,並且您從未**必須將其添加到角度應用程序中。 – hankduan 2015-01-21 19:55:09