我有以下功能,它从可用类别列表中选择一个类别。这个功能在我的第一次测试中运行良好。但是在我的第二次测试中具有不同有效类别名称的相同函数失败并出现以下错误。
错误:索引越界。试图访问索引处的元素:0,但只有 0 个元素匹配定位器 By.cssSelector(".grid-view-builder__category")
this.categoryElements = element.all(by.css('.grid-view-builder__category'));
this.selectCategory = function (categoryName) {
var filteredCategories = this.categoryElements.filter(function (category) {
return category.getText().then(function (text) {
log.info(text);
return text === categoryName;
})
})
filteredCategories.first().click().then(function () {
log.info("Select Category: " + categoryName);
}).then(null, function (err) {
log.error("Category: " + categoryName + " Not Found !!" + err);
});
}
规范文件
var columnSelect = require('pages/grid/columns/columnselector-page')()
it('Add Publisher ID Column to the Grid & Verify', function () {
var columnCountBefore = columnSelect.getColumnCount();
columnSelect.openColumnSelector();
columnSelect.selectCategory('Advanced');
columnSelect.selectColumn('Publisher ID');
columnSelect.apply();
var columnCountAfter = columnSelect.getColumnCount();
expect(columnCountAfter).toBeGreaterThan(columnCountBefore);
});
最佳答案
问题可能出在您定义和使用页面对象的方式上。这是一个可以尝试的快速解决方案 - 如果这有帮助,我们将讨论为什么会发生这种情况。
使 categoryElements
成为一个函数,而不是一个属性:
this.getCategoryElements = function () {
return element.all(by.css('.grid-view-builder__category'));
};
this.selectCategory = function (categoryName) {
var filteredCategories = this.getCategoryElements().filter(function (category) {
return category.getText().then(function (text) {
log.info(text);
return text === categoryName;
})
})
filteredCategories.first().click().then(function () {
log.info("Select Category: " + categoryName);
}).then(null, function (err) {
log.error("Category: " + categoryName + " Not Found !!" + err);
});
}
或者,这可能是一个“时间问题”——让我们通过 browser.wait()
添加一个显式等待等待至少一个类别to be present :
var EC = protractor.ExpectedConditions;
var category = element(by.css('.grid-view-builder__category'));
browser.wait(EC.presenceOf(category), 5000);
关于javascript - Protractor - 错误 : Index out of bound exception while using the same function for the second time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35996508/