我正在创建一个 Protractor 脚本来测试我的问答游戏,该游戏会提供随机问题和答案,因此我需要我的脚本能够确定哪个答案是正确的,以便我可以点击它。
我不知道如何直接从模型而不是元素获取值,因为正确/不正确不会作为页面上的元素公开。
该模型在 Question.Choices 中的 Choice
中提供答案,我需要找到 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;
});
});
因此 correctChoices
将包含 Choice.IsCorrect
为真的元素。
如果您需要获取正确选择的值数组,请使用 map()
和 getAttribute()
:
correctChoices.map(function (elm) {
return elm.getAttribute('value');
});
或者,如果您需要文本,请使用 getText()
:
correctChoices.map(function (elm) {
return elm.getText();
});
关于javascript - Protractor - 如何从中继器而不是元素获取实际值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29174754/