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');
},