Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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 function
it('Does not do much!', function() { // function here gets correct 'this'
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)
or you can move the fixture closer to the tests (less optimal but overall not by much)
it('Does not do much!', () => {
cy.fixture("DataFile").then((data) => {
cy.visit('https://rahulshettyacademy.com/angularpractice/')
cy.get("form input[name='name']").type(data.key.Name)
cy.get("form input[name='email']").type(data.key.Email)
cy.get("#exampleInputPassword1").type(data.login.key.Password)
cy.get('select').select(data.key.Gender)
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.