是否可以通过Cypress将`postMessage`发送至应用程序?如何传递将通过`postMessage`接收的数据?

时间:2019-06-19 07:59:59

标签: browser automated-tests e2e-testing cypress

我正在创建Cypress e2e测试,但是我们的应用在父页面顶部以模式(iframe)打开。由于赛普拉斯不支持iframe,因此我决定尝试运行“独立”应用程序。但是,在启动应用时,应用是通过window.postMessage(有关流和会话的信息)从父页面获取数据的。是否可以通过赛普拉斯将此数据传递给应用程序?

我试图通过cy.request获取从后端接收的数据,它解决了会话数据的问题,但是我仍然不知道如何传递有关流的信息。

2 个答案:

答案 0 :(得分:0)

确实可以在访问页面时发送POST表单。 As of Cypress 3.2.0, you can send POST requests using cy.visit.

您可以检查cy.visit() docs for more details,但这是您尝试执行的快速代码示例:

cy.visit({
  url: 'http://my-app.com/standalone-iframe-modal.html',
  method: 'POST',
  body: {
    formKey: 'some-value',
    anotherKey: 'some-other-value'
  }
})

答案 1 :(得分:0)

您可以在赛普拉斯中发送window.postMessage()。由于Cypress在您的应用程序旁边的浏览器中运行,因此它可以访问所有Web API。

为了获得对应用程序window对象的引用,可以使用cy.window()

将所有内容放在一起,这就是将postMessage发送到被测应用程序的方式:

cy.window() // get a reference to application's `window`
.then($window => {
  const message = 'some data here'
  $window.postMessage(message, '*')
})