1

I am creating a Protractor script to test my quiz game that puts up random questions and answers, so I need my script to be able to figure out which answer is correct, so I can click it.

I can't figure out how to get the values directly from the model, instead of elements, since correct/incorrect is not exposed as an element on the page.

The model provides the answers in Choice in Question.Choices, and I need to find the Choice where Choice.IsCorrect is true. How do I get access to this value?

I wouldn't use the element() function, right?

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

1 Answer 1

1

The idea is to use element.all() in conjunction with filter() and evaluate():

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

As a result correctChoices would contain elements where Choice.IsCorrect is truthy.


If you need to get an array of values for the correct choices, use map() and getAttribute():

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

or, if you need texts make use of getText():

correctChoices.map(function (elm) { return elm.getText(); }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.