Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bug where cy.createSnapshot would interact with documents using custom elements #8080

Merged
merged 9 commits into from
Jul 28, 2020
29 changes: 29 additions & 0 deletions packages/driver/cypress/fixtures/custom-elements.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<title>Custom Elements</title>
</head>
<body>
<cy-snapshot-element id="snapshot-element"></cy-snapshot-element>

<script type="text/javascript">
// define empty functions to stub in tests
window.shadowScreenshotConstructor = function () {}
window.shadowScreenshotAttributeChanged = function () {}

if (window.customElements) {
window.customElements.define('cy-snapshot-element', class extends HTMLElement {
constructor () {
super()

window.shadowScreenshotConstructor()
}

attributeChangedCallback () {
window.shadowScreenshotAttributeChanged()
}
})
}
</script>
</body>
</html>
24 changes: 24 additions & 0 deletions packages/driver/cypress/integration/cy/snapshot_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,28 @@ describe('driver/src/cy/snapshots', () => {
})
})
})

context('custom elements', () => {
beforeEach(() => {
cy.visit('/fixtures/custom-elements.html')
})

// https://github.com/cypress-io/cypress/issues/7187
it('does not trigger constructor', () => {
const constructor = cy.stub(cy.state('window'), 'shadowScreenshotConstructor')

cy.createSnapshot()

expect(constructor).not.to.be.called
})

// https://github.com/cypress-io/cypress/issues/7187
it('does not trigger attributeChangedCallback', () => {
const attributeChanged = cy.stub(cy.state('window'), 'shadowScreenshotAttributeChanged')

cy.createSnapshot()

expect(attributeChanged).not.to.be.called
})
})
})
7 changes: 6 additions & 1 deletion packages/driver/src/cy/snapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ const create = ($$, state) => {

// TODO: throw error here if cy is undefined!

const $body = $$('body').clone()
// cloneNode can actually trigger functions attached to custom elements
// so we have to use importNode to clone the element
// to the outer doc and then reassign ownership to the original doc
// https://github.com/cypress-io/cypress/issues/7187
// https://github.com/cypress-io/cypress/issues/1068
const $body = $$(state('document').adoptNode(document.importNode($$('body')[0], true)))

// for the head and body, get an array of all CSS,
// whether it's links or style tags
Expand Down