When I tried to pull data from fictures into my test case I got an Cannot read property ‘key’ of undefined
error message.
Code:
describe(‘My First Test’, () => {
beforeEach(function() {
cy.fixture("DataFile").then((data) => {
this.key = data
})
})
it('Does not do much!', () => {
cy.visit('https://rahulshettyacademy.com/angularpractice/')
cy.get("form input[name='name']").type(this.key.Name)
cy.get("form input[name='email']").type(this.key.Email)
cy.get("#exampleInputPassword1").type(this.login.key.Password)
cy.get('select').select(this.key.Gender)
})
Error:
Cannot read property ‘key’ of undefined
To access properties of
this
, use a function instead of an arrow functionor you can move the fixture closer to the tests (less optimal but overall not by much)