2017-10-14 130 views
0

app.component.html如何在Angular 4.0中設置量角器的* ngFor集合?

<div class="student-list" *ngFor="let student of students"> 
    <p>{{student.name}}</p> 
</div> 

我想測試學生是否有名稱。但是students是在app.component.ts中設置的列表。

現在我該如何設置量角器的students

我已經做了以下

element(by.binding('students')).sendKeys(['abc', 'xyz']); 

但是這是行不通的,它拋出Failed: unknown error: angular is not defined。 什麼是正確的方法?

回答

0
<div class="student-list" *ngFor="let student of students"> 
    <p *ngIf="student.name">{{student.name}}</p> 
</div> 

這是使用它可以顯示只有那些有名字的用戶的方式之一。

1

角度選擇器如by.modelby.binding不支持Angular2及其以上的版本。

由於每docs

量角器可與AngularJS版本大於1.0.6/1.1.4,並 與角應用程序兼容。請注意,對於Angular應用程序, 不支持by.binding()和by.model()定位器。我們 推薦使用by.css()。

,你可以在這裏看到的例子

https://angular.io/guide/upgrade#e2e-tests

編輯

解決您的問題,您可以使用標識

HTML

<div *ngFor="let student of students" id="students"> 
    <p class="student-list"> {{student.name}}</p> 
</div> 

測試

var students = element.all(by.id('students')).all(by.css('student-list')); 
var firstOrg = organizations.get(0); 
it('should have an org with specific name', function() { 
    expect(firstOrg.getText()).toEqual('Name you expect'); 
}); 
+0

沒關係。但是,如果我使用by.css元素(by.css('。student-list'))。sendKeys(['abc','xyz']);這也失敗了說沒有找到使用locator的元素:通過(css選擇器,.student-list) –

+0

它應該沒有。 – Sajeetharan

+0

沒有試過。 like(by.css('student-list'))。sendKeys(['abc','xyz']);仍然沒有找到元素 –