1

I am working on cypress for web application automation and having an issue with accessing a function from an object.

The code below is from a javascript file in 'e2e' folder.

    class productsDemo {
        pageObjects = {
            productName: () => cy.get('.product-name-input'),
            productDescription: () => {
                cy.get('.product-description-input');
            },
            productCode: () => cy.get('.product-code-input')
       }
    
        inputProdName(prodname) {
            this.pageObjects.productName().type(prodName);
        }
        
        inputProdDesc(proddesc) {
            this.pageObjects.productDescription().type(proddesc);
        }
    }
    
    module.exports = new productsDemo();

The code below is from a cypress test file to import and use the class.

    import productsDemo from '../pageClasses/products.js'
    
    describe('Product Creation', () => {
        it('Create new Product', () => {
            cy.visit('http://localhost:3000/products');
            
            productsDemo.inputProdName('Laptop');
            productsDemo.inputProdDesc('Electronics');
            productsDemo.pageObjects.productCode().should('have.text', 'XXX124');
        }
    })

While running this file, getting this error in test runner.

Cannot read properties of undefined (reading 'type')

And, this type error is showing in the javascript file for 'inputProdDesc' function.

If the function declaration in 'pageObjects' object from the javascript file is modified as follows, then it works without problems:

    productDescription: () => cy.get('.product-description-input'),

Could anyone help me to understand what is the issue with this code in setting value for 'product description' and how and to resolve this?

    productDescription: () => {
        cy.get('.product-description-input');
    },      

1 Answer 1

0

You have simply missed out the return statement from your page object class method call.

This is how it should look

productDescription: () => {
  return  cy.get('.product-description-input');
},      

Without that return statements the code thinks you are returning undefined and that is the error message it is giving you:

Cannot read properties of undefined

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.