Navigating to the next page when element is not found

I’ve created a script for testing inside of SAP Hybris with Cypress. Generally everything works fine, although I wanted to include a function that will basically navigate to the next page if the searched element is not found on the current page. Please take a look at the code:

const checkProductOnPage = (CATALOG_LABEL, retries = 0) => {
        const maxRetries = 5; // Adjust this based on the maximum number of pages you expect
    
        // Try to find the product on the current page
        return cy.contains('span.yw-coll-browser-hyperlink', CATALOG_LABEL, { timeout: 10000 }) // Adjust timeout as needed
            .then(productElement => {
                if (productElement.length) {
                    // If the product element is found
                    return cy.wrap(productElement).scrollIntoView().should('be.visible').click({ force: true });
                } else if (retries < maxRetries) {
                    // If the product element is not found and we haven't exceeded max retries
                    return cy.get('body').then(body => {
                        if (body.find('.z-paging-icon.z-icon-angle-right').length > 0) {
                            // Click on the pagination button and wait for the page to update
                            return cy.get('.z-paging-icon.z-icon-angle-right').eq(0).click({ force: true })
                                .wait(5000) // Adjust wait time as needed
                                .then(() => checkProductOnPage(CATALOG_LABEL, retries + 1)); // Recurse with increased retry count
                        } else {
                            throw new Error('Reached the last page and product not found');
                        }
                    });
                } else {
                    throw new Error('Product not found after maximum retries');
                }
            });
    };

    const checkProductReplacements = (productCode, replacements, isFirstSearch) => {
        cy.get('.yw-fulltextsearch-search-button').eq(0).click();
    
        // Determine the correct input element to type into based on whether it's the first search
        const inputIndex = isFirstSearch ? 3 : 5;
        cy.get('.z-bandbox-input').eq(inputIndex).type(productCode, { force: true }).wait(300);
    
        cy.get('.yw-fulltextsearch-search-button').eq(0).click().wait(3000);
    
        return checkProductOnPage(CATALOG_LABEL).then(() => {
            cy.wait(5000);
            cy.get('.yw-editorarea-tabbox-tabs-tab').contains("Categories").click().wait(300);
    
            // Use cy.then to properly chain the commands
            cy.then(() => {
                replacements.forEach(replacement => {
                    const label = LABELS[replacement.level];
                    cy.get('.z-listitem')
                        .contains(replacement.code)
                        .should('contain', label)
                        .parent()
                        .should('contain', CATALOG_LABEL);
                });
    
                cy.get('.yw-navigationhistory-back').eq(0).should('be.visible').click({ force: true });
                cy.get('button[title="Clear"]').eq(0).click().wait(1000);
            });
        });
    };

    it('should test the replacement matrix', () => {
        checkProductReplacements("G0000605", [
          { code: "G0000605", level: 'L1' },
          { code: "G0000592", level: 'L2' },
          { code: "G0000604", level: 'L1' }
        ], true);
        checkProductReplacements("G0000606", [
          { code: "G0000606", level: 'L1' },
          { code: "G0000592", level: 'L3' },
          { code: "G0000604", level: 'L2' }
        ], false);
        checkProductReplacements("G0000607", [
          { code: "G0000607", level: 'L1' },
          { code: "G0000605", level: 'L2' },
          { code: "G0000593", level: 'L3' }
        ], false);
    });
});

It successfully tested the first product (G0000605) while when it tried to do the same with the second product (G0000606) I’ve received this error:

Timed out retrying after 10000ms: Expected to find content: ‘Tunisia Product Catalog : Staged’ within the selector: ‘span.yw-coll-browser-hyperlink’ but never did.

cypress/e2e/New.cy.js:40:19

  38 |     
  39 |         // Try to find the product on the current page
> 40 |         return cy.contains('span.yw-coll-browser-hyperlink', CATALOG_LABEL, { timeout: 10000 }) 
     |                   ^
  41 |             .then(productElement => {
  42 |                 if (productElement.length) {
  43 |                     // If the product element 

It didn’t find the G0000606 product because it is located on the second page, so Cypress should have navigate to the second page, but instead it just dropped an error where it cannot find the requested product.

I believe that something should be modified in this part of the script:

const checkProductOnPage = (CATALOG_LABEL, retries = 0) => {
        const maxRetries = 5; // Adjust this based on the maximum number of pages you expect
    
        // Try to find the product on the current page
        return cy.contains('span.yw-coll-browser-hyperlink', CATALOG_LABEL, { timeout: 10000 }) // Adjust timeout as needed
            .then(productElement => {
                if (productElement.length) {
                    // If the product element is found
                    return cy.wrap(productElement).scrollIntoView().should('be.visible').click({ force: true });
                } else if (retries < maxRetries) {
                    // If the product element is not found and we haven't exceeded max retries
                    return cy.get('body').then(body => {
                        if (body.find('.z-paging-icon.z-icon-angle-right').length > 0) {
                            // Click on the pagination button and wait for the page to update
                            return cy.get('.z-paging-icon.z-icon-angle-right').eq(0).click({ force: true })
                                .wait(5000) // Adjust wait time as needed
                                .then(() => checkProductOnPage(CATALOG_LABEL, retries + 1)); // Recurse with increased retry count
                        } else {
                            throw new Error('Reached the last page and product not found');
                        }
                    });
                } else {
                    throw new Error('Product not found after maximum retries');
                }
            });
    };

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