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
|
Show 4 more comments
4 Answers
Reset to default 6The 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
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