使用量角器从表中收集数据的最佳方法是什么。
我收集下面代码中显示的数据,10行需要20-30秒。
buildStr计数器用于为每一行创建一个对象,8是列数。
row = {};
gridRows = [];
element.all(by.css('#contenttableGrid div[role="gridcell"] div')).each(function(element){
element.getText().then(function(text){
row[headerName[buildStr]] = text;
buildStr++;
if(buildStr === 8){
buildStr = 0;
gridRows[rowCounter] = row;
rowCounter++;
row = {};
}
});
});
答案 0 :(得分:1)
我认为加快速度的一种方法是通过在其上注入脚本来提取页面上的所有数据。可以在browser.executeScript()
(docs)的帮助下完成。在您的示例中,每当您调用getText()
时,Protractor都必须向浏览器发出请求,它所做的调用的次数=表中的单元格数。但是使用browser.executeScript()
它会进行一次调用并在浏览器中执行所有操作,这可能非常快。然后,您只需将此数据返回到测试规范并通过Promises使用它。
var headerName = {};
// I assume that this variable holds names for headers
// you can pass data from test spec to injected script as arguments (below)
// promise will be resolved with the value you return from `executeScript`
var promise = browser.executeScript(function (headerName) {
// all the stuff inside this function happens on your page under test
// it is not a Protractor environment
var buildStr = 0;
var rowCounter = 0;
var row = {};
var gridRows = [];
var cells = document.querySelectorAll('#contenttableGrid div[role="gridcell"] div');
for (var i = 0, l = cells.length; i < l; i++) {
var text = cells[i].textContent;
// `headerName` object is passed as an argument from test spec
row[headerName[buildStr]] = text;
buildStr++;
if (buildStr === 8) {
buildStr = 0;
gridRows[rowCounter] = row;
rowCounter++;
row = {};
}
}
// return all collected data back to test spec
return gridRows;
}, headerName); // pass helper object from test spec to injectable function
promise.then(function (gridData) {
console.log(gridData); // result of computations
});
如果您想使用browser.executeScript()
,请务必阅读docs,因为它有很多具体时刻。