简体   繁体   English

赛普拉斯 - iframe 处理

[英]Cypress - iframe handling

I have some code that tries to catch elements inside an iframe but I just keep getting thrown back an error我有一些代码试图捕获 iframe 中的元素,但我只是不断被抛出一个错误

it('Send Medication',function(){
       cy.get('.aut-iframe') 
       .should(iframe => expect(iframe.contents().find('body')).to.exist)
       .then(iframe => cy.wrap(iframe.contents().find('body')))
       .within({}, $iframe => {
            cy.get('.pending-medication-check-box').click({force:true})

This is the error that I get:这是我得到的错误:

在此处输入图像描述

在此处输入图像描述

Lastly, this is the iframe info:最后,这是 iframe 信息:

在此处输入图像描述

I would assume you are just missing retries for fetching the iframe until the body is loaded.我假设您只是在加载正文之前错过了获取 iframe 的重试。 Simple get and should combo doesn't wait for this to happen.简单的getshould组合不会等待这种情况发生。 Official docs state you should use .its('body') when working with iframes, so try something like this:官方文档声明您应该在使用 iframe 时使用.its('body') ,因此请尝试以下操作:

cy.get('.aut-iframe').its('body').then((body) => { cy.wrap(body).should('...') }

Reference: https://www.cypress.io/blog/2020/02/12/working-with-iframes-in-cypress/#header参考: https ://www.cypress.io/blog/2020/02/12/working-with-iframes-in-cypress/#header

  1. Install the cypress-iframe plugin.安装cypress-iframe插件。

  2. After installation write import 'cypress-iframe';安装后写入import 'cypress-iframe'; under cypress/support/commands.jscypress/support/commands.js

  3. Then finally your code should look like:最后你的代码应该是这样的:

cy.frameLoaded('.aut-iframe')
cy.iframe('.aut-iframe')
  .find('.pending-medication-check-box')
  .should('be.visible')
  .click()

First in your command file add this:首先在你的命令文件中添加这个:

Cypress.Commands.add('getIframe', () => {
return cy
    .get('.aut-iframe')
    .its('0.contentDocument.body').should('not.be.empty')      
    .then(cy.wrap)
})

Then in your test you can use the command above in this way:然后在您的测试中,您可以通过这种方式使用上面的命令:

cy.getIframe()
  .get('.pending-medication-check-box').click({force:true})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM