Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
how to trim extra spaces from an selected locator value and convert it to integer so assertion of greater than is performed over it in cypress?

i have the grid which has html as follow

<div class="grid-container">
  <div class="ui three column grid">
          <div class="row">
              <div class="fourteen wide column">
                  <h3>Successfull</h3>
              </div>
        </div>
    <div class="row">
      <div class="column"></div>
      <div class="column"><h5>Before</h5></div>
      <div class="column"><h5>After</h5></div>
    </div>
    <div class="row">
        <div class="column">Status</div>
        <div class="column"><h5>3</h5></div>
        <div class="column"><h5>50</h5></div>
    </div>
    <div class="row">
      <div class="column">Result</div>
      <div class="column"><h5>loose</h5></div>
      <div class="column"><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?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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);
});
about 4 years ago · Santiago Trujillo Report

0

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)
  })
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!