-2

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)
        })
    })

nikhil udgirkar
  • 367
  • 1
  • 7
  • 23
  • 7
    Does this answer your question? [How can I merge properties of two JavaScript objects dynamically?](https://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically) – TesterDick Jul 27 '23 at 07:50
  • @TesterDick No it does not – nikhil udgirkar Jul 27 '23 at 14:02

2 Answers2

5

Would you not just do the same thing, but using fixture data?

So here we rely on the mixing of properties of an object using a spread operator and then adding our brand new properties at the end of the list.

Please reference the following page to get an understanding: Spread syntax (...)

  ...
  let emailId =  .. // calculation for email

  cy.fixture('my-fixture-file-that-has-most-of-the-data-required.json')
    .then(fixtureData => {
      const body = {...fixtureData, email: emailId}
      cy.request({
        ..
        body,              // add the body variable from above line
        headers: {
          ...
      })
Pi.
  • 153
  • 6
  • What if there were nested JSON like the following { { "data": [ { "stuff": [ { "onetype": [ { "name": "Ganesh vishal kumar", "gender": "Male", "email": "ganesh.kumar234@test.org", "status": "active" }, { "name": "Amit kumar", "gender": "Male", "email": "amit.kumar234@test.org", "status": "active" } ] } ] } ] } – nikhil udgirkar Jul 27 '23 at 03:58
0

You can use this plugin

Very easy to configure, and gives you almost all faker capabilities in json fixture files, like this:

{
    "name": "Using fixtures to represent data ${faker.string.uuid()}",
    "email": "${faker.internet.email()}",
    "body": "Fixtures are a great way to mock data ${faker.string.uuid()} for responses to routes",
    "int": "${faker.number.int()}",
    "intParam": "${faker.number.int(100)}",
    "intObjectParam": "${faker.number.int({ min: 10, max: 15 })}",
    "string": "${faker.string.numeric()}",
    "airline": "${faker.airline.flightNumber({ addLeadingZeros: true })}",
    "airline2": "${faker.airline.flightNumber({ length: { min: 2, max: 3 } })}",
    "color": "${faker.color.colorByCSSColorSpace({ format: 'css', space: 'display-p3' })}",
    "boolean": "${faker.datatype.boolean(0.9)}",
    "between": "${faker.date.between({ from: '2029-01-01T00:00:00.000Z', to: '2030-01-01T00:00:00.000Z' })}",
    "amount": "${faker.finance.amount({ min: 5, max: 10, dec: 5, symbol: '', autoFormat: true })}",
    "arrayElement": "${faker.helpers.arrayElement(['cat', 'dog', 'mouse'])}"
}
Zaista
  • 1,272
  • 8
  • 18