问题描述
我正在尝试在iframe中查找元素,但是它不起作用.
I'm trying to find elements in iframe but it doesn't work.
是否有人可以在iframe中使用赛普拉斯运行测试?进入iframe并在其中工作的某种方式.
Is there anyone who have some system to run tests with Cypress in iframe? Some way to get in iframe and work in there.
推荐答案
提到的一个已知问题此处.您可以创建自己的自定义cypress命令来模拟iframe功能.在您的cypress/support/commands.js
It's a known issue mentioned here. You can create your own custom cypress command which mocks the iframe feature. Add following function to your cypress/support/commands.js
Cypress.Commands.add('iframe', { prevSubject: 'element' }, ($iframe, selector) => {
Cypress.log({
name: 'iframe',
consoleProps() {
return {
iframe: $iframe,
};
},
});
return new Cypress.Promise(resolve => {
resolve($iframe.contents().find(selector));
});
});
然后您可以像这样使用它:
Then you can use it like this:
cy.get('#iframe-id')
.iframe('body #elementToFind')
.should('exist')
此外,由于CORS/同源策略的原因,您可能必须在cypress.json
中将chromeWebSecurity
设置为false
(将chromeWebSecurity设置为false可以访问嵌入在应用程序中的跨域iframe并导航到任何没有跨域错误的超域.
Also, because of CORS/same-origin policy reasons, you might have to set chromeWebSecurity
to false
in cypress.json
(Setting chromeWebSecurity to false allows you to access cross-origin iframes that are embedded in your application and also navigate to any superdomain without cross-origin errors).
这是一种解决方法,它在本地对我有效,但在CI运行期间无效.
This is a workaround though, it worked for me locally but not during CI runs.
这篇关于赛普拉斯-在iframe中运行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!