Cypress 避免等待背景图像加载

Var*_*eja 1 javascript testing unit-testing cypress

我正在为登录页面编写测试:

cy.visit(loginUrl);
//   type username and password
cy.get('#user').type(email);
cy.get('#password').type(password);
cy.get('#submit').click();
Run Code Online (Sandbox Code Playgroud)

但我可以看到,尽管文本框可见,但在加载背景图像之前,cypress 不会开始测试。背景图像有点大,加载需要时间,因此测试必须等待很长时间才能完成登录。

如果文本框可用,如何使 Cypress 不等待背景图像加载并开始输入?

Tes*_*ick 6

用截距对背景图像进行存根。

如果我制作一个显示大型 NASA 图像的示例网站:
https://images-assets.nasa.gov/image/PIA15985/PIA15985~orig.jpg加载大约需要 2 秒。

<body>
  <img src="https://images-assets.nasa.gov/image/PIA15985/PIA15985~orig.jpg" />
</body>
Run Code Online (Sandbox Code Playgroud)
cy.viewport(1500,2000)

Cypress.automation('remote:debugger:protocol', {
  command: 'Network.clearBrowserCache',
})

const start = Date.now()
cy.visit(url).then(() => {
  expect(Date.now() - start).to.be.greaterThan(1500)
})
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


添加夹具拦截,加载本地光盘大约需要200ms。

cy.viewport(1500,2000)

Cypress.automation('remote:debugger:protocol', {
  command: 'Network.clearBrowserCache',
})

cy.intercept(/PIA15985~orig.jpg/, {fixture: '2560x12932.svg'})

const start = Date.now()
cy.visit(url).then(() => {
  expect(Date.now() - start).to.be.lessThan(500)
})
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述