Search code examples
automationcypresse2e-testing

Value from dynamic table (Cypress)


I need to take a value from a table that changes after every page refresh. For example CPU for Chrome

describe('test dynamic table and check Chrome CPU', () => {
  it('passes', () => {
    cy.visit('some url')
    cy.contains('span', 'CPU')
      .parent()
      .within(() => {
        cy.contains('[role=cell]', 'Chrome')
      })
  })
})

Table:

table image

UPDATE: FYI After refreshing the page, you will get columns and row changing place:

enter image description here


Solution

  • You can do like this:

    cy.contains('span', 'Chrome')
      .parent()
      .within(() => {
        cy.contains('[role=cell]', '%')
          .invoke('text')
          .then((text) => {
            cy.log(text) //Logs 8.3%
    
            //removing % and converting into number and asserting
            expect(+text.replace(/%/g, '')).to.be.greaterThan(5)
            expect(+text.replace(/%/g, '')).to.be.lessThan(15)
            expect(+text.replace(/%/g, '')).to.eq(8.3)
          })
      })