gpt4 book ai didi

javascript - 无法断言数组列表中存在关键字

转载 作者:太空宇宙 更新时间:2023-11-04 15:37:00 35 4
gpt4 key购买 nike

我正在尝试获取数组列表的计数,然后尝试断言数组值中是否存在关键字。下面是我的代码,有问题;

describe('My Test', function() {

it('Test starts', function() {

browser.ignoreSynchronization = true;
browser.get('https://www.w3schools.com/angular/');

browser.sleep(5000).then(function(){});
var results = element.all(by.css(".sidesection>p>a"));

var results_count=results.count().then(function(counting){
console.log("There are total "+counting+" lines");
return counting;
})
results_count.then (function(count){
console.log("There are totalx "+count+" lines");

for (var iterate=1;iterate<count;iterate++){

results.get(iterate).getText().then(function(text){
console.log("The text in Relationship Type node line "+iterate+" is ---"+text);
expect(text.indexOf('Navigation')!=-1).toBeTruthy();
})
}
})
})

})

输出:

There are total 19 lines
There are totalx 19 lines
The text in Relationship Type node line 19 is ---Dropdowns
The text in Relationship Type node line 19 is ---Accordions
The text in Relationship Type node line 19 is ---Convert Weights
The text in Relationship Type node line 19 is ---Animated Buttons
The text in Relationship Type node line 19 is ---Side Navigation
The text in Relationship Type node line 19 is ---Top Navigation
The text in Relationship Type node line 19 is ---JS Animations
The text in Relationship Type node line 19 is ---Modal Boxes
The text in Relationship Type node line 19 is ---Progress Bars
The text in Relationship Type node line 19 is ---Parallax
The text in Relationship Type node line 19 is ---Login Form
The text in Relationship Type node line 19 is ---HTML Includes
The text in Relationship Type node line 19 is ---Google Maps
The text in Relationship Type node line 19 is ---Loaders
The text in Relationship Type node line 19 is ---Tooltips
The text in Relationship Type node line 19 is ---Slideshow
The text in Relationship Type node line 19 is ---Filter List
The text in Relationship Type node line 19 is ---Sort List
[31mF[0m

Failures:
1) My Test Test starts
Message:
[31m Expected false to be truthy.

我遇到了 2 个疑问:

1.) 为什么我在所有值列表中硬编码数字 19,我希望输出计数像 1,2,3,4... 一样迭代

2.) 尽管关键字存在于某些数组值中,但为什么我的 Expect 语句失败。

有人可以纠正我理解并解决上述两个问题吗?

最佳答案

关于(1)我不太肯定,但我绝对可以回答(2)有助于改进你的代码

1) 这看起来像是经典的 for 循环作用域问题,其中循环在调用时已完成......请参阅 this question以供引用。尚不清楚这如何与 Protractor 和控制流执行一起发挥作用。

2)您的期望失败,因为它检查每一行,您是说每行文本的条件与“导航”相比将评估为真实。对于其中很多(即幻灯片、工具提示、加载器等)来说,这将失败。您需要更好的断言,例如您可以一一对应链接:expect(results.get(i).getText()).toEqual('Help'),或者您可以一系列导航项并期望它们匹配等等......但你肯定需要一个更好的断言。这个测试到底想做什么?

无论哪种方式,这里都是对您的代码的一些一般帮助:

  1. 除非您正在做一些非常具体的事情,否则您实际上并不需要在 Protractor 中使用 for 循环。您只需使用 each 即可迭代 ElementArrayFinder。
  2. 这更具语义性,但是您可以使用从 Promise 返回的值,而不是将其分配给变量,您的某些代码有些多余。如果你像这样实现的话,你可以省略关于 results_count 的部分:

    results.count().then(function(counting){
    console.log("There are total "+counting+" lines"); // logs 19
    return counting;
    }).then(function (count) {
    console.log(count); // logs 19
    for(var i = 0; i<count; i++) {

    }
    })

但是,for 循环在 Protractor 中并不是真正必要的。相反,您可以只使用 each,这使您的代码更加简洁,并且还消除了您遇到的循环闭合问题:

    var results = element.all(by.css(".sidesection>p>a"));
results.each(function (elem, index) {
return elem.getText().then(function (text) {
console.log('Element at index ' + index + ' has text ' + text);
// this still fails because it's not a good assertion
expect(text.indexOf('Navigation')!=-1).toBeTruthy();
});
});

关于javascript - 无法断言数组列表中存在关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44260712/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com