Home > Back-end >  how to trim extra spaces from an selected locator value and convert it to integer so assertion of gr
how to trim extra spaces from an selected locator value and convert it to integer so assertion of gr

Time:07-22

i have the grid which has html as follow

<div >
  <div >
          <div >
              <div >
                  <h3>Successfull</h3>
              </div>
        </div>
    <div >
      <div ></div>
      <div ><h5>Before</h5></div>
      <div ><h5>After</h5></div>
    </div>
    <div >
        <div >Status</div>
        <div ><h5>3</h5></div>
        <div ><h5>50</h5></div>
    </div>
    <div >
      <div >Result</div>
      <div ><h5>loose</h5></div>
      <div ><h5>win</h5></div>
    </div> </div> </div>

and look like this enter image description here

i am trying to get data for status before and after and wanted to make the

assertion

as

3 is greater than zero
and 
69 is greater than 65 

I don't want to use the net-child() in finding the locator as i am trying to perform it on cypress syntax only

my effort

but getting this error

cy.get(.three).contains('Status').parent().should('have.text',0);



 get  .three
    -contains status
    -parent
    -next
    -assert expected '<div.column>' to have text '0' but the text was ' 3 '

i.e. it is returning wide space before and after 3

when i tried to convert it into an integer to cater my requirement

cy.get(".three").contains('Status').parent().invoke('val').then(parseFloat).should('be.gte',0);

 get  .three
    -contains status
    -parent
    -next
    -assert expected NaN to be at least 0

also is there any better way of traversing through the required locator?

CodePudding user response:

If your text is always returning something like ' 3 ', then you can easily use .trim() to remove whitespace before converting the string to a number.

cy.get('.three').contains('Status').parent().then(($el) => {
  const stringVal = $el.text().trim(); // turns ' 3 ' into '3'
  const numVal =  stringVal; // turns '3' into 3
  // the above two lines could be combined into a single line
  expect(numVal).to.be.greaterThan(0);
});

CodePudding user response:

You can do something like this:

cy.get('.three')
  .contains('Status')
  .parent()
  .within(() => {

    //To validate as text
    cy.get('.column').eq(1).should('have.text', '3')

    //To validate as a number
    cy.get('.column')
      .eq(1)
      .invoke('text')
      .then((text) =>  text.trim())
      .should('be.gte', 0)
  })
  • Related