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

cy.wrap() unexpectedly change command behaviour #28298

Closed
ValeronAI opened this issue Nov 9, 2023 · 2 comments
Closed

cy.wrap() unexpectedly change command behaviour #28298

ValeronAI opened this issue Nov 9, 2023 · 2 comments
Labels
stale no activity on this issue for a long period

Comments

@ValeronAI
Copy link

Current behavior

In case custom commands with previously yielded subject (prevSubject: true) should be used cy.wrap() function.

But two similars commands at the first glance in some case will give different results. For example:

cy.get("[data-cypress-id=test-element-wrapper]")
  .find("[data-cypress-id=test-element]")
  .should("have.length", 2);

and

cy.get("[data-cypress-id=test-element-wrapper]")
  .findByCypressId("test-element")
  .should("have.length", 2);

Where findByCypressId is defined:

Cypress.Commands.add(
    "findByCypressId",
    { prevSubject: true },
    (subject, selector, ...args) => {
        return cy.wrap(subject).find(`[data-cypress-id=${selector}]`, ...args);
    }
);

I expect that results of this commands will be similar. But in case DOM changing while test running, results will be different.

I have create simple example in which results of two above commands isn't similar. I test page on which change elements count after 1.5s after page initialising. Builtin find command has expected behaviour, it retries command for a few seconds and test successfully finished. But in case custom findByCypressId don't retry elements find so test failing. Below I'm leaving Vue single file component example and cypress test, also link to simple project example.

<script setup>
  import { ref } from "vue";

  const showFirstElement = ref(true);

  setTimeout(() => (showFirstElement.value = false), 1500);
</script>

<template>
  <ul>
    <li v-if="showFirstElement" data-cypress-id="test-element-wrapper">
      <span data-cypress-id="test-element">test item</span>
    </li>
    <li data-cypress-id="test-element-wrapper">
      <span data-cypress-id="test-element">test item</span>
    </li>
    <li data-cypress-id="test-element-wrapper">
      <span data-cypress-id="test-element">test item</span>
    </li>
  </ul>
</template>

Page tests:

describe("Wrap testing", () => {
    it("Use builtin find command", () => {
        cy.visit("/");

        cy.get("[data-cypress-id=test-element-wrapper]")
			.find("[data-cypress-id=test-element]")
			.should("have.length", 2);
    });
    it("Use custom command findByCypressId", () => {
        cy.visit("/");

        cy.get("[data-cypress-id=test-element-wrapper]")
			.findByCypressId("test-element")
			.should("have.length", 2);
    });
});

Desired behavior

Custom command findByCypressId("test-element") should have same behaviour as find("[data-cypress-id=test-element]").

Test code to reproduce

For testing please clone and run tests in public project: https://github.com/ValeronAI/cypress-test.

Cypress Version

13.4.0

Node version

16.14.0

Operating System

macOS Ventura 13.6

Debug Logs

No response

Other

No response

@cypress-app-bot
Copy link
Collaborator

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

@cypress-app-bot cypress-app-bot added the stale no activity on this issue for a long period label May 8, 2024
@cypress-app-bot
Copy link
Collaborator

This issue has been closed due to inactivity.

@cypress-app-bot cypress-app-bot closed this as not planned Won't fix, can't repro, duplicate, stale May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale no activity on this issue for a long period
Projects
None yet
Development

No branches or pull requests

2 participants