Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

238
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda