使用量角器搜索包含特定文本的中继器中的元素

我如何从包含特定文本的转发器中搜索元素?

我尝试了这样的事情:

element(by.repeater('item in array')).all(by.cssContainingText('.xyz','my item title')); // only gets the first element

我可以使用.thenelement.all之后自己搜索,但是我想知道它是否存在像cssContainingText这样简单的东西,但是对于中继器:

element(by.repeaterContainingText('item in array','my item title'))

或者像这样链接的元素:

element.all(by.repeater('item in array')).element(by.cssContainingText('.xyz','my item title'));

一个解决方案与过滤器(但非常慢)

element.all(by.repeater('item in array')).filter(function(elem){
    return elem.getText().then(function(text){
        return text.indexOf('my item title') > -1;
    });
}).then(function(filteredElements) {
    return filteredElements[0];
})

量角器允许添加定位器..我使用以下实现(纯JavaScript)成功地在20个测试左右的项目。

这是一个使用lodash和jquery的解决方案。 下面还有一个纯javascript。

https://gist.github.com/GuyMograbi/7a5f5e580bcf8d7da58a

by.addLocator('text',

/**
 *
 * @param {string} text - will be lowercased
 * @param {string} selector - to get list of children
 * @param {null|object} parent - protractor will provide this..
 */
function(text, selector, parent) {
    return _.filter($(parent || 'body').find(selector), function(e){
        return $(e).is(':visible') && $(e).text().toLowerCase().trim() === text.toLowerCase().trim();
    });
});

并与它一起使用

return $('table').all(by.text('my text', 'tr')).first().getText().then(function(text){...})

要么

return element(by.text('my text', 'tr')).getText().then(function(text){...})

不要忘记以下关于量角器和定位器

我花了2个小时打破了我的大脑为什么我的定位器不工作。 请记住以下内容:

  • 量角器'忽略'隐藏的元素,但是你的定位器默认不这样做。所以如果你写一个定制的定位器,为了使它在角度上按预期工作,你必须过滤掉隐藏的元素
  • 如果你需要调试,我发现它最有用的getOuterHtml().then..
  • 纯粹的JavaScript

    没有jQuery和lodash,这也是如此

    by.addLocator('text',
    
    /**
     *
     * @param text - will be lowercased
     * @param selector - to get list of children
     * @param parent - protractor will provide this..
     */
    function(text, selector, _parent) {
    
        return Array.prototype.filter.call( (_parent || document).querySelectorAll(selector), function(e){
            return e && !!(e.offsetWidth || e.offsetHeight || e.getClientRects().length) && e.textContent && e.textContent.toLowerCase().trim() === text.toLowerCase().trim();
    
        });
    });
    
  • isVisible部分最终是从jquery消息来源获取的。 请参阅以下网址的答案:https://stackoverflow.com/a/33456469/1068746
  • 重要提示 - 或 - 为什么这应该是正确的答案

    使用filter你会得到一个承诺。 使用这个解决方案你会得到一个元素 因此,不要使用getByText(..).then(function(e){ e.click() })您将获得getByText(...).click() - sweeet!

    限制和可能的增强

    在你的情况下,不能使用中继器定位器,这是一个令人失望的,除非你修改的位置,而不是texttextInRepeater ,然后'[ng-repeat=' + repeater + ']'在代码中加入。

    另一件你可能想要增强的功能非常有用。 有时候你想获得一个包含文本的元素,或者可能包含一个包含文本的子元素,但实际上你想要选择父元素。


    是的,你可以在量角器中链接元素。

    element(locator1).element(locator2);
    

    protractor-Locators.md


    我知道你可能不再需要这个答案,但是我会尽快更新以防其他人遇到同样的问题。 您可以使用

    element(by.linkText('.xyz'));
    
    链接地址: http://www.djcxy.com/p/83575.html

    上一篇: search an element in a repeater containing specific text using protractor

    下一篇: Efficiently get an element's visible area coordinates