Reputation: 5496
I have an element in my HTML which contain one checkbox; something like (the actual output is a bit more complex as we are using React + Material UI. This is however the general idea, one 'container' with only one checkbox within):
<span class="..." cy-data="checkbox-container">
...
<checkbox ... />
</span>
At the moment to get checkbox in my test I'm using this:
cy.get('[data-cy=checkbox-container]').within(() => {
cy.get('[type="checkbox"]').check();
})
Is there a shorter way to write this?
cy.get('[data-cy=checkbox-container]').get('[type="checkbox"]')
returns the first checkbox found in the complete document; not the one under checkbox-container
.
cy.get('[data-cy=checkbox-container] > [type="checkbox"]')
raises a "not found" error.
Upvotes: 4
Views: 11545
Reputation: 141
Would something like this work?
cy.get('.checkbox:contains("checkbox-container")').within(() => {
cy.get('[type="checkbox"]').check();
});
It's obviously very similar to the code you posted but I recently used selector:contains("specificThingImLookingFor")
to get myself out of a jam.
Sourced from here: https://glebbahmutov.com/cypress-examples/6.5.0/commands/querying.html#jquery-selectors
Upvotes: 0
Reputation: 3741
You were close with the first solution, you can actually use find()
for this, that searches for the element within the previous search results. So it would look like this:
cy.get('[data-cy=checkbox-container]')
.find('[type="checkbox"]')
Upvotes: 11
Reputation: 142
I reckon your values are not dynamic in the checkbox, in that case, you can use
cy.get('[type="checkbox"]').check(['name']) right?
Did you try this? Or you are trying something different? If so can you please explain a bit more?
Cheers,
Upvotes: 0