question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do we iterate the multiple data from JSOIN file?

See original GitHub issue

I have JSON file as my test data which has multiple Iteration. It is throwing an error when executing multiple iterations. Could you please guide me here?

Below is my code.

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

describe('Testsuite', function()
{
    beforeEach(function()
    {
        cy.fixture('WebshopDatatable/MultipleDataGiftcards').then((TestData) =>
        {
            this.data = TestData
        })
    })

    it('AddingGiftCards', function()
    {
        //Launch the url
        cy.visit('http://demowebshop.tricentis.com/')

        // Enter the login details
        cy.get('.ico-login').click()

        cy.get('#Email').type(this.data.email)
        cy.get('#Password').type(this.data.password)
        cy.get('.button-1.login-button').click()


    })

})

JSON Test Data:

{

    "Gift Cards": [
        {
            "url": "http://demowebshop.tricentis.com/",
            "email": "boy@mail.com",
            "password": "Boy123",
            "categories": "Gift Cards",
            "orderby": "Price: Low to High",
            "pagesize": "4",
            "viewmode": "List",
            "productName": "$5 Virtual Gift Card"
        },

        {
            "url": "http://demowebshop.tricentis.com/",
            "email": "boy@mail.com",
            "password": "Boy123",
            "categories": "Gift Cards",
            "orderby": "Price: Low to High",
            "pagesize": "4",
            "viewmode": "List",
            "productName": "$5 Virtual Gift Card"
        },

        {
            "url": "http://demowebshop.tricentis.com/",
            "email": "boy@mail.com",
            "password": "Boy123",
            "categories": "Gift Cards",
            "orderby": "Price: Low to High",
            "pagesize": "4",
            "viewmode": "List",
            "productName": "$5 Virtual Gift Card"
        }

    ]

}

Error:

undefined error

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
senthkumcommented, May 4, 2020

Hi Mate, please find the code below, I was able to fix it.

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

describe('Testsuite', function()
{
     before(()=>
     {
         cy.fixture('WebshopDatatable/MultipleDataGiftcards.json').as('gifts') 
    })
    it('AddingGiftCards', function()
    {
        cy.log(`There are ${this.gifts.GiftCards} .`)
        const gCards = this.gifts

         cy.get(gCards).each((giftobject) => 
         {
              //Write all your business logic test case code here.

         }
   })
})

Thanks Senthil.M

1reaction
senthkumcommented, Jun 2, 2020

Hi All,

Have you created a .json file in the fixture folder? If yes

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

describe('Testsuite', function()
{
     before(()=>
     {
//importing the son file and saving to an alias name
         cy.fixture('WebshopDatatable/MultipleDataGiftcards.json').as('gifts') 
    })
    it('AddingGiftCards', function()
    {
 //Using the alias name to this keyword, So we can use globally  
        const gCards = this.gifts
//looping your .json data with a new variable giftobject
         cy.get(gCards).each((giftobject) => 
         {
              //Write all your business logic test case code here.
        cy.get('#Email').type(giftobject.username )
        cy.get('#Password').type(giftobject.password)
        cy.get('.button-1.login-button').click()  
         }
   })
})

It should defiantly work…

Thanks Senthil.M

Read more comments on GitHub >

github_iconTop Results From Across the Web

Iterating through multiple objects in JSON file - Stack Overflow
If you have a JSONArray of JSONObjects of your Offer JSON (which you would if you add the brackets like I suggested), then...
Read more >
How do I loop through a JSON file with multiple keys/sub-keys ...
How do I loop through a JSON file with multiple keys/sub-keys in Python? - You can parse JSON files using the json module...
Read more >
How to Loop Through the Array of JSON Objects in JavaScript
This tutorial will guide you on how to loop the array of JSON objects in JavaScript. We'll explain the types of loops and...
Read more >
How to iterate and process data in different arrays/objects of a ...
How to iterate and process data in different arrays/objects of a JSON file? Hi community,. given following example: {.
Read more >
Python Parse multiple JSON objects from file - PYnative
To parse a JSON file with multiple JSON objects read one JSON object at a time and Convert it into Python dict using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found