简体   繁体   English

量角器单击一系列元素

[英]Protractor clicking through an array of elements

I'm quite new to e2e testing and in using protractor / jasmine framework. 我对e2e测试和使用量角器/茉莉框架很陌生。 I know how to get an array of elements and also how to click on an anchor. 我知道如何获取元素数组以及如何单击锚点。 But how would / is it even possible to click through a list of anchors returned by a element selector / repeater? 但是/如何甚至可能单击元素选择器/转发器返回的锚列表?

I've been trying various ways, but as an example (latest one which hasn't been deleted lol) this is what I got: 我一直在尝试各种方法,但是作为一个示例(最新的示例,尚未删除),这就是我得到的:

element.all(by.repeater('link in links')).then(function(links) {
    links.forEach(function(link) {

        link.click().then(function() {
            console.log('callback for click ');

        });
    });
});

This appears to take the first element and click through, however come the next iteration it hangs (I can see why, but struggling to figure a way to resolve - is this some kind of promise & resolve factor i need to take into account?) 这似乎需要第一个元素并单击,但是会挂起它的下一个迭代(我可以理解为什么,但是努力寻找一种解决方法的方法-我是否需要考虑这种承诺和解决因素?)

The error coming back is 返回的错误是

Failed: stale element reference: element is not attached to the page document 失败:旧元素引用:元素未附加到页面文档

Any guidance / link to help would be appreciated - googling hasn't returned anything of note to me so far... 任何帮助的指导/链接将不胜感激-谷歌搜索到目前为止尚未向我返回任何注意事项...

Thanks in advance! 提前致谢!

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). 为了避免“陈旧性”问题,您必须获得由href解析的属性值组成的数组, map() + then()可以帮助您解决这些问题(如前所述)。 You just don't need index and you can iterate over links starting from the first: 您只是不需要index ,就可以从第一个开始迭代链接:

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 解决方案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 解决方案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); }); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM