如何在Angular 4.0中设置量角器的* ngFor集合?

如何在Angular 4.0中设置量角器的* ngFor集合?

问题描述:

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。 什么是正确的方法?

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

这是使用它可以显示只有那些有名字的用户的方式之一。

角度选择器如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']);仍然没有找到元素 –