gpt4 book ai didi

javascript - 访问新窗口 - cypress.io

转载 作者:行者123 更新时间:2023-12-03 02:26:07 25 4
gpt4 key购买 nike

问题就这么简单。在 Cypress 中,如何访问运行测试时打开的新窗口。

重新创建的步骤:

  1. Run the test. After some action, new window pops up (the url is dynamic in nature).
  2. Fill in the fields in the new window, and click a few buttons.
  3. After required actions are completed in the new Window, close the new window and move back to the main window.
  4. Continue execution with the main window.

兴趣点:焦点应该是

main window -> new window -> main window

我读过一些与使用iframe确认框相关的内容,但这里没有这些。涉及访问一个全新的窗口。类似于 Window Handlers在 Selenium 中。不幸的是找不到任何相关内容。

最佳答案

通过 Cypress 访问新窗口是 intentionally not supported .

但是,现在可以通过多种方式在 Cypress 中测试此功能。您可以将测试分成单独的部分,并且仍然确信您的应用程序已被覆盖。

  1. Write a test to check that when performing the action in your app, the window.open event is called by using cy.spy() to listen for a window.open event.
cy.visit('http://localhost:3000', {
onBeforeLoad(win) {
cy.stub(win, 'open')
}
})

// Do the action in your app like cy.get('.open-window-btn').click()

cy.window().its('open').should('be.called')
  1. In a new test, use cy.visit() to go to the url that would have opened in the new window, fill in the fields and click the buttons like you would in a Cypress test.
cy.visit('http://localhost:3000/new-window')

// Do the actions you want to test in the new window

可以找到完全有效的测试示例 here .

关于javascript - 访问新窗口 - cypress.io,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47749956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com