Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

124
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda