I'm struggling to read data from a JSON fixture file in Cypress for my test cases. Despite following various instructions and examples, I can't seem to make it work. Any ideas on what I might be doing wrong?
Below are the details of my files along with the relevant code snippets.
TEST CASE:
describe('Test for user data via JSON', ()=> {
// Utilize cy.fixture() method to retrieve data from fixture file
before(function () {
cy.fixture('users').then(function (userData) {
this.userData = userData;
})
})
it.only('Login Test. User1', () => {
cy.wait(500)
cy.visit('http://localhost/')
cy.wait(500)
onLoginPage.usernameInput(this.userData[0].username)
onLoginPage.passwordInput(this.userData[0].password)
onLoginPage.buttonGo()
})
})
JSON FILE
File Name: users.json
[
{
"id": 0,
"username": "User1",
"password": "password1",
"_comment": "All rights"
},
{
"id": 1,
"username": "User2",
"password": "password2",
"_comment": "All rights"
},
{
"id"quot;: 2,
"quot;username"quot;: ""User3",
quot;password"": ""password3",
quot;_comment": ""Reading only"
}
]
The error message indicates: "Cannot read properties of undefined (reading 'users')" And points out the issue in this line of code:
onLoginPage.usernameInput(this.**u**serData[0].username)
The fixtures folder is located at: "../fixtures/users"
This should have been straightforward based on the examples I've seen. Can someone help me figure out what's missing here? I appreciate any guidance provided.
Thank you!