[英]Cypress: testing React app in offline mode causes test to hang
I am testing a React application created using create-react-app
.我正在测试使用
create-react-app
的 React 应用程序。 I overwrite the App.js file to display text depending on whether the browser is online/offline:我覆盖 App.js 文件以根据浏览器是否在线/离线显示文本:
import { Offline, Online } from "react-detect-offline";
function App() {
return (
<div className="App">
<Online>
<p data-testid="online-text">online</p>
</Online>
<Offline>
<p data-testid="offline-text">offline</p>
</Offline>
</div>
);
}
export default App;
I then run npx cypress open
and created the following test file:然后我运行
npx cypress open
并创建了以下测试文件:
/// <reference types="cypress" />
const goOffline = () => {
cy.log("**go offline**")
.then(() => {
Cypress.automation("remote:debugger:protocol", {
command: "Network.enable",
});
})
.then(() => {
Cypress.automation("remote:debugger:protocol", {
command: "Network.emulateNetworkConditions",
params: {
offline: true,
latency: -1,
downloadThroughput: -1,
uploadThroughput: -1,
},
});
});
};
const goOnline = () => {
cy.log("**go online**")
.then(() => {
Cypress.automation("remote:debugger:protocol", {
command: "Network.emulateNetworkConditions",
params: {
offline: false,
latency: -1,
downloadThroughput: -1,
uploadThroughput: -1,
},
});
})
.then(() => {
Cypress.automation("remote:debugger:protocol", {
command: "Network.disable",
});
});
};
describe("app", () => {
it("should render online text when online", () => {
goOnline();
cy.get("[data-testid='online-text']").should("exist");
goOnline();
});
it("should render offline text when offline", () => {
goOffline();
cy.get("[data-testid='offline-text']").should("exist");
goOnline();
});
it("should not render online text when offline", () => {
goOffline();
cy.get("[data-testid='online-text']").should("not.exist");
goOnline();
});
});
This tests the app in offline mode according to this guide .这会根据本指南在离线模式下测试应用程序。 The first 2 tests run as expected but the 3rd test gets stuck in an infinite loop:
前 2 个测试按预期运行,但第 3 个测试陷入无限循环:
What Cypress version are you using?您使用的是哪个赛普拉斯版本?
I had a similar issue when after going Offline, Cypress couldn't go Online.脱机后,我遇到了类似的问题,Cypress 无法 go 联机。 After looking for an answer stumbled upon this reason https://github.com/cypress-io/cypress/issues/17723#issuecomment-906152925 .
在寻找答案后偶然发现了这个原因https://github.com/cypress-io/cypress/issues/17723#issuecomment-906152925 。 Eventually, I was able to get my code working using Cypress version
6.9.1
and not 9.5.1
最终,我能够使用 Cypress 版本
6.9.1
而不是9.5.1
让我的代码正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.