Skip to content

framebusting when form is dynamically created and target set to _top #26029

Open
@mschile

Description

@mschile

Current behavior

After clicking the submit button, a form is dynamically created and the target is set to _top which is causing Cypress to go back to the spec selection page.

Desired behavior

Submitting the form should not framebust and go back to the spec selection page.

Workarounds:

  • Stub document.createElement (taken from this blog):
  // before clicking on the link, stub the Document.createElement
  // and if the user is trying to create a new form, stub its
  // property "target" to not allow opening new tabs; always have it at "_self"
  cy.document().then((doc) => {
    const create = doc.createElement.bind(doc)
    cy.stub(doc, 'createElement').callsFake((name) => {
      if (name === 'form') {
        const form = create('form')
        cy.stub(form, 'target').value('_self')
        return form
      } else {
        return create(name)
      }
    })
  })
  • Intercept the response and rewrite the body:
  cy.intercept('*submit-code.js', (req) => {
    req.on('response', (res) => {
      // the form is being dynamically created and has a target of _top which causes the framebusting, so we change it to _self
      res.body = res.body.replaceAll('_top', '_self')
    })
  })

Test code to reproduce

https://github.com/mschile/cypress-test-tiny/tree/issue-26029

Cypress Version

12.7.0

Node version

v16.18.0

Operating System

macOS 12.6

Debug Logs

No response

Other

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions