OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Cannot loop all rows in cucumber.js examples table

  • Thread starter Thread starter Shirley
  • Start date Start date
S

Shirley

Guest
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
         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 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
           return 'pending';
         });

Can someone please give me some guidance?

<p>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</p>
<pre><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></pre>
<p>Here is my condition function and step definition file</p>
<pre><code>//Condition function
async function answerQuestion(questionNum, ...answers) {
await World.driver.sleep(1500);
for (let i = 0; i < answers.length; i++) {
const ans = answers;
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></pre>
<p>The Error Output is</p>
<pre><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></pre>
<p>Can someone please give me some guidance?</p>
 

Latest posts

Top