'Typescript/cypress-cucumber-preprocessor : Writing a Scenario Outline with dynamic examples

My question is very similar to this one: Behave: Writing a Scenario Outline with dynamic examples. The difference is that I do not use Python. I handle my Gherkin scenarios with Cypress (through the cypress-cucumber-preprocessor library : https://github.com/TheBrainFamily/cypress-cucumber-preprocessor).

Let say I have this scenario outline (written in my Jira):

Given I provide <a list of numbers> and <another list of numbers>

Then I check wether they are equals

Examples:
| list of numbers  | another list of numbers | 
| 1                | 1                       | 
| 100              | 200                     | 

I want to set my numbers dynamically because I will receive them from a REST call. Is there a way to do that?

In Python with behave, it seems that it is possible to do so with a before_feature().

The scenario would be like that:

Given I provide <a list of numbers> and <another list of numbers>

Then I check wether they are equals

Examples:
| list of numbers  | another list of numbers | 
| .                | .                       | 

But I don't know how to iterate on my examples to set them. Is it possible?



Solution 1:[1]

Ideally the tests should not be complex and their result should be fixed and expected. So you can mock the service call to return response as per your tests.

However, just for your solution, You can use some holder that you can replace before you start tests. Eg

Given I provide <a list of numbers> and <another list of numbers>

Then I check wether they are equals

Examples:
| list of numbers  | another list of numbers | 
| %A%              | %B%                     | 

Write the code to replace the holder's value with the response you receive from the REST API call

//..
const contents = fs.readFileSync("path/of/file.feature").toString();
contents.replace("%A%", "23");
//..

Solution 2:[2]

yes it's possible! :D

Feature file:

Given I provide <a list of numbers> and <another list of numbers>

Then I check wether they are equals

Examples:
| list of numbers  | another list of numbers | 
| .                | .                       | 

Test file:

import { Given, When, Then, And } from 'cypress-cucumber-preprocessor/steps'

Given("I provide {} and {}", (aListOfNumbers, anotherListOfNumbers) => {
   // You can pass value from step to step using .as('something')
});

see docs here for cypress .as method

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Amit Kumar Gupta
Solution 2 Ian