Protractor clicking through an array of elements

别来无恙 提交于 2019-12-01 20:05:30

Managed to figure a workaround, although this doesn't feel quite right. Anyway, if anyone has better suggestion feel free to post :)

element.all(by.repeater('link in links')).map(
    function(link, index) {
        return {
            index: index,
            href: link.getAttribute('href')
        };
    })
    .then(function(links) {
        for (var i = links.length - 1; i >= 0; i--) {
        browser.get(links[i].href);
        // do some page specific stuff here.
    };
});

To avoid the "staleness" problem, you have to get an array of href resolved attribute values, which map()+then() can help you with (as you've already provided). You just don't need index and you can iterate over links starting from the first:

element.all(by.repeater('link in links')).map(function(link) {
    return link.getAttribute('href');
}).then(function(links) {
    for (var i = 0; i < links.length; i++) {
        browser.get(links[i]);
    }
});

Solution 01

public findSpecificElementAndClick(element: ElementArrayFinder,expected: number){
        let clickedIndex: number = -1;
        element.filter(function (elementItem, index) {
            clickedIndex++;
            if(index === (expected-1)){
                element.get(clickedIndex).click();
                return true;
            }
        }).then(function (bool) {

        }).catch(function (err) {
            throw new FrameworkException('Ooops ! Error... '+err.message);
        });
    }

Solution 02
public findTextAndClick(element: ElementArrayFinder,expected: string) {
        let clickedIndex: number = -1;
        let status :boolean = false;
        element.filter(function (elementItem, index) {
            return elementItem.getText().then(function (text) {
                if(text === expected) {
                    clickedIndex = index;
                    status = true;
                    return true;
                }
            });
        }).then(function (bool) {
            if(!status){
                throw new ApplicationException("Ooops ! Couldn't found "+expected+" Record ...");
            }else{
                element.get(clickedIndex).click();
            }
        }).catch(function (err) {
            throw new FrameworkException('Ooops ! Error... '+err.message);
        });
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!