Cypress - run test in iframe

北城以北 提交于 2020-02-25 08:23:28

问题


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

Is there anyone who have some system to run tests with Cypress in iframe? Some way to get in iframe and work in there.


回答1:


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')

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).

This is a workaround though, it worked for me locally but not during CI runs.




回答2:


that is correct. Cypress doesn't support Iframes. It is simple not possible at the moment. You can follow (and upvote) this ticket: https://github.com/cypress-io/cypress/issues/136



来源:https://stackoverflow.com/questions/57605596/cypress-run-test-in-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!