首页 > 解决方案 > 用柏树找不到 iframe

问题描述

我试图让柏树找到我的 iframe。我发现了一个 GitHub 问题,它讨论了关于 iframe 的 cypress 限制的变通方法,就是我采用的解决方案。

Cypress.Commands.add('iframe', { prevSubject: 'element' }, $iframe => {
    return new Cypress.Promise(resolve => {
        $iframe.on('load', () => {
            resolve($iframe.contents().find('body'));
        });
    });
});

这是我实际使用此命令的地方。

context("Basic simple test", () => {
    it("can visit our app", () => {
        cy.visit("http://localhost:3000");
        cy.get('#haha').iframe();
    })
})

这是我的 iframe 代码。

<StyledFrame
   sandbox="allow-forms allow-scripts allow-same-origin allow-modals allow-popups allow-presentation"
   title="sandbox"
   id="hahah"
   srcDoc={this.state.isLoading ? loader : this.state.bundle}
 />

然而,赛普拉斯告诉我它找不到 id 为haha.

标签: javascriptreactjsiframecypress

解决方案


这可能只是因为您要查找的 id 是 #haha 而 iframe 的 id 是 haha​​h (带有额外的 h )。尽管不能保证您不会不小心拼错 id,但我建议您使用更具描述性的名称,这样它就不太可能发生。


推荐阅读