I have a scenario like, answering a multiple choice question. I need to answer the 1st question with 1st option, 2nd question with the 2nd option and so on.. in round robin fashion. Any suggestion, how can I perform using selenium Geb ?

I have written the code for selecting multiple choice and selecting randomly but now I need to select the answer in round robin fashion.

I'm using below code :

def answerQuestions(numQuestions, numAnswers, sectionName) {
        for(int i = 0; i < numQuestions; i++) {
            log.info(" ${bookingcode}\t"+sectionName+": "+ i)

//I need to select the 1st option for 1 st question instead random
            def index = rand.nextInt(numAnswers)
            waitFor {
                cover.displayed == false
                mcQuestions[index].value() != null

1 Answer 1


Here is the algorithm:

  1. For each question you should detect the total number of answers. Lets put it to variable numAnswers.
  2. Being on a particular question (let variable currQuestion hold the current Question number) you should take the value index which will be evaluated as: index = currQuestion % numAnswers (all indices start from 0)

Below is the example of how algorithm work:

#Qestion    totalAnswers    index
0           5               0
1           5               1
2           3               2
3           3               0
4           3               1
5           2               1 <-- **

** - the index will remain the same here. This is what you will probably have to rework in the algorithm. This happens because of variable number of answers for each question and will not be an issue if the number of answers will be constant. However I just give you a direction and it is up to you how to improve the approach.

  • Thanks, Alexy for beautiful explanation and I have been implemented the same. It's Working Fine. Commented Aug 20, 2018 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.