javascript - How we can import the new changes in json file made by previous test? - Stack Overflow

I have a spec file where I have to loop through the it block to create orders, and storing them in a Js

I have a spec file where I have to loop through the it block to create orders, and storing them in a Json object of array, Then in same spec file I have to validate the orders create in previous it block and loop through the it block as I did for order creation.

But for validation code it block doesn't execute as PO_Array is async and check for execution even before data fell into Array. Below is my pseudo code

import DuplicateExistingNewOrderStyle from "../../fixtures/DuplicateExistingNewOrderStyle.json"

describe("Create Orders", () => {


    let PO_Array = []
    var object1 = {}

    DuplicateExistingNewOrderStyle.forEach((DuplicateStyleData, index) => {


        it(`Create ${DuplicateStyleData.brand} order for dup ExistingStyle`, () => {


            object1 = {
                brand: DuplicateStyleData.brand,
                purchaseOrder: Date.now()
            }
            PO_Array.push(object1)
            cy.writeFile(`cypress/POcsv/dataform1.json`, PO_Array)  

        })
    })

   
  PO_Array.forEach(orderData => {
        it(`${orderData.brand} Validate order test`, () => {
            cy.log(`${orderData.brand}`)
        })
    })

})

As there are two orders in json file in result of first it block My requirement is 'Validate order test' should be execute two time 'Create Nike Validate order test' 'Create Addidas Validate order test'

I have tried write and read file stuff but it doesn't work as above it block works

**Tried below second solution with hooks and read and write file but not working and return Cannot read properties of undefined (reading 'forEach') error **

describe("Create Orders", () => {
    let orderRegions, draftURL;
    
    let PO_Array=[]
    var object1={}
   

   
   
  context("test1",()=>{



    DuplicateExistingNewOrderStyle.forEach((DuplicateStyleData, index) => {
       

    it(`Create ${DuplicateStyleData.brand} order for dup ExistingStyle`, () => {
        Cypress.env("Attempt",Cypress.currentRetry)
        orderRegions = 'Rest of World'
      

                object1={brand:DuplicateStyleData.brand, purchaseOrder:Date.now()} 
                PO_Array.push(object1)
    
            
                console.log(PO_Array)
                
                   
               
        })

  

    })
    after("store info",()=>{
        cy.writeFile('cypress/fixtures/POcsv/dataform1.json',PO_Array)
    })
   
})

 
   
      context("Test 2",()=>{
        let JsonData
        before("read data",()=>{
            cy.fixture('POcsv/dataform1.json').then(data=>{
                JsonData=data

            })

        })
       
    
        JsonData.forEach(orderData=>{
            it(`${orderData.brand} test`,()=>{
                cy.log(`${orderData.brand}`)
                cy.log(`${orderData.purchaseOrder}`)
            })
        })
    })
     
})

I have a spec file where I have to loop through the it block to create orders, and storing them in a Json object of array, Then in same spec file I have to validate the orders create in previous it block and loop through the it block as I did for order creation.

But for validation code it block doesn't execute as PO_Array is async and check for execution even before data fell into Array. Below is my pseudo code

import DuplicateExistingNewOrderStyle from "../../fixtures/DuplicateExistingNewOrderStyle.json"

describe("Create Orders", () => {


    let PO_Array = []
    var object1 = {}

    DuplicateExistingNewOrderStyle.forEach((DuplicateStyleData, index) => {


        it(`Create ${DuplicateStyleData.brand} order for dup ExistingStyle`, () => {


            object1 = {
                brand: DuplicateStyleData.brand,
                purchaseOrder: Date.now()
            }
            PO_Array.push(object1)
            cy.writeFile(`cypress/POcsv/dataform1.json`, PO_Array)  

        })
    })

   
  PO_Array.forEach(orderData => {
        it(`${orderData.brand} Validate order test`, () => {
            cy.log(`${orderData.brand}`)
        })
    })

})

As there are two orders in json file in result of first it block My requirement is 'Validate order test' should be execute two time 'Create Nike Validate order test' 'Create Addidas Validate order test'

I have tried write and read file stuff but it doesn't work as above it block works

**Tried below second solution with hooks and read and write file but not working and return Cannot read properties of undefined (reading 'forEach') error **

describe("Create Orders", () => {
    let orderRegions, draftURL;
    
    let PO_Array=[]
    var object1={}
   

   
   
  context("test1",()=>{



    DuplicateExistingNewOrderStyle.forEach((DuplicateStyleData, index) => {
       

    it(`Create ${DuplicateStyleData.brand} order for dup ExistingStyle`, () => {
        Cypress.env("Attempt",Cypress.currentRetry)
        orderRegions = 'Rest of World'
      

                object1={brand:DuplicateStyleData.brand, purchaseOrder:Date.now()} 
                PO_Array.push(object1)
    
            
                console.log(PO_Array)
                
                   
               
        })

  

    })
    after("store info",()=>{
        cy.writeFile('cypress/fixtures/POcsv/dataform1.json',PO_Array)
    })
   
})

 
   
      context("Test 2",()=>{
        let JsonData
        before("read data",()=>{
            cy.fixture('POcsv/dataform1.json').then(data=>{
                JsonData=data

            })

        })
       
    
        JsonData.forEach(orderData=>{
            it(`${orderData.brand} test`,()=>{
                cy.log(`${orderData.brand}`)
                cy.log(`${orderData.purchaseOrder}`)
            })
        })
    })
     
})

Share Improve this question edited Jan 29 at 20:46 Muneeb asked Jan 29 at 11:21 MuneebMuneeb 1212 silver badges11 bronze badges 9
  • You can write to a file with docs.cypress.io/api/commands/writefile – jabaa Commented Jan 29 at 12:04
  • @jabaa I'm writing in a file aleady but not able to access on run time I think – Muneeb Commented Jan 29 at 12:29
  • You can read the file with docs.cypress.io/api/commands/readfile – jabaa Commented Jan 29 at 13:16
  • @jabaa my requirment is bit different, through write and reading it's working fine if I'm reading file in inside it block, but I have loop through the it block So it could execute according the the orders inside data file. For example 2 orders in file, 1st Addidas and second Nike, so it block should execute two times first validate Addidas order then second execute Validate Nike order – Muneeb Commented Jan 29 at 13:38
  • basically I'm writing one it block ordersData.forEach(data=>{ it(Validate ${data.brand} order,()=>{ }) }) and output should be in form of two test cases like "Validate Addidas order" "Validate Nike order" – Muneeb Commented Jan 29 at 13:41
 |  Show 4 more comments

4 Answers 4

Reset to default 6

The reason is the test isolation principle, which clears the variables between each it() call.

For a better understanding read Cypress explanation here.

So, to avoid the issue turn it off for the two it()

import DuplicateExistingNewOrderStyle from "../..

describe("Create Orders", {testIsolation:false}, () => {  <-- add switch here

The first part isn't a test, it's just file manipulation so you can change it to a beforeEach().

beforeEach(() => {
  DuplicateExistingNewOrderStyle.forEach((DuplicateStyleData) => {
    cy.log(`Create ${DuplicateStyleData.brand} order for dup ExistingStyle`)
    PO_Array.push({ 
      brand: DuplicateStyleData.brand,
      purchaseOrder: Date.now()
    })
  })
  cy.writeFile(`cypress/POcsv/dataform1.json`, PO_Array)  
})

Modify the json file after importing with a mapping function. There isn't any need to write it, but you can do it before the tests.

import DuplicateExistingNewOrderStyle from "../../fixtures/DuplicateExistingNewOrderStyle.json"

const PO_Array = DuplicateExistingNewOrderStyle.map(DuplicateStyleData => ({
  brand: DuplicateStyleData.brand,
  purchaseOrder: Date.now()
}))

before(() => {
  cy.writeFile(`cypress/POcsv/dataform1.json`, PO_Array) 
})

describe("Create Orders", () => {
  PO_Array.forEach(orderData => {
    it(`${orderData.brand} Validate order test`, () => {
      ...
    })
})

This Solution not the best but it fullfil the requirement. The requirement was Cypress should loop through for each brand and validate order data exist.

let say we have two brands 'Adidas' and 'nike' and requirement was order creation it block should execute two times as per brands count

Create Adidas order for Dup Existing style Create Adidas order for Dup Existing style

and Similarly it should execute validation test through loop iteration

Adidas Validate order test Nike Validate order test

In above question it was working fine for create script but not executing for Validation script it was not picking up data for PO_Array So I tried below solution by providing manually the brands name array but it still not good solution and better to pick brand from previous data (Array of object) created while creating orders.

import DuplicateExistingNewOrderStyle from "../../fixtures/DuplicateExistingNewOrderStyle.json"

describe("Create  Orders for all brands and validate them", () => {
    let PO_Array = []
    var object1 = {}

    context("create order",()=>{   

    DuplicateExistingNewOrderStyle.forEach((DuplicateStyleData, index) => {


        it(`Create ${DuplicateStyleData.brand} order for dup ExistingStyle`, () => {


            object1 = {
                brand: DuplicateStyleData.brand,
                purchaseOrder: Date.now()
            }
            PO_Array.push(object1)
            cy.writeFile(`cypress/POcsv/dataform1.json`, PO_Array)  

        })
    })
})
context("create order",()=>{  
    let brandsList=['Adidas', 'Nike'] 

   
    brandsList.forEach((brand ,index)=> {
        it(`${brand} Validate order test`, () => {
            cy.readFile('cypress/POcsv/dataform1.json').then(orderDetails=>{
                orderDetails=orderDetails[index]

                cy.log(`${orderDetails.brand}`)
                cy.log(`${orderDetails.purchaseOrder}`)

            })
            
        })
    })
})

})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745302391a4621513.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信