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

333
Visualizações
How to use Cypress datatables to loop through multiple test scenarios?

I am trying to use datatables to test one input field in my Cypress scenario below

 Scenario: The one where the user enters a value to calculate the factorial
     Given the user navigates to the Factoriall calculator screen
     When the user enters a value into the input box
         | value    |
         | 12       |
         | 34.56    |

     And the user clicks the Calculate button
     Then a message is displayed to the user
         | message                           |
         | The factorial of 12 is: 479001600 |
         | Please enter an integer           |

Here is the current testing behaviour:

  1. 12 is entered into the input box.
  2. 34.56 is entered into the input box.
  3. The Calculate button is clicked.
  4. The message is validated ("Please enter an integer" is displayed) as the input reads 1234.56.

This is how I want the test to behave:

  1. 12 is entered into the input box.
  2. The Calculate button is clicked.
  3. The factorial value is displayed.
  4. The input box is cleared.
  5. 34.56 is entered into the input box.
  6. The Calculate button is clicked.
  7. The error message is displayed.

I understand there's an issue with my current logic in that it's entering all the text at the beginning before clicking the button, but can someone please tell me what updates are required so I can get this test working as expected?

Here are my Step Definitions:

When('the user enters a value into the input box', (userInputs) => {
    userInputs.hashes().forEach((userInput) => {
        myValue = userInput.value
        factoriall.getInputBox().type(myValue)
    });
});

When('the user clicks the Calculate button', () => {
    factoriall.getBtnCalculate().click();
});

Then('a message is displayed to the user', (expectedMessages) => {
    expectedMessages.hashes().forEach((expectedMessage) => {
        factoriall.getInputBox().clear();
        myMessage = expectedMessage.message
        factoriall.getResultParagraph().should('have.text', myMessage)
    });
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It would be best to use a Scenario Outline.
Using this, your entire scenario will be executed 2 times (or the # of rows you add in your table under Examples) and each time, the words with angle brackets <value> and <message> will be replaced by their corresponding value from the Examples: table.

Scenario Outline: The one where the user enters value <value> to calculate the factorial
     Given  the user navigates to the Factoriall calculator screen
     When   the user enters value <value> into the input box
     And    the user clicks the Calculate button
     Then   this message is displayed to the user: <message>
     Examples:
         | value | message                           |
         | 12    | The factorial of 12 is: 479001600 |
         | 34.56 | Please enter an integer           |

To use the values <value> and <message>, your need to call your step like this:

When(/^the user enters value (.+) into the input box$/, (value) => {
    ...
});

Then(/^this message is displayed to the user: (.+)$/, (message) => {
    ...
});

In my example above, I use regular expressions to capture my variables. There are other ways, but I prefer this one.

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