API Testing

JSON Objects Examples With Cypress

JSON (JavaScript Object Notation) is a lightweight format for storing and transporting data,

Often used when data is sent from a server to a web page and is “self-describing” and easy to understand.

Few Examples For JSON Objects

In this test, we printing to console some data were stored in several ways.

The “simpleObject” example is a regular object that contains two keys with different values.

For simpleArryOfValues we created a regular array.

In “arryOfObjects” we created an array with objects and in “typeOfData” we used string and numbers data.

The last mix example is a combination of all the different uses.

/// <reference types="cypress" />

describe('JSON Objects', () => {

    it('JSON Objects Demo ', () => {

        cy.visit('www.google.com');

        const simpleObject = { "key": "value", "key2": "value2" }

        const simpleArryOfValues = ["zero", "banana", "apple"]

        const arryOfObjects = [{ "key": "value", "key2": "value2", "key3": "value3" }]

        const typesOfData = { "string": " Hello this is a test", "number": 20 }

        const mix = {
            "FirstName": "Kobi",
            "LastName": "Konkan",
            "Age": 63,
            "Friends": [
                {
                    "firstName": "David",
                    "lastName": "Morten"
                },
                {
                    "firstName": "Natasha",
                    "lastName": "Golferberg"
                }
            ]

        }


        console.log(simpleObject.key2)
        console.log(simpleObject["key2"]) 
        console.log(simpleArryOfValues[2])
        console.log(arryOfObjects[0].key)
        console.log(mix.Friends[0].firstName)


    });
})

Here we can see the print outcome in the Console inside the browser.

That was just a small example of how JSON looks like and how we can print that data to the console with Cypress.

In the next article, we will cover Cy.Intercept

Click here to read – Cypress.io | Cy.Intercept Implementations Tutorial (Soon)

Ofir Blumental

QA Automation Engineer - ofirblumental1@gmail.com

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button