最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cypress - Assert only one element with a text exists - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Jul 6, 2020 at 19:10 Sree.Bh 1,7892 gold badges22 silver badges27 bronze badges asked Jul 5, 2020 at 17:40 mikasamikasa 9003 gold badges13 silver badges33 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 15

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.

发布评论

评论列表(0)

  1. 暂无评论