Cypress - iframe 处理

Bru*_*uez 2 javascript iframe cypress cypress-component-test-runner cypress-iframe

我有一些代码尝试捕获 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})
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

在此输入图像描述

在此输入图像描述

最后,这是 iframe 信息:

在此输入图像描述

Zai*_*sta 5

我假设您只是错过了在加载正文之前获取 iframe 的重试。简单getshould组合不会等待这种情况发生。官方文档声明您应该.its('body')在使用 iframe 时使用,因此请尝试以下操作:

cy.get('.aut-iframe').its('body').then((body) => { cy.wrap(body).should('...') }
Run Code Online (Sandbox Code Playgroud)

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