I’m a bit new with cypress so I’m having a trouble with this one.
I have this TestData.json file in fixtures:
{
"data1":
{
"Client": "Client1",
"ClientID": "202300001",
"PlanTypes": ["type1", "type2"]
},
"data2":
{
"Client": "Client2",
"ClientID": "202300001",
"PlanTypes": ["type3", "type4", "type5"]
}
}
Then the site that I’m working on have a dropdown list that have items like this:
Client Plan Types: "", "type1", "type2", "For Validation" ("" and "For Validation" is like a default items on the dropdown while type1 and type2 is dynamic it changes per client)
Now what I did was to get these items and transfer it to an array "arr1" which I did not have a problem.
What I am having trouble with is comparing arr1 with PlanTypes (from fixtures). Also, I needed to add "" and "For Validation" values in PlanTypes (without adding it to the fixture file itself) first before comparing it to arr1.
Here is what my code looks like:
script.js
import filePage from "../../../support/page-objects/filePage";
const filePage = new filePage()
const testDataFile = '/TestData.json'
cy.fixture(testDataFile).then((testData) => {
const msTestData = testData.data1
filePage.checkPlanTypes(msTestData);
});
filePage.js
checkPlanTypes(msTestData) {
let arr1 = []
this.elements.dataPlanTypeList().find('li').each((planTypeData) => {
arr1.push(planTypeData.text().trim())
}).then(() => {
let clientPlanTypes = []
clientPlanTypes.push("")
clientPlanTypes.push(msTestData)
clientPlanTypes.push("For Validation")
expect(clientPlanTypes).to.have.same.members(arr1)
})
}
When I try to run it, I get this error "expected [ Array(4) ] to have the same members as [ Array(3) ]".
I also tried "cy.wrap(arr1).should('deep.equal', clientPlanTypes)
" but also have error "expected [ Array(4) ] to deeply equal [ Array(3) ]"
When I cy.log(clientPlanTypes)
it logs "[, [REGULAR, VIP], For Validation]"
Any ideas on how I can compare PlanTypes(clientPlanTypes) and arr1? Any help is greatly appreciated. Thanks!
2
Answers
I think the issue is coming from the line where you are adding the data from the fixture to the new array:
In doing this, you are adding an array (
msTestData
) to the new array (clientPlanTypes
) instead of adding the members of the fixture array to the new array. This is why yourcy.log
prints out[, [REGULAR, VIP], For Validation]
vs. your expected of["", REGULAR, VIP, For Validation]
I think you could resolve this by iterating through the fixture array and individually adding the members.
Pushing an array into an array gives you a nested array
You can use the flat method to fix it