我想为一个使用ng-grid的页面写一个量角器测试。我没有看到任何关于如何做到这一点的文档。在我的页面上,我看到一个包含数据的网格,html看起来像这样:
<div class="gridStyle"
ng-grid="tenantsGridOptions"
ng-if="tenantsGridOptions != undefined" >
</div>如何从量角器查找此栅格上的元素?
发布于 2014-07-04 03:57:24
考虑以下控制器:
var app = angular.module('angularE2EExamples');
app.controller('GridCustomersController', function ($scope, $http) {
$scope.customers = [{id: 1, name: 'Lissa Montrose', email: 'lissa@company.com', city: 'Washington', comment: ''},
{id: 2, name: 'Karri Lanze', email: 'karri@company.com', city: 'Dallas', comment: ''},
{id: 3, name: 'Michael Smith', email: 'michael@company.com', city: 'Berkeley', comment: ''},
{id: 4, name: 'Fred Tyler', email: 'fred@company.com', city: 'Washington', comment: ''}
];
$scope.gridCustomers = {
data: 'customers',
columnDefs: [{field: 'id', displayName: 'Id', width: 30},
{field: 'name', displayName: 'Name'},
{field: 'email', displayName: 'Email'},
{field: 'city', displayName: 'City'},
{field: 'comment', displayName: 'Comment',
cellTemplate: '<input class="form-control input-sm" type="text" ng-input="COL_FIELD" ng-model="row.entity.comment" />'}
],
enableCellSelection: true,
enableRowSelection: false,
enableCellEdit: true,
enableColumnResize: true,
enableColumnReordering: true,
multiSelect: false,
width: 'auto'
};
});和下面的HTML:
<div ng-controller="GridCustomersController">
<div class="gridStyle" ng-grid="gridCustomers" style="height: 200px">
</div>
</div>在ng-grid组件中访问不同元素的一个非常有用的方法是使用by.binding('row.entity.<field>'),其中'field‘是数据模型的关键字。您需要定义一个测试用例,如下所示:
describe('Customer test cases.', function() {
it('Should iterate all grid elements', function(){
browser.get('http://localhost:9000/customers');
element.all(by.binding('row.entity.id')).each(function(cell){
browser.sleep(500);
cell.click();
cell.getText().then(function(text){
console.log('Id: ' + text);
});
});
element.all(by.binding('row.entity.name')).each(function(cell){
browser.sleep(500);
cell.click();
cell.getText().then(function(name){
console.log('Name: ' + name);
});
});
element.all(by.model('row.entity.comment')).each(function(cell){
browser.sleep(500);
cell.click();
cell.sendKeys('New customer.');
});
browser.sleep(2000);
});
});在Plunker中找到控制器的源代码和HTML内容
在本例中,我为最后一列定义了一个自定义模板。因此,我使用by.model('row.entity.<field>')来访问相应的元素。
this Git repository中提供了一个完整的可运行的给定e2e测试示例。
希望能有所帮助。
https://stackoverflow.com/questions/24375766
复制相似问题