我正试图让柏树找到我的iframe。我发现了一个GitHub问题,该问题讨论了关于cypress关于iframe的局限性的解决方法,而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}
/>
赛普拉斯告诉我,它找不到ID为haha
的元素。
答案 0 :(得分:0)
这可能只是因为您要查找的ID是#haha,而iframe的ID是hahah(带有额外的h)。尽管不能保证您不会误将id拼错,但我还是建议您使用更具描述性的名称,以使可能性尽可能小。