In my automated test, I intend to automatically answer quiz and click the “next” button to proceed to the next page after each question is done in each page (questionNum). However, only the first page runs correctly and navigates to the next page, but on the second page, it doesn’t perform any operations. I thought that within the “When” block, each row in the examples would be automatically read without needing to write an additional loop. Addtionally, some data in the example is N/A, I also wrote a condition to filter them out.
Here is my Feature file
<code>When User answer question <questionNum> by <answer1> and <answer2> and <answer3> and <answer4> and <answer5> and click Next button to the next question
| questionNum | answer1 | answer2 | answer3 | answer4 | answer5 |
| 1 | 1 | 2 | 3 | 4 | 5 |
| 2 | 1 | 2 | N/A | N/A | N/A |
<code>When User answer question <questionNum> by <answer1> and <answer2> and <answer3> and <answer4> and <answer5> and click Next button to the next question
Examples:
| questionNum | answer1 | answer2 | answer3 | answer4 | answer5 |
| 1 | 1 | 2 | 3 | 4 | 5 |
| 2 | 1 | 2 | N/A | N/A | N/A |
</code>
When User answer question <questionNum> by <answer1> and <answer2> and <answer3> and <answer4> and <answer5> and click Next button to the next question
Examples:
| questionNum | answer1 | answer2 | answer3 | answer4 | answer5 |
| 1 | 1 | 2 | 3 | 4 | 5 |
| 2 | 1 | 2 | N/A | N/A | N/A |
Here is my condition function and step definition file
<code>//Condition function
async function answerQuestion(questionNum, ...answers) {
await World.driver.sleep(1500);
for (let i = 0; i < answers.length; i++) {
const xpath=`(//input[@name='row-radio-buttons-group'])[${6*i + ans}]`;
const element = await World.driver.findElement(By.xpath(xpath));
await World.driver.executeScript("arguments[0].scrollIntoView();", element);
// Wait for the element to be clickable
await World.driver.wait(until.elementLocated(By.xpath(xpath)), 15000); // Wait for element to be located
await World.driver.wait(async () => {
}, 10000); // Wait for element to be clickable and click on it
await World.driver.sleep(5000);
When('User answer question {int} by {int} and {int} and {int} and {int} and {int} and click Next button to the next question', async (questionNum, answer1, answer2, answer3, answer4, answer5) => {
await answerQuestion(questionNum, answer1, answer2, answer3, answer4, answer5);
const nextButtonXpath = `//div[@id='root']/div/main/div[2]/div/div[2]/div/div/div[3]/div/button${questionNum === 1 ? "[2]" : "[3]"}`;
await World.driver.findElement(By.xpath(nextButtonXpath),5000).click();
console.error('Error occurred while clicking the Next button', error.message);
<code>//Condition function
async function answerQuestion(questionNum, ...answers) {
await World.driver.sleep(1500);
for (let i = 0; i < answers.length; i++) {
const ans = answers[i];
if (ans !== 'N/A') {
const xpath=`(//input[@name='row-radio-buttons-group'])[${6*i + ans}]`;
const element = await World.driver.findElement(By.xpath(xpath));
// Scroll to the element
await World.driver.executeScript("arguments[0].scrollIntoView();", element);
// Wait for the element to be clickable
await World.driver.wait(until.elementLocated(By.xpath(xpath)), 15000); // Wait for element to be located
await World.driver.wait(async () => {
try {
await element.click();
return true;
} catch (e) {
return false;
}
}, 10000); // Wait for element to be clickable and click on it
}
}
await World.driver.sleep(5000);
//Step_Definition
When('User answer question {int} by {int} and {int} and {int} and {int} and {int} and click Next button to the next question', async (questionNum, answer1, answer2, answer3, answer4, answer5) => {
await answerQuestion(questionNum, answer1, answer2, answer3, answer4, answer5);
try{
const nextButtonXpath = `//div[@id='root']/div/main/div[2]/div/div[2]/div/div/div[3]/div/button${questionNum === 1 ? "[2]" : "[3]"}`;
await World.driver.findElement(By.xpath(nextButtonXpath),5000).click();
} catch(error){
console.error('Error occurred while clicking the Next button', error.message);
throw error;
}
});
</code>
//Condition function
async function answerQuestion(questionNum, ...answers) {
await World.driver.sleep(1500);
for (let i = 0; i < answers.length; i++) {
const ans = answers[i];
if (ans !== 'N/A') {
const xpath=`(//input[@name='row-radio-buttons-group'])[${6*i + ans}]`;
const element = await World.driver.findElement(By.xpath(xpath));
// Scroll to the element
await World.driver.executeScript("arguments[0].scrollIntoView();", element);
// Wait for the element to be clickable
await World.driver.wait(until.elementLocated(By.xpath(xpath)), 15000); // Wait for element to be located
await World.driver.wait(async () => {
try {
await element.click();
return true;
} catch (e) {
return false;
}
}, 10000); // Wait for element to be clickable and click on it
}
}
await World.driver.sleep(5000);
//Step_Definition
When('User answer question {int} by {int} and {int} and {int} and {int} and {int} and click Next button to the next question', async (questionNum, answer1, answer2, answer3, answer4, answer5) => {
await answerQuestion(questionNum, answer1, answer2, answer3, answer4, answer5);
try{
const nextButtonXpath = `//div[@id='root']/div/main/div[2]/div/div[2]/div/div/div[3]/div/button${questionNum === 1 ? "[2]" : "[3]"}`;
await World.driver.findElement(By.xpath(nextButtonXpath),5000).click();
} catch(error){
console.error('Error occurred while clicking the Next button', error.message);
throw error;
}
});
The Error Output is
<code>? When User answer question 2 by 1 and 2 and N/A and N/A and N/A and click Next button to the next question
Undefined. Implement with the following snippet:
When('User answer question {int} by {int} and {int} and N\/A and N\/A and N\/A and click Next button to the next question', function (int, int2, int3) {
// When('User answer question {int} by {int} and {float} and N\/A and N\/A and N\/A and click Next button to the next question', function (int, int2, float) {
// When('User answer question {int} by {float} and {int} and N\/A and N\/A and N\/A and click Next button to the next question', function (int, float, int2) {
// When('User answer question {int} by {float} and {float} and N\/A and N\/A and N\/A and click Next button to the next question', function (int, float, float2) {
// When('User answer question {float} by {int} and {int} and N\/A and N\/A and N\/A and click Next button to the next question', function (float, int, int2) {
// When('User answer question {float} by {int} and {float} and N\/A and N\/A and N\/A and click Next button to the next question', function (float, int, float2) {
// When('User answer question {float} by {float} and {int} and N\/A and N\/A and N\/A and click Next button to the next question', function (float, float2, int) {
// When('User answer question {float} by {float} and {float} and N\/A and N\/A and N\/A and click Next button to the next question', function (float, float2, float3) {
// Write code here that turns the phrase above into concrete actions
<code>? When User answer question 2 by 1 and 2 and N/A and N/A and N/A and click Next button to the next question
Undefined. Implement with the following snippet:
When('User answer question {int} by {int} and {int} and N\/A and N\/A and N\/A and click Next button to the next question', function (int, int2, int3) {
// When('User answer question {int} by {int} and {float} and N\/A and N\/A and N\/A and click Next button to the next question', function (int, int2, float) {
// When('User answer question {int} by {float} and {int} and N\/A and N\/A and N\/A and click Next button to the next question', function (int, float, int2) {
// When('User answer question {int} by {float} and {float} and N\/A and N\/A and N\/A and click Next button to the next question', function (int, float, float2) {
// When('User answer question {float} by {int} and {int} and N\/A and N\/A and N\/A and click Next button to the next question', function (float, int, int2) {
// When('User answer question {float} by {int} and {float} and N\/A and N\/A and N\/A and click Next button to the next question', function (float, int, float2) {
// When('User answer question {float} by {float} and {int} and N\/A and N\/A and N\/A and click Next button to the next question', function (float, float2, int) {
// When('User answer question {float} by {float} and {float} and N\/A and N\/A and N\/A and click Next button to the next question', function (float, float2, float3) {
// Write code here that turns the phrase above into concrete actions
return 'pending';
});
</code>
? When User answer question 2 by 1 and 2 and N/A and N/A and N/A and click Next button to the next question
Undefined. Implement with the following snippet:
When('User answer question {int} by {int} and {int} and N\/A and N\/A and N\/A and click Next button to the next question', function (int, int2, int3) {
// When('User answer question {int} by {int} and {float} and N\/A and N\/A and N\/A and click Next button to the next question', function (int, int2, float) {
// When('User answer question {int} by {float} and {int} and N\/A and N\/A and N\/A and click Next button to the next question', function (int, float, int2) {
// When('User answer question {int} by {float} and {float} and N\/A and N\/A and N\/A and click Next button to the next question', function (int, float, float2) {
// When('User answer question {float} by {int} and {int} and N\/A and N\/A and N\/A and click Next button to the next question', function (float, int, int2) {
// When('User answer question {float} by {int} and {float} and N\/A and N\/A and N\/A and click Next button to the next question', function (float, int, float2) {
// When('User answer question {float} by {float} and {int} and N\/A and N\/A and N\/A and click Next button to the next question', function (float, float2, int) {
// When('User answer question {float} by {float} and {float} and N\/A and N\/A and N\/A and click Next button to the next question', function (float, float2, float3) {
// Write code here that turns the phrase above into concrete actions
return 'pending';
});
Can someone please give me some guidance?