如何在窗口中单击“接受”并与赛普拉斯确认

时间:2020-06-01 09:27:59

标签: alert cypress

我刚收到这种类型的警报,却不知道如何用赛普拉斯处理它

please click here to view my alert

基本上,我认为从文档中看,赛普拉斯会自动接受警报,但发现警报出现时,赛普拉斯窗口却被卡住了

我尝试了一些在线阅读的解决方案,但都无济于事。像下面这样的

    cy.on('window:confirm', str => {
        expect(str).to.eq('do you swear you are a legitimate user and intend to act honestly?')
    })

正如他们所说,如果这个返回true或不返回任何东西,它将起作用。但实际情况并非如此。

    it('interceptorTrigger', () => {
        cy.visit('http://localhost:9009/?path=/story/loginwithemail--interceptortrigger')
        cy.getIframe('#storybook-preview-iframe')
            .find('button')
            .click()
        cy.on('window:confirm', str => {
            // expect(str).to.eq('do you swear you are a legitimate user and intend to act honestly?')
        })
        cy.wait(2000)
        cy.log('done')
    })

1 个答案:

答案 0 :(得分:0)

在致电cy.on('window:confirm',...)之前尝试定义您的click()

查看"Catalog of Events" API文档页面 如果您向下滚动到Examples > Window Confirm,它将显示如何拦截呼叫并对其进行断言:

// Define
cy.on('window:confirm', (str) => {
  expect(str).to.eq('first confirm')
  // return false to deny
})
// THEN click
cy.get('button').click()

还可以分享getIframe()函数的外观吗?