0

I'm new to cypress, so I apologize if I make no sense here. i have a cypress script that does a POST request. My end goal is to check API validations. whether API responds with correct error messages for a given JSON body. for that, I want to pass the same JSON body with different values to the cypress request function.

I have my JSON object in a different js file. (channel_query.js)

export const CreateChannel = {
"name": "channe Name",
"tagline": "tasdsadsaest",
"date": "03 Mar 2021",
"time": "2.00 p.m",
"beginsOn": "2021-03-04T13:59:08.700",
"expiresOn": "2021-05-28T14:54:08.700",
"description": "sample Descritptin",
"url": "www.google.com"}

I have my cypress request in the integration folder (channel.js)

import { CreateChannel } from '../queries/channel_query';
it('Create a channel',function() {
    cy.request({
        method: 'POST',
        url: '{my URL}',
        body: CreateChannel ,
        headers: headers
        }).then((response) => {
            expect(response.status).to.eq(201)
            expect(response.body.name).to.eq(CreateChannel.name)
    })
}) })

My question is,

How to make values in JSON object dynamic & then define them in the cypress request function? so I can pass the same JSON to check different validations.

@Mr. Gleb Bahmutov

Help is much appreciated guys!

Richard Matsen
  • 20,671
  • 3
  • 43
  • 77
heeram
  • 1
  • 5

1 Answers1

0

The simplest way might be to place an array of channels in the JSON file and make the test data-driven.

export const channelData = [
  {
    "name": "channe Name",
    ... // plus other properties
  },
  {
    "name": "name2",
    ... // plus other properties
  },
]

The test

import { channelData } from '../queries/channel_query';

describe('Test all channels', () => {

  channelData.forEach((channel, index) => {

    it(`Testing channel "${channel.name}"`, function() {
      cy.request({
        method: 'POST',
        url: '{my URL}',
        body: channel,
        headers: headers
      }).then((response) => {
        expect(response.status).to.eq(201)
        expect(response.body.name).to.eq(channel.name)
      })
    }) 
  })
  • my JSON body is a little big one. so for this approach, i have to maintain big JSON objects. so is there a way to import each JSON object & add some key-value pair within the test before attaching it to the cy. request function?. basically modify the json object before add it to the request in channel.js file ? @steve Zodiac – heeram Mar 18 '21 at 12:55