量角器-如何从中继器而不是元素中获得实际值

Protractor - How to get actual values from the Repeater, not the Elements?

本文关键字:元素 中继器 量角器      更新时间:2023-09-26

我正在创建一个Protractor脚本来测试我的智力竞赛游戏,该游戏会随机提出问题和答案,所以我需要我的脚本来找出哪个答案是正确的,这样我就可以点击它。

我不知道如何直接从模型而不是元素中获取值,因为correct/incorrect不会在页面上显示为元素。

该模型在Choice in Question.Choices中提供了答案,我需要找到Choice.IsCorrect为真的Choice。如何获取此值?

我不会使用element()函数,对吧?

element(by.repeater('Choice in Question.Choices').row(0).column('Choice.IsCorrect'))

我们的想法是将element.all()filter()evaluate():结合使用

var correctChoices = element.all(by.repeater('Choice in Question.Choices')).filter(function (elm) {
    return elm.evaluate('Choice.IsCorrect').then(function (value) {
        return value;
    });
});

结果CCD_ 7将包含其中CCD_ 8是真的元素。


如果需要获得正确选择的值数组,请使用map()getAttribute():

correctChoices.map(function (elm) {
    return elm.getAttribute('value');
});

或者,如果您需要文本,请使用getText():

correctChoices.map(function (elm) {
    return elm.getText();
});