我需要一些有关 Protractor 中嵌套 for 循环以及正确转换/理解 promise 的帮助。在下面的“测试”中,该功能适用于所有值,但是一旦我尝试放入嵌套的 for 循环,事情就会向南发展。有人对此有一个干净的建议吗?我尝试过 forEach ,有些人指出它本质上处理 promise 问题,但我似乎得到了相同的结果。
我的测试数据如下:
objectPage.chartValues = {
[['chart','ChartID01'],['title','TitleText01'],['Name01','value01'],['Name02','Value02']],
[[‘chart','ChartID02'],['title','TitleText02'],['Name01','value01'],['Name02','Value02']],
[[‘chart','ChartID03'],['title','TitleText03'],['Name01','value01'], [‘Name02’,'Value02'],['Name03','Value03']]
}
it ('test', function (){
for (chartNumber = 0; chartNumber < objectPage.chartValues.length; chartNumber++) {
for (chartEntry = 1; chartEntry < ObjectPage.chartValues[chartNumber].length; chartEntry++) {
//for readability of next call pulled out here
chart = objectPage.chartValues[chartNumber][0][1];
name = objectPage.chartValues[chartNumber][chartEntry][0];
value = objectPage.chartValues[chartNumber][chartEntry][1];
pageObject.getbackgroundcolor(chart, name).then(function (color) {
expect(pageObject.getElementFromTable(chart, name, color).getText())
.toEqual(value);
});
}
}
});
//function calls in pageobject the call for get background is straight forward.
this.getbackgroundcolor = function (chartName, valueName) {
return element(by.id(chartName)).element(by.cssContainingText('.dxc-item', valueName)).element(by.tagName('rect')).getAttribute('fill');
//get element is very similar.
this.getElementFromTable = function(chartName, valueName, colorname) {
some searching stuff..
return element(by.css(‘tspan'));
我的结果似乎表明外观执行,但没有从实际期望返回。最后尝试找到背景颜色为 null 的项目的值。我知道这不是真的,因为我已经单独并按顺序运行了所有值,没有出现问题。希望我避免了剪切和过去/概括错误。
谢谢。
更新: it('验证页面上的图表', function () {
myChartlength = objectPage.chartValues.length;
for (chartNumber = 0; chartNumber < myChartlength; chartNumber++) {
(function (chartNumber) {
myEntrylength = objectPage.chartValues[chartNumber].length;
chartValues = objectPage.chartValues[chartNumber];
for (chartEntry = 2; chartEntry < myEntrylength; chartEntry++) {
(function (chartEntry) {
//pulled these out for readablility of next call.
chart = chartValues[0][1];
name = chartValues[chartEntry][0];
value = chartValues[chartEntry][1];
console.log('chart: ' + chart + ', name: ' + name + ', value: ' + value);
page.getbackgroundcolor(chart, name).then(function (color) {
expect(objectPage.getElementFromTable(chart, name, color).getText()).toEqual(value);
});
})(chartEntry);
};
})(chartNumber);
};
});
最佳答案
是的,如果我正确理解你的问题,那么你的问题是异步的。在返回任何 promise 之前,它会通过循环触发。
要循环测试,我发现的最佳解决方案是使用 IIFE
( Instantly Invoked Function Expression )。在其中,您创建循环,创建 iife,并传入索引。
这是一个基本示例:
describe('to loop tests', function() {
var data = ['1', '2', '3'];
for(var i = 0; i < data.length; i++) {
// create your iife
(function(i) {
it('pass in the index to an iife', function() {
console.log('i is: ' + i);
expect(data[i]).toBe(true);
});
})(i); // pass in index
}
});
这非常适合从数据文件或其他内容进行数据驱动测试。如果您需要多个循环,就像示例代码中那样,您只需创建多个 iife。
关于javascript - Protractor 嵌套 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31504840/