1

my problem statement is i am getting a text out of element something like this "stock name: apple inc","stock id: 23244"

i wanted to extract the text portion after ":" colon

my effort

cy.get(".column:nth-child(1)).find('li').eq(0).should('have.text','apple') //returning "stock name: apple inc" instead of "apple inc" only
cy.get(".column:nth-child(1)).find('li').eq(1).should('have.text','23244') //returning "stock id: 23244" instead of "23244" only
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

2

If you want to assert just the portion of the text you can use include.text instead of have.text

cy.get('.column:nth-child(1)').find('li').eq(0).should('include.text', 'apple')
cy.get('.column:nth-child(1)').find('li').eq(1).should('include.text', '23244')

You can use th split to extract the text and assert it as well.

cy.get('.column:nth-child(1)')
  .find('li')
  .eq(0)
  .invoke('text')
  .then((text) => {
    cy.log(text.split(':')[1]) //prints apple inc
    expect(text.split(':')[1]).to.equal('apple inc')
  })

cy.get('.column:nth-child(1)')
  .find('li')
  .eq(1)
  .invoke('text')
  .then((text) => {
    cy.log(text.split(':')[1]) //prints apple 23244
    expect(text.split(':')[1]).to.equal('23244')
  })
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Alapan Das
  • 17,144
  • 3
  • 29
  • 52
  • thanks for your prompt response, i need one more help can you pls suggest me some good resource/course for leaning cypress and javascript – Anil Tiwari Jul 13 '22 at 11:51
  • I guess the example test files that comes with cypress installation are a good way to learn. Another good resource is glebs blog https://glebbahmutov.com/blog/tags/cypress/. Apart from that I have written few blogs on cypress if you want can check them as well https://github.com/alapanme/Cypress-Automation. For js I would suggest w3schools. – Alapan Das Jul 13 '22 at 11:55