【问题标题】:trouble finding iframe with cypress用柏树找不到 iframe
【发布时间】:2019-01-22 03:18:19
【问题描述】:

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

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}
 />

但是,Cypress 告诉我它找不到 ID 为 haha 的元素。

【问题讨论】:

    标签: javascript reactjs iframe cypress


    【解决方案1】:

    这可能只是因为您要查找的 id 是 #haha 而 iframe 的 id 是 haha​​h (带有额外的 h )。虽然不能保证您不会不小心拼错 id,但我还是建议您使用更具描述性的名称,以减少这种可能性。

    【讨论】:

      猜你喜欢
      • 2021-01-27
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-07
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      相关资源
      最近更新 更多