所以我一直在试图弄清楚如何根据 find All 元素的数量来单击按钮 x 次。这意味着如果同一个类名找到 3 个元素,那么我们循环 3 次,这应该单击按钮 3 次。
我做了这样的事情:
(新更新,请查看底部的编辑帖子)
通常情况下element.all(by.className('btn btn-remove btn-outlined')).getText()
是 3 但可以更改为 6 和随机数,所以我的想法是先读取有多少btn btn-remove btn-outlined
在 HTML 中,然后单击找到的元素多次。因此,如果找到 3 个,则单击按钮 3 次。
然而现在的问题是找出有多少个元素。它也循环多次,以 text.length 的形式给出,但在循环内部似乎跳过了it由于某些原因而起作用,我不明白为什么
现在它确实找到了元素的长度,即 3,并且似乎循环了很多次,但它跳过执行 it 功能(这部分):
it('Click remove button - ' + i + '/' + text.length, function (done) {
browser.driver
.then(() => browser.executeScript("arguments[0].click();", element.all(by.className('btn btn-remove btn-outlined').first().getWebElement())));
.then(() => done());
});
it('Wait for fading button to be gone', function (done) {
setTimeout(function () {
done();
}, 1000);
});
我担心我可能做错了一些我没有意识到的事情。
我怎样才能找到 DOM 中有多少给定元素并循环多次+单击删除按钮?
编辑代码:
it('Click remove button', function (done) {
element.all(by.className('btn btn-remove btn-outlined')).getText().then(function (text) {
console.log(text.length) //returns 3
for (var i = 0; i < 4; i++) {
console.log(i); //Does print 0 1 2
it('Click remove button - ' + i + '/' + text.length, function (done) {
console.log("Remove button"); //Doesnt print
browser.driver
.then(() => browser.executeScript("arguments[0].click();", element.all(by.className('btn btn-remove btn-outlined').first().getWebElement())));
.then(() => done());
});
it('Wait for fading button to be gone', function (done) {
console.log("TIme out"); //Doesnt print
setTimeout(function () {
done();
}, 1000);
});
}
})
done();
});