Search code examples
cypress

How to check if an element has 'any' value Cypress?


I am writing some basic cypress tests and I just want to make sure that a table we have has all of the correct columns and that any data is rendering.

I know .contains() checks the element selection AND allows text matching, but there could be all sorts of data in this table, I just want to make sure something renders from the backend.

Any idea how to check if there is any (non-specific) value?

describe('Data Source Table renders properly', () => {
  beforeEach(() => {
    cy.viewport(1920, 1080)
    cy.visit('http://localhost:3000/source')
    // Assert we are on the correct page /source
    cy.url().should('include', '/source')
  })
  it('Data Source header exists', () => {
    cy.contains('Data Source')
  })
  it('Data Source table exists, column headers are correct and there is data', () => {
    cy.get('table').should('exist')
    cy.wait(2000)
    cy.get('table').contains('th', 'Ip')
    cy.get('table').contains('th', 'Station')
    cy.get('table').contains('th', 'Table')
    cy.get('table').contains('th', 'Status')
  })
})

Solution

  • You are right, .contains() allows text matching. It also allows regex matching which you can apply for your situation.

    // this will check all td's will have any text
    // here is regex example regexr.com/6jmi6
    cy.contains('td', /\w/g).should('be.visible')