简体   繁体   English

升级到 React18 / Next.js13 时,Cypress 测试突然点击失败

[英]Cypress tests suddenly failing on click when upgrading to React18 / Next.js13

Summary概括
The cypress test suite on our project is consistently failing on a few tests on the branch updating React to version 18 and Next.js to version 13. It fails on clicking elements, with the 'element has detached from the DOM' error.我们项目上的 cypress 测试套件在将 React 更新到版本 18 和将 Next.js 更新到版本 13 的分支上的一些测试中始终失败。单击元素失败,出现“元素已从 DOM 分离”错误。 The development environment is set to react strict mode, which the tests run on.开发环境设置为反应严格模式,测试在该模式下运行。

The Problem问题
We are using MUI 5, and the test is to check different inputs and buttons.我们正在使用 MUI 5,测试是检查不同的输入和按钮。 The overall problem in the tests is that it is unable to click the checkbox input from MUI, as well as a link on a Card component from MUI as well.测试中的总体问题是它无法单击来自 MUI 的复选框输入,以及来自 MUI 的 Card 组件上的链接。 The checkbox is in a dialog modal component.该复选框位于对话框模式组件中。

The test does not error on the click() command, and the test proceeds as if it clicked it, but the checkbox does not actually get clicked on the screen.测试不会在 click() 命令上出错,并且测试会像单击它一样进行,但实际上并没有在屏幕上单击该复选框。 Adding a cy.wait() after this click does result in the checkbox actually getting clicked, but I would prefer an alternative to wait.在此单击后添加 cy.wait() 确实会导致复选框实际被单击,但我更喜欢等待的替代方法。

The test does however fail on the click() command when clicking the link in the Card component, with the following error: Cypress Error on click()但是,当单击 Card 组件中的链接时,测试在 click() 命令上失败,并出现以下错误: Cypress Error on click()

Adding a wait to the click() that clicks the 'vehicle-card-link' element makes the test and the click quite flaky, and somehow makes other tests in the same test suite that were previously not failing, more flaky.向单击“vehicle-card-link”元素的 click() 添加等待会使测试和单击非常不稳定,并且以某种方式使同一测试套件中以前未失败的其他测试更加不稳定。

The Test Code测试代码
The following is the cypress test code that is failing, with added comments for context:以下是失败的 cypress 测试代码,添加了上下文注释:

// The test
cy.getBySel('more-filters-button').click(); // Opens the MUI Dialog Modal
cy.get('input[name="condition"][value="New"]').click(); // Gets the Checkbox component and clicks on it
cy.getBySel('save-filter-modal').click(); // Clicks a button to close the modal
cy.getBySel('vehicle-card-link').first().click(); // Gets the first MUI Card and clicks on the link
cy.getBySel('vehicle-name').should('exist’); // Checks that the new page with a specific element has opened

// from the commands.js file, for context
Cypress.Commands.add('getBySel', (selector, args, operator = '') => cy.get(`[data-cy${operator}=${selector}]`, { ...args }));

Some More Context: We use the data-cy attribute on components to help with testing, and the custom command above helps to get those elements in cypress.更多上下文:我们在组件上使用 data-cy 属性来帮助进行测试,上面的自定义命令有助于在 cypress 中获取这些元素。

Important Notes重要笔记

  1. This is one of the failing tests, the others follow a similar pattern and I have left them out.这是失败的测试之一,其他测试遵循类似的模式,我将它们排除在外。 If you would like more examples, I can add them.如果您想要更多示例,我可以添加它们。
  2. This test does not fail on any other PR, neither is it very flaky on other PRs.这个测试在任何其他 PR 上都不会失败,在其他 PR 上也不会很不稳定。 The PR this is failing on only updates React to React18, and Next to Next13, along with a few changes to how images render with Next13, and should not be affecting this part of the app or code. PR this is failed on 仅将 React 更新为 React18,将 Next 更新为 Next13,以及对图像如何使用 Next13 进行渲染的一些更改,不应影响应用程序或代码的这一部分。
  3. There are other tests for other parts of the app that use checkbox and card components from MUI that are not failing.对于使用 MUI 中的复选框和卡片组件的应用程序的其他部分,还有其他测试没有失败。

Suspected Cause疑似原因
I imagine this has to do with strict mode on React, and it reloading the elements on the page twice.我想这与 React 的严格模式有关,它会两次重新加载页面上的元素。

What I've Tried我试过的

  1. Adding guards before the click command:在点击命令前添加守卫:
    .should('exist'), and .should('存在'), 和
    .should('be.visible') .should('be.visible')
    This does not affect the behaviour of the problematic tests.这不会影响有问题的测试的行为。

  2. Adding a cy.wait(100) after the click.单击后添加一个 cy.wait(100)。
    This makes the test go from failing every time to quite flaky as mentioned before, when it comes the vehicle-card-link MUI Card element.当涉及车辆卡链接 MUI 卡元素时,这使得测试从每次都失败变得非常不稳定。

  3. Adding a timeout or delay to the click command为点击命令添加超时或延迟
    . . click({delay: 1000}) or click({timeout: 1000})点击({延迟:1000})或点击({超时:1000})
    This does not affect the behaviour of the problematic tests.这不会影响有问题的测试的行为。

The fundamental problem is there are zero checks on the page changes that occur after each action.根本问题是对每次操作后发生的页面更改的检查为零。

The tests will try to run at the fastest speed possible, and that means you can (sometimes) get stale elements picked up (hence detached errors).测试将尝试以最快的速度运行,这意味着您可以(有时)拾取陈旧的元素(因此出现分离错误)。

Add some visual check after actions, as example在操作后添加一些视觉检查,例如

// open modal and confirm it's open
cy.getBySel('more-filters-button').click(); 
cy.contains('My Modal Title Here').should('be.visible')

// Change state, save, confirm modal is closed 
cy.get('input[name="condition"][value="New"]').click(); 
cy.getBySel('save-filter-modal').click(); 
cy.contains('My Modal Title Here')
  .should('not.be.visible')
//  .should('not.exist')     // or this one

// Carry on after save action is complete
cy.getBySel('vehicle-card-link').first().click(); 

Update to Cypress v12.2.0更新至 Cypress v12.2.0

The latest version of Cypress has revamped queries that detect detached errors and fixes them when the runner finds them.最新版本的 Cypress 改进了检测分离错误的查询,并在运行器发现它们时修复它们。

This step alone should fix your issue.仅这一步就可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 条带或paystack与react18不兼容 - Is stripe or paystack incompatible with react18 在 react18 中安装 react-lottie 时出错 - getting error in while installing react-lottie in react18 WebSocket 升级到 react18 后断开连接 - WebSocket gets disconnected after upgrade to react18 升级到 React 18 时有什么方法可以修复 Bootstrap 引起的错误 - Is there any way to fix errors caused by Bootstrap when upgrading to React 18 单击上一个和下一个按钮时 react.js 中的卡片转换 - card transition in react.js when click on prev and next button 尝试访问本地 API 端点时,React/Next.js docker 构建失败 - React/Next.js docker build failing when trying to reach out to local API endpoint 使用react-i18next useTranslationHooks时出现“ i18next backendConnector:加载命名空间失败” - “i18next backendConnector: loading namespace failed” when using react-i18next useTranslationHooks 如何解决错误:升级到 Next.js 11 时找不到模块“react/jsx-dev-runtime”? - How to solve Error: Cannot find module 'react/jsx-dev-runtime' when upgrading to Next.js 11? 升级到 react@18 和 react-dom@18 失败 - Upgrading to react@18 and react-dom@18 fails React JS i18next 使用 JSX 插值进行翻译 - React JS i18next translate with JSX interpolation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM