Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

javascript - 无法遍历ElementArrayFinder(Unable to iterate over an ElementArrayFinder)

I'm a beginner writing e2e Javascript tests using Protractor.(我是使用Protractor编写e2e Javascript测试的初学者。)

I have been trying to identify an array of elements that have a text property associated with them.(我一直在尝试确定具有与之关联的text属性的元素数组。) I'm able to target them using(我可以使用)

groupedNodeNumbers : ElementArrayFinder;

this.groupedNodeNumbers = this.element.all(by.css(".side-label-background + .side-label"));

This is supposed to give me an array of elements on which I can call getText() to extract the value associated with each of them.(这应该给了我一个元素数组,可以在上面调用getText()来提取与每个元素相关的值。)

validateCountGraphNodes = async() => {
        let count = 0;
        console.log('Inside')
        this.groupedNodeNumbers.each((groupedNodeElement) => {
            groupedNodeElement.getText().then((num) => {
            console.log('Inside loop')
                count += parseInt(num, 10);
                console.log(count);
            });
        });
}`

I am able to log ('Inside') but not ('Inside Loop') and hence my function fails to retrieve the text associated with each element.(我能够登录('Inside')但不能登录('Inside Loop'),因此我的函数无法检索与每个元素关联的文本。)

Could someone please point where I'm going wrong here?(有人可以指出我在哪里出问题了吗?)

  ask by Jonesh Sharma translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since getText() applied returns string instead of an array of strings (existing issue), you can try the following:(由于应用的getText()返回的是字符串而不是字符串数组(存在问题),因此您可以尝试以下操作:)

const cssSelector = ".side-label-background + .side-label";


$$(cssSelector).getText().then(textArrNotArr => {
    for(let i = 0; i< textArrNotArr.lenght; i++) {
        console.log('arr[i] = ', textArrNotArr[i]);
    }
}); 

or(要么)

$$(cssSelector).count().then(elFinderArray => {
    elFinderArray.forEach(elFinder => {
        elFinder.getText().then((txt, index) => {
            console.log(index);
        });
    });
});

note: $$('cssSelector') can be used instead of element.all(by.css('cssSelector'))(注意:可以使用$$('cssSelector')代替element.all(by.css('cssSelector')))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...