0

I am following page page object model in cypress. So get the value from getName() created object on class MHome and call the function getName() but it returns undefined

Object class

Class MHome{
    const loc = locator-A
    const loc1 = locator-B
     getName(){ 
         cy.getTeam2Home(loc, loc1).then((value)=>{
            // cy.log(value.toString())
             cy.wrap  (value.toString());
         })
    }
}

Calling the getName () function in cypress test case:

const Sh = new MHome();
And ("verify title of elements, function (){
  cy.log (Sh.get.Name())
})

cy.log (Sh.get.Name()) returns undefined . Kindly help me in

2 Answers 2

1

You seem to have a lot of typos all around, a proper ts syntax would be:

class MHome {
    loc:string
    loc1: string
    constructor() {
        this.loc = "locator-A"
        this.loc1 = "locator-B"
    }
    getName() {
        return cy.getTeam2Home(this.loc, this.loc1).then((value)=>{
            // cy.log(value.toString())
            cy.wrap(value.toString());
        })
    }
}

And the instantiate the class and call the method like:

const Sh = new MHome()
Sh.getName().then((value)=> { cy.log(value) }
Sign up to request clarification or add additional context in comments.

1 Comment

Sh.getName() shows void as return type even after after specifying return in getName() method. Please what is mistake ?Thanks
0

Thank you guys your comments help me lot, problem sloved by using cypress.promise commands :

Class MHome{
    const loc = locator-A
    const loc1 = locator-B

     getName(){ 
       return new Cypress.Promise((resolve, reject) => {
         cy.getTeam2Home(loc, loc1).then((value)=>{
            // cy.log(value.toString())
             resolve(value.toString());
         })
    }
}

And callig functions :

const Sh = new MHome();
And ("verify title of elements, function (){
  Sh.getName().then((val)=>{
            cy.log(val+'----ddd---');
        })
})

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.