Protractor - Find all elements and loop length of found elements and click button

北慕城南 提交于 2021-02-17 05:36:07

问题


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.

I have done something like this:

(New update, check edit post at the bottom)

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.

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

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.

How can I find how many given elements are in the DOM and loop that many times + click the remove button?

EDIT CODE:

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();
});

回答1:


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.

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.

Try below code

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();
    })

});


来源:https://stackoverflow.com/questions/60189623/protractor-find-all-elements-and-loop-length-of-found-elements-and-click-butto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!