Linx
Linx

Reputation: 763

How to console text in an element using Cypress?

I am using Cypress for testing. I want to make sure that certain data is collected when on the page, but that does not show in the html. I have added hidden html elements for this purpose, which looks like this:

<div class="hidden">
    <p id="c.campaign">3005</p>
</div>

Now I want to use my Cypress test to make sure that there is a campaign saved to the page. This is what I have so far:

cy.get('#c.campaign').should('not.be.visible').invoke('text').then((text) => {
    cy.log(text)
})

The should not be visible is necessary as the element it hidden and does not find it otherwise. It should be logging 3005 but nothing is appearing. failed test

Upvotes: 1

Views: 5396

Answers (1)

Linx
Linx

Reputation: 763

Turns out the problem was having my classes named with a '.'. I believe that was breaking the jquery text function. I changed my classes to this format

<p id="test-campaign">3005</p>

and this code now works.

  cy.get('#test-campaign')
    .should('not.be.visible')
    .invoke('text').then((text) => {
      campaign = text
      cy.log('campaign',  text)
    })

Upvotes: 2

Related Questions