未找到元素时导航到下一页

问题描述 投票:0回答:1

我创建了一个脚本,用于在 SAP Hybris 与 Cypress 之间进行测试。一般来说,一切工作正常,尽管我想包含一个函数,如果在当前页面上找不到搜索的元素,该函数基本上会导航到下一页。请看一下代码:

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);
    });
});

它成功测试了第一个产品(G0000605),而当它尝试对第二个产品(G0000606)执行相同操作时,我收到了此错误:

10000 毫秒后超时重试:预计会在选择器:“span.yw-coll-browser-hyperlink”中找到内容:“突尼斯产品目录:已上演”,但从未找到。

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 

它没有找到 G0000606 产品,因为它位于第二页,所以 Cypress 应该导航到第二页,但它只是抛出了一个错误,无法找到所请求的产品。

我认为脚本的这一部分应该修改一些内容:

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');
                }
            });
    };
testing automated-tests cypress
1个回答
0
投票

if/else
块永远不会到达
else

这就是罪魁祸首;

cy.contains('span.yw-coll-browser-hyperlink', CATALOG_LABEL, { timeout: 10000 }) 
  .then(productElement => {
    if (productElement.length) {
      ...
    } else if (retries < maxRetries) {
  1. cy.contains('span.yw-coll-browser-hyperlink', CATALOG_LABEL)
    如果未找到 CATALOG_LABEL,则测试失败。

  2. if (productElement.length)
    表示“如果我找到了该元素”。但这始终是正确的,因为如果不是,则上面的第 1 点已经通过了测试。

相反,这是您需要的代码。我假设可能有几个不同的

span.yw-coll-browser-hyperlink
包含不同的文本。

cy.get('span.yw-coll-browser-hyperlink')
  .then(links => {

    // check the contains CATALOG_LABEL here (non-failing if not found)
    const productElement = links.filter(`:contains(${CATALOG_LABEL})`)

    if (productElement.length) {
      ...
    } else if (retries < maxRetries) {

© www.soinside.com 2019 - 2024. All rights reserved.