I have a table and I want to ensure that values are not repeated within a column using Cypress. Currently, I am doing
cy.get("#myTable")
.find(".table")
.contains("unique_value")
.should("exist")
This piece of code does check if a value in a column exists but it doesn't ensure that its the only entry with this value in the table. How do I check for uniqueness through Cypress?
How do I get an element's text contents? Cypress commands yield jQuery objects, so you can call methods on them. If the text contains a non-breaking space entity then use the Unicode character \u00a0 instead of . Tip: watch the Confirming the text with non breaking space entity video.
Cypress can validate the text on an element with the help of jQuery text() method. This method shall help us to fetch the text content on the selected element. We can also put assertions on the text content of the element. cy.
You can also verify visibility using not. be. visible , and you can use and expect statement too.
Surprisingly, this
cy.get("#myTable")
.find(".table")
.contains("unique_value")
.should('have.length', 1);
or even this
cy.get("#myTable")
.find(".table td")
.contains("unique_value")
.should('have.length', 1);
will return a false positive - if you run it with two unique_value
table cells it incorrectly passes.
Ref contains - yields indicates a single value is returned.
The best way I found was to shift the contains()
up into the .find()
selector,
cy.get("#myTable")
.find('.table td:contains("unique_value")')
.should('have.length', 1)
The above tests uniqueness between cells. If you want to also test the value within the cell, the simplest way is to invoke the text()
method.
cy.get("#myTable")
.find('.table td:contains("unique_value")')
.should('have.length', 1) // ensure only one cell has value
.invoke('text')
.should('equal', 'unique_value') // check the exact content of the cell
See the first example on this page Assertions
sorry about the previous answer, I forgot with .contains it selects sorts out 1 element from the found list. anyway, there are two ways to do this.
1st : it'll look like,
cy.get(".table")
.find(('td:contains("unique_value")'))
.should('have.length', 1)
Note: This might be problematic if the same table cell has repetitive unique_value
For example: Let's say you are expecting a unique id (1234) in a table cell, but the table looks as follows
<tr>
<td>1234, 1234<td>
<td>value<td>
<tr>
If we take this scenario, the above solution will make the test as passed even though the unique id is repeated in the same cell. If you want to validate those kinds of scenarios too, I think the best solution would be,
cy.get(".table")
.then($el => {
expect($el[0].innerText.split('unique_value').length).to.equal(2)
})
in this case, if the table does not contain unique_value the array length will be 1 and if it has more than 1 unique_value the array length will be more than 2 so, this will work with any string, paragraph, table, etc.
cheers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With