Home > front end >  Cypress TS, how to get second text that wrapped double quotes?
Cypress TS, how to get second text that wrapped double quotes?

Time:09-04

I got this web page to test claim number. But I am not able to extract the same.

<h1  style="text-transform: capitalize; display: flex; align-items: center; margin-bottom: 0.5rem;">
"Claims #"
"75078"
</h1>

The xpath I am trying to get is

export const getClaimsHeader = () => cy.xpath("//h1[contains(@class,'undefined')]")

This is the code I am trying to get the claim Number

  getClaimsHeader()
  .each((header) => {
    claimNum = header.toString().split('#');
    actualClaimNumber = claimNum[1];
    console.log('header text', actualClaimNumber);
  })
  .then(() => {
    expect(claimNumber).to.equal(actualClaimNumber);
  });

But actualClaimNumber is displayed as blank because header.toString() is returning only "Claims #"

CodePudding user response:

You have to extract the inner text and then apply the split. Something like this:

claimNum = header.text().split('#');

CodePudding user response:

It looks like you nearly have it, but you would need to remove the double-quotes as well since they are part of the text.

Also add trim() to remove any linefeed that may be there.

Lastly, in your sample code expect(claimNumber).to.equal(actualClaimNumber) will never pass because actualClaimNumber is calculated from claimNumber

const actualClaimNumbers = ['75078', ...];

getClaimsHeader()
  .each((header, index) => {
    const claimNumber = header.text().split('#')[1].replace(/"/g, '').trim()

    console.log('header text', claimNumber);

    expect(claimNumber).to.equal(actualClaimNumbers[index])
  })
  • Related