Protractor 使用element.all(by.binding)时索引超出范围
用户流:搜索案例>>搜索该案例中的项目>>预期返回的结果匹配数值Protractor 使用element.all(by.binding)时索引超出范围,protractor,Protractor,用户流:搜索案例>>搜索该案例中的项目>>预期返回的结果匹配数值 describe('Search', function () { beforeEach(function () { loginPage.signIn(); loginPage.login(username, password); }); afterEach(function () { homePage.logOut(); }); it('alecxes suggestion cod
describe('Search', function () {
beforeEach(function () {
loginPage.signIn();
loginPage.login(username, password);
});
afterEach(function () {
homePage.logOut();
});
it('alecxes suggestion code', function () {
var presenceOfAll = function(elementArrayFinder) {
return elementArrayFinder.count().then(function (count) {
return count > 0;
});
};
homePage.searchForCase(test_case);
filterSearchMenu.searchWithinCase(search_argument);
var hits = element.all(by.binding('response.hits.total'));
browser.wait(presenceOfAll(hits), TIMEOUT);
expect(element.all(by.binding('response.hits.total')).count()).toEqual(4);
});
});
该失败,预期为4,但为0
describe('Search', function () {
searchResultTotal = element(by.binding('response.hits.total'));
beforeEach(function () {
loginPage.signIn();
loginPage.login(username, password);
});
afterEach(function () {
homePage.logOut();
});
it('should be able to search', function () {
homePage.searchForCase(test_case);
filterSearchMenu.searchWithinCase(search_argument);
browser.wait(EC.visibilityOf(searchResultTotal), TIMEOUT).then(function () {
expect(element.all(by.binding('response.hits.total')).count()).toEqual(4);
});
});
});
此有效,但返回的警告是,通过.binding('response.hits.total')为定位器找到了多个元素。
describe('Search', function () {
beforeEach(function () {
loginPage.signIn();
loginPage.login(username, password);
});
afterEach(function () {
homePage.logOut();
});
it('should be able to search', function () {
homePage.searchForCase(test_case);
filterSearchMenu.searchWithinCase(search_argument);
browser.wait(EC.visibilityOf(element.all(by.binding('response.hits.total')).first()), TIMEOUT).then(function () {
expect(element.all(by.binding('response.hits.total')).count()).toEqual(4);
});
});
});
此失败并将索引抛出边界
describe('Search', function () {
beforeEach(function () {
loginPage.signIn();
loginPage.login(username, password);
});
afterEach(function () {
homePage.logOut();
});
it('should be able to search', function () {
homePage.searchForCase(test_case);
filterSearchMenu.searchWithinCase(search_argument);
browser.wait(EC.visibilityOf(element.all(by.binding('response.hits.total')).first()), TIMEOUT).then(function () {
expect(element.all(by.binding('response.hits.total')).count()).toEqual(4);
});
});
});
第二组眼睛和任何帮助都将不胜感激。我认为
的可视性不喜欢多个元素。尝试:
browser.wait(EC.visibilityOf(element.all(by.binding('response.hits.total')).first()), TIMEOUT);
您还可以使用自定义预期条件来解决此问题,该条件将检查是否有超过0个元素与找到的定位器匹配:
var presenceOfAll = function(elementArrayFinder) {
return elementArrayFinder.count().then(function (count) {
return count > 0;
});
};
var hits = element.all(by.binding('response.hits.total'));
browser.wait(presenceOfAll(hits), TIMEOUT);
expect(element.all(by.binding('response.hits.total')).count()).toEqual(4);
如果我没有记错的话,就不能调用单个元素的.first()
,但是我尝试了,它给了我预期的错误元素(…)。first不是一个函数。需要关闭元素。所有都需要关闭,但预期条件不喜欢这样。当我尝试执行browser.wait(EC.visibilityOf(element.all)(by.binding('response.hits.total')).first()、TIMEOUT)时
它仍然会给我一个超出范围的索引。还想添加一个注释,当前在我的一个页面对象中,我正在调用.first()
关闭元素。all
this.riskTypes=element.all(by.css('rga-filters>rga-risk facet>div>md复选框')代码>和它的用法类似于browser.wait(EC.visibilityOf(this.riskTypes.last()),TIMEOUT)代码>。唯一的区别是它是通过css查找元素。我不确定是不是装订把它弄掉了。呜呜。。。很抱歉我的意思是元素。全部。是的,装订可能是个问题?看起来这应该行得通。我想,但出于某种原因,它不行。不确定这是一个bug还是我做错了。我要试试亚历克斯的建议。我会报告我的发现,但我确实同意我最初的方式“应该”起作用。我继续将所有函数的这种存在放入规范中:it('should to search',function(){homePage.searchForCase('KJ_000269_1');filterseachmenu.searchWithinCase('Serum');var searchResult=element.all(by.binding('response.hits.total');browser.wait(presenceOfAll(searchResult),TIMEOUT);searchResult.then(function(results){expect(results.length.toBe(4);});});
失败,结果是“Expected 0 to be 4.”。我不明白为什么这些东西没有等待。非常令人沮丧。抱歉,我更新了我的评论:)我有很多话要说,我想确定这是清楚的。代码的格式很奇怪,如果我需要发布另一条注释以使其更清晰,请告诉我。@Sevfuria好:)但不确定这怎么可能。等待将确保至少找到1个元素。如果您在等待之后立即执行expect(searchResult.count()).toEqual(4)
,该怎么办?谢谢。在等待失败后立即添加expect,并显示“Expected 0将是4”。仍然如此。当我添加一个浏览器时,请在浏览器之前立即休眠(1000)
。等待它通过。没有意义。@Sevfuria如果您在等待后“重新填充”元素,如答案中所示,该怎么办?谢谢