javascript - 赛普拉斯:如何处理从 iframe 触发的窗口确认关闭
问题描述
我需要有关如何关闭从 iframe 触发的确认窗口的帮助。我正在尝试下面的代码,但这仍然无法关闭确认窗口。
cy.get('iframe[id="ParentMainContent_Frame"]').then(($iframe) => {
const $body = $iframe.contents().find('body')
const $win = $iframe[0].contentWindow
cy.stub($win,'confirm').as('windowConfirm')
cy.wrap($body)
.find('#ParentMainContent_MainContentMaster_ctl00_PlaceOrderButton').click().should(function () {
expect(this.windowConfirm).to.be.calledWith('Thank you for your Order!')
})
})
触发此窗口的按钮如上所示:
希望有人可以看看和帮助。
解决方案
使用cypress-iframe可以让您在开始测试其内容之前更多地检查 iframe 加载是否已完成。
缺点是这个库的方法 yieldbody
但不是 iframe 窗口,尽管我认为你可以使用body.ownerDocument.window
它来获取它。
cy.enter('iframe[id="ParentMainContent_Frame"]').then(getBody => {
const body = getBody();
const win = body.ownerDocument.window;
cy.spy(win,'confirm').as('windowConfirm'); // spy not stub
body.find('#ParentMainContent_MainContentMaster_ctl00_PlaceOrderButton')
.click()
.should(function () {
expect(this.windowConfirm).to.be.calledWith('Thank you for your Order!')
})
})
})
推荐阅读
- uwp - UWP 应用程序失败 WACK 并出现多个错误
- php - 提取距离较近的 SPACE 和某个字符之间的子字符串
- react-native - 创建一个可重用的 React Native 模态组件
- azure-data-lake - U-SQL 输出器的提示并行化
- ios - 动态调整 UICollectionViewCell 和 UICollectionView 的大小,可能是 UITableViewCell
- node.js - 带有linkedin配置文件的Auth0返回空配置文件
- c - 自动矢量化随机播放指令
- angular - 创建带有排序和过滤选项的表
- javascript - 全部或部分在 TypeScript 中模拟类和接口
- c# - 如何在代码后面/ XAML中获取x:元素的名称