問題描述
所以我一直試圖弄清楚如何根據 find All 元素的數量單擊按鈕 x 次.這意味著如果有 3 個元素由相同的類名找到,那么我們循環 3 次,應該點擊按鈕 3 次.
So I have been trying to figure out on how to click a button x times depending on how many find All elements are. Meaning if there is 3 elements that is found by the same classname then we loop 3 times which should click the button 3 times.
我做過這樣的事情:
(新的更新,檢查底部的編輯帖子)
通常 element.all(by.className('btn btn-remove btn-outlined')).getText()
是 3 但可以更改為 6 和隨機數所以我的想法是首先閱讀 HTML 中有多少 btn btn-remove btn-outlined
并單擊多次找到的元素.因此,如果找到 3 個,則單擊該按鈕 3 次.
Usually the element.all(by.className('btn btn-remove btn-outlined')).getText()
is 3 but can be changed to 6 and random numbers so my idea was to read first how many btn btn-remove btn-outlined
are in the HTML and click that many times of found elements. So if 3 found then click the button 3 times.
但是現在的問題是它要找到有多少元素.它也循環了很多次,以 text.length 給出,但在循環內部,由于某些原因,它似乎跳過了 it 函數,我不知道為什么
However the problem right now is that it is finding how many elements there are. It is also looping that many times that is given as text.length but inside the loop it seems to skip the it functions for some reasons and I cant figure out why
現在它確實找到了 3 個元素的長度,它似乎循環了很多次,但它跳過了它的功能(這部分):
Right now it does find the length of the elements which is 3 and it seems to loop that many times but it skips to do the it functions (This part):
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);
});
我很害怕我可能做錯了我不知道的事情.
and I am afarid that I might have done something wrong that I am not aware of.
如何找到 DOM 中有多少給定元素并循環多次 + 單擊刪除按鈕?
How can I find how many given elements are in the DOM and loop that many times + click the remove button?
編輯代碼:
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();
});
推薦答案
你的it
里面的for循環沒有執行的原因是那些it
在運行時動態生成的,沒有受到測試框架 Jasmine、Mocha 的尊重.
The reason of your it
inside for loop not executed is those it
generated dynamically in run time and did not respected by the test framework, Jasmine, Mocha.
據我所知,Jasmine 在執行測試腳本文件之前需要加載和解析靜態 it
,在加載和解析階段生成的動態 it
將被忽略.因此,您需要刪除動態 it
.
As I learned, Jasmine need to load and parse the static it
in test script files before executing them, dynamic it
generated behind the load and parse stage will be ignored. Thus you need remove dynamic it
.
試試下面的代碼
it('Click remove button', function (done) {
let allBtns = element.all(by.className('btn btn-remove btn-outlined'));
allBtns.count()
.then(function (cnt) {
console.log('Find buttons:', cnt)
for (let i = 0; i < cnt; i++) { // important to use let but var here.
console.log('Remove button - ' + i + '/' + cnt);
browser.executeScript("arguments[0].click();", allBtns.get(i).getWebElement())
browser.sleep(1000) // sleep 1s
}
})
.then(()=>{
done();
})
});
這篇關于量角器 - 查找所有元素和找到的元素的循環長度并單擊按鈕的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!