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

246
Visualizações
How to get the sum of a list of numbers stored in a Cucumber Feature File data table in Cypress framework?

In my Cucumber/Cypress test framework, I am trying to add a list of numbers together. They are defined in the below Feature File:

 When add the following pokemon to my cart
     | pokemonName | price |
     | bulbasaur   | 64    |
     | ivysaur     | 142   |
     | venusaur    | 263   |

Here is my Step Definition:

When('I add the following pokemon to my cart', (dataTable) => {
  let totalAmount;
  dataTable.hashes().forEach(elem => {
    totalAmount += elem.price
  });
  cy.log(totalAmount)
});

Actual Result: When I run this test, the totalAmount value logged is 64142263.

Expected Result: I want the actual result to be the sum of the 3 prices - 469

I understand what the code is doing is appending the values together as strings, but I want to know how I can treat these values as numbers, so I can get the sum of them combined.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Cypress is most likely returning the text as a string instead of a number. When using += with a string, it will addend the string to the current string ('123' += '456' => '123456') instead of actually adding the numbers.

You could simply cast the text returned to a Number, if you did not want to use the unary plus operator.

...
totalAmount += Number(ele.price)
...

If picking this approach, it would probably be helpful to define a type for totalAmount

about 4 years ago · Juan Pablo Isaza Relatório

0

I never used Cucumber/Cypress but it looks like your totalAmount is the concatenation of your price values (=> price is considered as a string). You should try using the unary plus operator :

totalAmount = +totalAmount + +elem;
about 4 years ago · Juan Pablo Isaza 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