1

我正在嘗試使用量角器來測試我的警報是否正確顯示。這是我的一個規格文件。使用量角器測試UI自舉警報

的HTML代碼看起來像

<div class="alert ng-scope floater bottom left fadeZoomFadeDown alert-success" 
ng-class="[type ? 'alert-' + type : null]" 
style="display: block;"><!-- ngIf: dismissable --> 
    <button type="button" class="close ng-scope" ng-if="dismissable" ng-click="$hide()"> 
    </button><!-- end ngIf: dismissable --> <strong ng-bind="title" class="ng-binding"></strong>&nbsp; 
<span ng-bind-html="content" class="ng-binding">Test Person added successfully. </span> 
</div> 

的spec.js文件,該警報

it('should be able to add a person to the person list from the list person view', function() { 
    element(by.linkText('Persons')).click(); 
    personsUrl = browser.getCurrentUrl(); 

    count = element.all(by.repeater('person in persons')).length; 
    element(by.linkText('Add Person')).click(); 
    element(by.id('person.firstName')).sendKeys('Test Person'); 

    //After adding person return to the persons list view. 
    var alertElement = element(by.css('.alert-success')); 
    element(by.buttonText('Add Person')).click().then(function(){ 
     expect(browser.getCurrentUrl()).toEqual(personsUrl); 
     browser.wait(protractor.ExpectedConditions.visibilityOf(alertElement), 10000) //Wait for 10 seconds until alert is visible 
     .then(function(){ 
      expect(alertElement.isDisplayed()).toBe(true); 
     }); 
    }); 
}); 

終端返回錯誤說:「失敗:沒有找到元素使用定位器:By.cssSelector (「.alert-success」)「,但是當我手動測試並暫停瀏覽器時,我可以檢查警報並看到它有幾個類,警報成功絕對是其中之一。我只是不明白我做錯了什麼。

在此先感謝

回答

2

我想提醒不會立即可見,並不會在DOM所有的時間。所以你必須等到它可見,然後對它進行操作。以下是如何 -

var alertElement = element(by.css('.alert-success')); 
element(by.buttonText('Add Tenant')).click().then(function(){ 
    browser.wait(protractor.ExpectedConditions.visibilityOf(alertElement), 10000) //Wait for 10 seconds until alert is visible 
    .then(function(){ 
     expect(alertElement.isDisplayed()).toBe(true); 
     //Other operations on alert 
    }); 
}); 

希望它有幫助。

+1

是其更好地把它的承諾,'點擊()'函數返回的內部。然而,這並不是完全必要的,因爲量角器在內部處理Q形式的承諾,但它可以避免您可能面臨的大部分錯誤。使用相同的代碼更新代碼。 –

+0

Hey Girish我現在正在失敗:等待10102ms錯誤後超時。我是否應該將conf.js文件更改爲$ interval而不是$ timeout?我一直聽說這是解決量角器中很多問題的方法。 – jpstearns

+0

你確定你正在獲得正確的元素嗎?你可以在問題中更新你的HTML代碼嗎?添加時間間隔只會增加您的等待時間,除非您在指定的時間內獲得警報,否則不會受益。謝謝 –

1

測試的一個問題<uib-alert>:如果您使用dismiss-on-timeout屬性,則量角器將檢測到超時作爲未完成的動作,並且在超時過去之前它不會評估任何其他表達式。一旦超時,<uib-alert>消失!

NB這個例子中爲角1.5和量角器3.3.0

鑑於DOM元素

<uib-alert id="successAlert" type="success" close="$ctrl.closeAlert('success')" dismiss-on-timeout="{{$ctrl.alertTimeout}}">Success - You are great, good job!</uib-alert> 

這browser.wait塊將不工作(它會超時):

doSomethingToMakeTheAlertDisplay(); 

browser.wait(protractor.ExpectedConditions.visibilityOf(this.successAlert), 15000).then(function(result) { 
    console.log("browser.wait is done waiting"); 
    return result; 
}) 

但是,如果您禁用同步,然後重新啓用它,那麼browser.wait將工作

doSomethingToMakeTheAlertDisplay(); 

browser.ignoreSyncronization = true; 

browser.wait(protractor.ExpectedConditions.visibilityOf(this.successAlert), 15000).then(function(result) { 
    browser.ignoreSyncronization = false; 
    console.log("browser.wait is done waiting"); 
    return result; 
}) 

根本的問題是在這裏拍攝的:https://github.com/angular/protractor/issues/169