I am a newbie in Cypress. I want to know if there is any way to generate a dynamic payload by substituting values of JSON file with the values generated programmatically in cypress test. Something we do in Rest assured by substituting %s of JSON file. I searched a lot online but could not find it. Some of the questions which were on similar lines were This didn't work for me I want to pass a dynamic json body to the cypress request() function & define payload values
I have following json file
{
"name": "Ganesh vishal kumar",
"gender": "Male",
"email": "ganesh.kumar234@test.org",
"status": "active"
}
I want a dynamic JSON file fixture. In my below test, I am generating an email ID programmatically and using it directly in the JSON body. Here I want to use JSON file fixture
it('first post test', () => {
var pattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var emailId = "";
for (var x = 0; x < 10; x++) {
emailId = emailId + pattern.charAt(Math.floor(Math.random() * pattern.length));
}
emailId = emailId + '@test.org'
cy.request({
method: 'POST',
url: 'https://gorest.co.in/public/v2/users',
body: {
"name": "Ganesh Kumar",
"gender": "Male",
"email": emailId,
"status": "active"
},
headers: {
'authorization': 'Bearer 18806c8605b08cabb3c9ce642cbc3a21e1a8942a96c3b908a7e0e27c3b5cf354'
}
}).then((res) => {
expect(res.status).to.eq(201)
})
})