根据 getText() 的文本从 Protractor 中的 ElementArrayFinder 获取特定元素

2024-04-27

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

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

<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”选择第二个李。所以我有以下功能:

/**
 * 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
            }
        });
    });
}

然后我通过执行以下操作来调用该函数:

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); });
});

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

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 https://github.com/angular/protractor/issues/2227

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

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

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() 函数来完成任务

 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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

根据 getText() 的文本从 Protractor 中的 ElementArrayFinder 获取特定元素 的相关文章

随机推荐