I am new to using Cypress and I'm wondering if there is a way to generate a dynamic payload by replacing values in a JSON file with values generated programmatically in a Cypress test. This is similar to what we do in Rest Assured by substituting %s in the JSON file.
I have tried searching online but couldn't find a solution. Some similar questions that I came across were: I want to pass a dynamic JSON body to the cypress request() function & define payload values
Below is an example of a JSON file:
{
"name": "Ganesh vishal kumar",
"gender": "Male",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b3c3a353e283375302e363a2969686f1b2f3c">@example.com</a>",
"status": "active"
}
In the following test, I am generating an email ID programmatically and using it directly in the JSON body. However, I would like to use a JSON file fixture instead.
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)
})
})