jeudi 17 août 2017

nightwatch js help using conditional inside a loop

First of all, let me clarify I am new with javascript and nightwatch js so probably I am making a lot of mistakes even in the question itself.

Here is my problem. I am trying to automate an online test which has between 4 and 5 questions maximum which are randomly generated. Each question goes on a new page load so basically you answer a question, submit it and then get presented the next one.

The answers can be in different types, some of them are YES/NO buttons that you click, some of them are multiple choice that you also click, some of them are typing data into a text field which then you have to submit by pressing the Enter key on the keyboard. Luckily the locators used for each one of these types of answers are always the same (not dynamically generated). For example. Yes/No questions, the Yes answer will always be .answerYes or if it's a text field will always be #answerBox.

Basically i need to go through the 4 or 5 questions and it doesn't matter if I answer them correctly.

So for this I am thinking about using a for loop where I check if I get one of these elements I already identified and if so, I do the action and continue to the next page until the questionnaire is finished.

I have in mind something like the example below. The problem is I don't think this will work since javascript is asynchronous - which I don't fully understand yet, So it probably is not as simple as I am thinking.

for (i = 0; i < 10; i++) {

if (client.waitForElementVisible('.element1',1000)) {

    client.click('.element1');  //This would select first answer 
    client.click('nextButton'); //This would continue to the next question

}
else if (client.waitForElementVisible('.element2',1000)) {

    client.click('.element2');  //This would select first answer 
    client.click('nextButton'); //This would continue to the next question

}
else if (client.waitForElementVisible('.element3',1000)) {

    client.click('.element3');  //This would select first answer 
    client.click('nextButton'); //This would continue to the next question

}
else if (client.waitForElementVisible('.element4',1000)) {

    client.click('.element4');  //This would select first answer 
    client.click('nextButton'); //This would continue to the next question
}
else if (client.waitForElementVisible('.element5',1000)) { //element 5 would be the confirmation that test is over

    client.click('.element5'); //this would click the OK button when notified test is done
    leaveTheLoop(); //Not sure what would be the command to leave the loop  
}

}

Any advice on how to achieve this will be appreciated. I was looking also at the Switch Statement for javascript but I don't think I can write in the expression something that would validate if any of the elements I am expecting will be present all under one expression.

Hope this makes sense. Thanks a lot in advance.

Aucun commentaire:

Enregistrer un commentaire