我正在使用最新的Angular和最新的Protractor版本,我想知道如何在我的测试中使用ngFor循环.
在我的AngularJS应用程序中,它很简单.我刚刚使用了像by.repeater这样的东西,魔法就在后面完成了.
然而现在在Angular 4 APP中我并不那么幸运.
基于此ticket,尚不支持.另一方面,当somone已经使用它时,我看到了stackoverflow的票.
无论如何我的HTML是:
<div *ngFor="let org of userOrgList; count as count">
<button class="btn btn-default btn-lg col-xs-12" type="submit" (click)="selectOrg(org.id)">{{org.name}}</button>
</div>
我的测试看起来像:
var organizations = element.all(by.repeater('org of userOrgList'));
it('should have an org with specific name', function() {
expect(organizations.get(0).getText()).toEqual('myOrgName');
});
我收到一个错误:
Failed: Index out of bound. Trying to access element at index: 0, but
there are only 0 elements that match locator by.rep eater(“let org of
userOrgList; count as count”)
我的问题是:
如何在我的量角器测试中使用ngFor
最佳答案 解决方案我发现这对我有用,但仍然不像转发器那样干净
// HTML FILE: Added id to DIV
<div *ngFor="let org of userOrgList; count as count" id="organizations">
<button class="btn btn-default btn-lg col-xs-12" type="submit" (click)="selectOrg(org.id)">{{org.name}}</button>
</div>
//TEST FILE:
var organizations = element.all(by.id('organizations')).all(by.css('button'));
var firstOrg = organizations.get(0);
it('should have an org with specific name', function() {
expect(firstOrg.getText()).toEqual('Name you expect');
});
使用标准表中继器,它可能看起来像:
var data = element.all(by.css('table[name='' + TableName + '']')).all(by.css('tr td'));
expect(data.get(0).getText()).toEqual('0');