【问题标题】:How to achieve retries for individual tests using Cypress Cucumber Preprocessor?如何使用 Cypress Cucumber Preprocessor 实现单个测试的重试?
【发布时间】:2021-12-30 20:58:57
【问题描述】:

我目前面临的挑战是实现单个测试重试以稳定一小部分特定测试,这些测试依赖于某些后台处理,因此往往不稳定。我正在使用 Cypress 9.2.0 和 TypeScript 和 Cypress Cucumber Preprocessor 4.3.1。

为了提供更多详细信息,应用程序接收在后台处理的事件(通常最多需要 1-2 秒),然后创建数据记录。然后这些记录会显示在 UI 中的表格中。

在某些 Cucumber 场景中,我会端到端地测试这些案例。由于处理有时需要更长的时间,我想预防性地包括仅适用于这些测试用例的重试,更具体地说,是检查表格中最终显示的场景的“然后”步骤。

由于不幸的是,像标准赛普拉斯测试中的单个测试重试不适用于 Cucumber 预处理器,并且 cypress.json 中的全局测试重试也有问题,我想知道是否有其他方法可以实现这一点?

正常赛普拉斯测试重试的文档:https://docs.cypress.io/guides/guides/test-retries

【问题讨论】:

    标签: typescript automated-tests cucumber cypress cypress-cucumber-preprocessor


    【解决方案1】:

    我找到了解决我的问题的方法,尽管一般的解决方案可能并不完全理想。但首先,我还想提一下我之前尝试过但对我不起作用的方法:

    1. Individual Test Retries 在使用 Cypress Cucumber 预处理器时无法使用特定的测试步骤。

    2. Global Test Retries 正如问题中已经提到的那样,不幸的是在与 Cucumber Preprocessor 相关的问题上也存在问题。

    3. 我尝试过的另一种方法是使用cy.should() with a callback,但也没有成功。

    4. 第四种方法基于conditional testing,最后是让我得到以下解决方案的方法,同时考虑了this post关于如何不破坏赛普拉斯测试的想法,如果元素不可用:

    Then('the results related to some search keyword {string} are shown in the table', (search: string) => {
      checkTableWithRetries(search, 2);
    });
    
    function checkTableWithRetries(searchCriteria: string, retries: number) {
      cy.get('table').then(($table) => {
        if (checkIfTableRowExists($table) || retries === 0) {
          cy.get('table').find('tbody').contains('td', searchCriteria);
        } else {
          const time = retries === 1 ? 10000 : 5000;
          cy.wait(time);
    
          search(searchCriteria);
          checkTableWithRetries(searchCriteria, retries - 1);
        }
      });
    }
    
    function checkIfTableRowExists(element: JQuery<HTMLTableElement>) {
      return element.find('tbody').find('tr').length === 1;
    }
    
    function search(search: string) {
      cy.getByTestId('search-input').clear().type(`${search}`).should('have.value', `${search}`);
      cy.intercept('GET', `/api/endpoint**`).as('search');
      cy.getByTestId('search-button').click();
      cy.wait('@search');
    }

    如果一个元素被创建并显示在表格中,该代码确保我可以以不同的延迟连续检查多次。如果重复x次后元素仍然不存在,则可以认为确实有错误。

    为了澄清,使用的函数cy.getByTestId()不是标准的赛普拉斯命令,而是根据官方最佳实践部分here中的建议作为自定义命令添加的。

    Cypress.Commands.add('getByTestId', (selector, ...options) => {
      return cy.get(`[data-test=${selector}]`, ...options);
    });
    

    【讨论】:

      猜你喜欢
      • 2023-02-21
      • 2021-08-07
      • 2019-09-30
      • 1970-01-01
      • 2022-09-30
      • 1970-01-01
      • 2023-02-02
      • 2022-11-10
      • 1970-01-01
      相关资源
      最近更新 更多