我需要检查网格是否包含所有指定的标题,但是并非所有标题在DOM中同时显示,所以我必须滚动到“发现”标题。更多标题,但由于javascript的异步特性,我不知道如何使其正常工作。
我尝试过这样的事情,但我认为这不是一个好方法。
SELECT TO_CHAR(SYSTIMESTAMP, 'tzr') FROM dual;
并且调用如下所示:var self = this;
this.checkGridHeaders = function(gridName, expectedHeaders) {
var previous;
var headers = this.poGridUtil.getGrid( gridName ).element( by.css('.ui-grid-render-container-body'))
.element( by.css('.ui-grid-header') ).all( by.repeater('col in colContainer.renderedColumns track by col.uid') );
headers.each(function(header, headerIndex) {
if(expectedHeaders.length != 0 && header != 'undefined') {
header.getText().then(function(headerText){
if(headerText == '') {
browser.executeScript("arguments[0].scrollIntoView();", previous).then(function() {
self.checkGridHeaders(expectedHeaders);
});
}
previous = header;
console.log('***Element: ' + headerText );
expect(expectedHeaders.indexOf(headerText)).not.toEqual(-1);
header = 'undefined';
});
}
});
};
基本上我想检查checkGridHeaders('grid-className', ['header1', 'header2', 'header3', 'header4']);
是否可见,如果没有,则滚动到上一个已知标题,然后再次检查。我只是不知道如何正确地写它。
答案 0 :(得分:0)
尝试以下代码,
var self = this;
this.checkGridHeaders = function(gridName, expectedHeaders) {
var defer = protractor.promise.defer();
this.poGridUtil.getGrid( gridName ).element( by.css('.ui-grid-render-container-body .ui-grid-header'))
.all( by.repeater('col in colContainer.renderedColumns track by col.uid')).then(function (headers) {
var headerText =[];
for(var i=0;i<headers.length;i++){
browser.executeScript("arguments[0].scrollIntoView();", headers[i].getWebElement())
headerText.push(headers[i].getText())
}
defer.fulfill(headerText);
})
expect(defer.promise()).toContain(expectedHeaders)
});
};
答案 1 :(得分:0)
我明白了。这不是最好的方法,但有效。
this.checkGridHeaders = function(gridName, expectedHeaders, iterationNumber) {
if(isNaN(iterationNumber))
iterationNumber = 5;
var headers = [];
var findHeaders = function() {
return self.poGridUtil.getGrid( gridName ).element( by.css('.ui-grid-render-container-body .ui-grid-header'))
.all( by.repeater('col in colContainer.renderedColumns track by col.uid')).then(function (foundHeaders) {
var next = foundHeaders[foundHeaders.length - 1];
return self.getResolvedText(foundHeaders).then(function(result) {
browser.executeScript("arguments[0].scrollIntoView();", next.getWebElement());
for(var i = 0; i < result.length; i++) {
var cut = result[i].toString().replace("\n", "");
var shorten = cut.trim();
if(shorten !== "" && shorten !== " ")
headers.push(shorten);
}
browser.sleep(500);
return result;
});
});
};
var headerFinders = [];
for (var iterIndex = 0; iterIndex < iterationNumber; iterIndex++)
headerFinders.push(findHeaders());
var allPromises = headerFinders.map(function(item) {
return item;
});
var finderResult = protractor.promise.all(allPromises).then(function(presentValues) {
return presentValues;
});
finderResult.then(function(resultItems) {
var newHeaders = headers.filter( function( item, index, inputArray ) {
return inputArray.indexOf(item) == index;
});
for(var index = 0; index < expectedHeaders.length; index++) {
expect(newHeaders).toContain(expectedHeaders[index], 'Nie znaleziono naglowka: ' + expectedHeaders[index]);
}
});
};
如果有人知道如何整理此代码,请随时发表评论:)