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

后端 未结 1 717
我在风中等你
我在风中等你 2021-01-28 06:34

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 classn

相关标签:
1条回答
  • 2021-01-28 07:34

    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();
        })
    
    });
    
    0 讨论(0)
提交回复
热议问题