赛普拉斯 - 在 iframe 中运行测试 [英] Cypress - run test in iframe

查看:26
本文介绍了赛普拉斯 - 在 iframe 中运行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 iframe 中查找元素,但它不起作用.

I'm trying to find elements in iframe but it doesn't work.

有没有人有一些系统可以在 iframe 中使用 Cypress 运行测试?一些进入 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 中运行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆