赛普拉斯:如果 its() 函数超时,如何通过

时间:2021-06-05 02:55:34

标签: cypress

我有一个测试,我想知道在任意超时期限后未设置窗口属性。

所以,在伪代码中:

cy.window().its('msg_1', {
  timeout: 3000
}).should.timeoutIn3000

换句话说,如果达到 3000 毫秒超时,则测试通过。如果窗口属性 msg_1 在 3000 毫秒之前出现,则测试应该失败。

这可能吗?我可能在这里遗漏了明显的内容。

谢谢

2 个答案:

答案 0 :(得分:4)

策略可能是测试、等待、再测试。

it('does not see the message', () => {

  cy.visit('http://example.com').then(win => {
    setTimeout(() => {
      win.msg_1 = 'hi'
    }, 1000)
    
  })

  cy.window().then(win => expect(win.msg_1).to.eq(undefined))  // passes
  cy.wait(3000)
  cy.window().then(win => expect(win.msg_1).to.eq(undefined))  // fails

})

答案 1 :(得分:0)

好的,按照@EQQ 的有用回复,我发现了以下作品:

    // This will pass if it is not there
    cy.window().then(win => expect(win.msg_1).to.eq(undefined))

    // Wait for the timeout and test again
    cy.window().wait(3000).then(win => {
      expect(win.msg_1).to.eq(undefined)
    })

并作为自定义命令:

    /**
     * Use to test that a window property does NOT turn up
     * within the specified time period
     */
    Cypress.Commands.add("notIts", (prop, timeout) => {
      cy.window().then(win => expect(win[prop]).to.eq(undefined))
      cy.window().wait(timeout).then(win => {
        expect(win[prop]).to.eq(undefined)
      })
    });

用于:

    cy.notIts('msg_1', 3000)

也许“notIts”不是一个好名字,但无论如何,这对我有用。

谢谢! 默里