根据 getText() 的文本从量角器中的 ElementArrayFinder 中获取特定元素 [英] Get a specific element from an ElementArrayFinder in protractor based on the text of getText()

查看:64
本文介绍了根据 getText() 的文本从量角器中的 ElementArrayFinder 中获取特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据所需元素的文本从 ElementArrayFinder 中获取特定的 ElementFinder.

I'm trying to get the specific ElementFinder from an ElementArrayFinder based on the text of the desired element.

例如,假设我有一个 Angular2 应用程序提供的以下 HTML 片段:

For example, suppose I have the following HTML snippet provided by an Angular2 app:

<ul id="names">
  <li><span>Adam</span> <span class="ids">ID: 1</span></li>
  <li><span>Becky</span> <span class="ids">ID: 2</span></li>
  <li><span>Chris</span> <span class="ids">ID: 3</span></li>
</ul>

我想使用名称Becky"选择第二个 li.所以我有以下功能:

And I want to select the second li using the name "Becky". So I have the following function:

/**
 * Get a Card object that contains the ElementFinder of a specific <li> 
 * @param desiredName {string} The name to search on the row
 * @returns {Card} A Card object with a "verifyID" method.
 */
getCardByName(desiredName) {
    return $$('#names li').map(function(el) {
        return el.$('span:first-child').getText().then(function(name) {
            console.log("getCardByName: got the card for " + name);
            if (name === desiredName) {
                console.log("getCardByName: returning card");
                return new Card(el); // el should be pointing to an <li> element
            }
        });
    });
}

然后我通过以下方式调用该函数:

I then call that function by doing:

it('test Becky', function(done) {
    getCardByName("Becky").then(function(card) {
        console.log("SPEC: testing card");
        card.verifyId("ID: 2");
        done();
    })
    .catch(function(err) { console.log(err); });
});

我得到的输出是,我等了很长时间然后看到:

The output that I'm getting is, I wait a long time and then see:

getCardByName: got the card for Adam
getCardByName: got the card for Becky
getCardByName: returning card

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

使用我当前的解决方案,我不断遇到这里描述的问题:https://github.com/angular/protractor/issues/2227

With my current solution, I keep running into the issue described here: https://github.com/angular/protractor/issues/2227

我不确定我是做错了什么还是可以改进我的代码,或者我是否真的遇到了上面链接的错误报告中描述的问题.

I am uncertain as to whether I am doing something wrong or can improve my code, or if I really am running up against the issue described in the bug report linked above.

就其价值而言,假设 Card 对象如下所示:

For what it's worth, assume the Card object looks like:

function Card(containerEl) {
    this.containerEl = containerEl;
}

Card.prototype = {
    constructor: Card,
    verifyId: function(expectedId) {
        var el = this.containerEl.$('span.ids');
        expect(el.getText()).toEqual(expectedId);
    }
}

推荐答案

你可以使用filter()函数来完成任务

you can use filter() function to do the task

 var rows = element(by.id('names'));

 var becky = rows.all(by.tagName('li')).filter(function (elem) {
            return elem.getText().then(function (val) {
                return val === 'Becky'
            });
        }).first();

这篇关于根据 getText() 的文本从量角器中的 ElementArrayFinder 中获取特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆