question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cypress snapshotting causes `attributeChangedCallback` to be triggered on custom elements

See original GitHub issue

The documentation advertises that all websites can be tested with cypress. Unfortunately, I cannot confirm this for websites that use custom elements (https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements).

During the checks for elements with Cypress, the custom elements are apparently cloned or newly created. This means that the constructor of the custom element and possibly the attributeChangedCallback is called.

The functionality of the application is unfortunately changed by the Cypress tests! If, for example, a global variable is written in these lifecycle methods, a fetch request is executed, or the like, this happens several times in the test.

Here is a cypress project through which this behavior can be reproduced: https://gitlab.com/benjaminknauer/cypress-custom-elements-bug

The project runs tests against this page, which contains a custom element that prints calls of the lifecycle methods to the console: https://benjaminknauer.gitlab.io/cypress-custom-elements-bug/

Best regards Benjamin

Current behavior:

Custom element lifecycle methods are called several times. The execution of the tests influences the application.

image

Desired behavior:

Custom element lifecycle methods should be called just once. The execution of the application should not be affected by the tests.

image

Test code to reproduce

Project with cypress tests https://gitlab.com/benjaminknauer/cypress-custom-elements-bug

context("customElement - broken", () => {
    it("customElement", () => {
        // when
        cy.visit("https://benjaminknauer.gitlab.io/cypress-custom-elements-bug");

        // then
        cy.get(".nonExistentElement").should("be.visible");
    });
});

Site with a custom element to run test against: https://benjaminknauer.gitlab.io/cypress-custom-elements-bug/

Versions

Cypress 4.5.0 / Chrome / Fedora 30

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
goofballLogiccommented, May 20, 2020

in our case, the custom element triggers an XHR so the problem is particularly bad

0reactions
cypress-bot[bot]commented, Aug 3, 2020

Released in 4.12.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to Cypress v4.12.0, please open a new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Changelog - Cypress Documentation
When snapshotting the DOM, Cypress no longer causes attributeChangedCallback to be triggered on custom elements. Fixes #7187.
Read more >
attributeChangedCallback() always called twice ending up ...
It is called attributeChangedCallback , so fires on every single attribute change, including init when the element is added to the DOM.
Read more >
cypress-io/cypress - Gitter
We use CustomElements (WebComponents) quite a lot in our applications. ... cypress: the "attributeChangedCallback" method of our custom elements get invoket ...
Read more >
Using custom elements - Web Components | MDN
This article introduces the use of the Custom Elements API. ... The element is called word-count , its class object is WordCount ,...
Read more >
Custom Elements That Work Anywhere - Rob Dodson
In other words, attributes are useful for initial configuration, whereas properties are good for reflecting state. This leads to a few takeaways ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found