访问一个新窗口 - cypress.io [英] Access a new window - cypress.io

查看:2041
本文介绍了访问一个新窗口 - cypress.io的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题就是这么简单。在赛普拉斯,我如何访问在运行测试时打开的新窗口。



重新创建的步骤:



< blockquote>


  1. 运行测试。执行一些操作后,会弹出一个新窗口(该URL本质上是动态的)。

  2. 填写新窗口中的字段,然后单击几个按钮。

  3. 在新窗口中完成所需操作后,关闭新窗口并返回主窗口。

  4. 使用主窗口继续执行。


兴趣点:焦点应该是

 主窗口 - >新窗口 - >主窗口

我读过一些与使用 iframe有关的事情确认框,但这里没有。与访问一个全新的窗口有关。像 Window Handlers 中的内容硒。遗憾的是找不到与之相关的任何内容。

解决方案

目前的版本无法通过赛普拉斯访问新窗口。



但是,现在有很多方法可以在赛普拉斯测试此功能。您可以将测试分成不同的部分,但仍然可以确信您的应用程序已被覆盖。



  1. 编写测试以检查在您的应用中执行操作时, window.open 使用 cy.spy()<来调用事件/ code> 收听 window.open 事件。




  cy.visit('http:// localhost :3000',{
onBeforeLoad(win){
cy.stub(win,'open')
})
}

// Do您的应用中的操作,如cy.get('。open-window-btn')。click()

cy.window()。its('open')。should('be.called ')





  1. 在新测试中,使用 cy.visit() 转到将在新窗口中打开的网址,填写字段并单击按钮,就像在赛普拉斯测试中一样。




  cy.visit('http:// localhost:3000 / new-window')

// Do您想要在新窗口中测试的操作

可以找到完全正常工作的测试示例这里


The question is as simple as that. In Cypress, how can I access a new window that opens up when running the test.

Steps to recreate :

  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.

Point of interest: the focus should be

main window -> new window -> main window

I have read few things that relate to use of iframe and confirmation box, but here its none of those. Relates to accessing a whole new window. Something like Window Handlers in Selenium. Unfortunately could not find anything related to it.

解决方案

Accessing new windows via Cypress is not possible in its current version.

However, there are many ways this functionality can be tested in Cypress now. You can split up your tests into separate pieces and still have confidence that your application is covered.

  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

Fullly working test example can be found here.

这篇关于访问一个新窗口 - cypress.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆