Skip to content

cy.within support #8

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

Closed
npeterkamps opened this issue May 30, 2018 · 4 comments
Closed

cy.within support #8

npeterkamps opened this issue May 30, 2018 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@npeterkamps
Copy link
Collaborator

  • cypress-testing-library version: 2.0.0
  • node version: 8.4.0
  • npm version: 5.3.0

Relevant code or config

cy
  .getByLabelText("Label text")
  .getFormByTitle("Form title") // custom command
  .within(() => {
// select editor with the same label as the previous getByLabelText, but nested in the form
    cy.getByLabelText("Label text") 
  })

Philosophy

There's nothing to guarantee that label texts are unique within the page. Users will be filling out a form one section at a time. The form can have a title to indicate to the user what they should be filling in, while the label texts themselves are no different to another form with a different title.

So to mimic how the user would fill in the page, you'd look for the form with that title and look for label texts within that form section.

Problem description:

Cypress supports a within function. All cy statements within the within function scope should use its subject as the container.

Currently, cypress-testing-library always uses the window.document as container.

Suggested solution:

Check cy.state("withinSubject") (Cypress source) to check whether the query is being executed within a within function scope and use it instead of doc as container parameter to query functions.

@kentcdodds
Copy link
Member

I definitely want this 👍

@kentcdodds kentcdodds added enhancement New feature or request help wanted Extra attention is needed labels May 30, 2018
@npeterkamps npeterkamps removed the help wanted Extra attention is needed label Jun 3, 2018
@npeterkamps npeterkamps self-assigned this Jun 3, 2018
@sabin-bhattarai-cko
Copy link

The following code does not work

cy.get("contextText").within(() => {
    cy.getByLabelText("labelForDropdown")
      .click({ force: true })
      .getByText("Hello")
      .click({ force: true });
  });

Only by adding the cy.wait(5000) works. I thought the within would wait automatically.

cy.get("contextText").within(() => {
    cy.wait(5000);
    cy.getByLabelText("labelForDropdown")
      .click({ force: true })
      .getByText("Hello")
      .click({ force: true });
  });

@alexkrolick
Copy link
Contributor

Isn't the click with force preventing the retry from doing it's validation on "failed" click attempts?

@mwmcode
Copy link

mwmcode commented Apr 24, 2020

is this supported now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants