cypress: Failed to find body in content Document

Current behavior

its .0.contentDocument.body - assert expected <body> not to be empty

Desired behavior

correctly find the body on the webpage

Test code to reproduce

its occurring in the beforeEach not relation to any individual test

Cypress Version

10.8.0

Node version

v16.16

Operating System

MacOS current

Debug Logs

Timed out retrying after 6000ms: cy.its() errored because the property: contentDocument returned a null value. The property: body does not exist on a null value.

cy.its() waited for the specified property body to become accessible, but it never did.

If you do not expect the property body to exist, then add an assertion such as:

cy.wrap({ foo: null }).its('foo.baz').should('not.exist')

Because this error occurred during a before each hook we are skipping the remaining tests in the current suite:

Other

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

Hiya guys, did some digging and this was the solution that I found.

  1. Disable chromeWebSecurity (set to false)
  2. Inside your test code add this function as you see fit:
const getElemInsideIFrame = (iFrameselector: string, elementSelector: string) =>
  cy
    .get(iFrameselector)
    .find('iframe')
    .then(($el) => {
      if (!$el || $el.length === 0) {
        return cy.wrap(null);
      }
      return cy.wrap($el[0].contentWindow?.document.body.querySelector(elementSelector));
    });

Notice how I’ve used contentWindow instead of contentDocument. I found it on a tutorial here. An oldie but a goodie.

Essentially, imagine you were in the javascript console in your browser and you were searching for an element inside an iframe there. Whatever is returned I wrap up and send back to cypress for more chaining and testing code.

NOTE. Make sure the iframe is fully loaded before calling this. Lest the content of the iframe will return null and make your tests flaky. I personally used cy.intercept and cy.wait to check when certain network requests were complete and then run my iframe automation testing code

@vpkhachane is the iframe in the same origin as the document? If not you’ll have to wait for our iframe support feature.

Could you provide a repo that recreates the issue. It’s difficult to determine why contentDocument may return null for body when we don’t have access to the running sight. Typically when I put together sample repos I start with https://github.com/cypress-io/cypress-test-tiny

Is the iframe feature now available? I am facing a similar issue and the iframe is from a different origin

@mud1387 please open a new issue with your example and details on how to reproduce what you are seeing. We will be happy to take a look and see if we can help.