Cannot loop all rows in cucumber.js examples table

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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?

New contributor

Shirley is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật