2022-04-30

How to get text from the DOM element in cypress

I am new in cypress, I want to create a dynamic method that returns the text of whatever DOM element pass on it, so I have created one but it returns some unexpected result please see the below code and suggest to me where I am doing wrong or what is the best option for achieving this task.

login_objrepo.json

{
  "Signin_lbl":".login100-form-title.p-b-10"  //Locator
}

Login.sepc.js

import commonUtility from "../../support/commonUtility";
const util = new commonUtility();
const objLogin = require('../../fixtures/login_objrepo');

    describe('Login Page', function () {
        it('Verify Page', () => {
            util.openUrl(objLogin.URL);
            const exp = 'Sign In';
            const act = util.getText(objLogin.Signin_lbl);
            cy.log("Exp title=" + exp + " and Act=" + act)
            cy.get(objLogin.Signin_lbl).should('have.text',exp);
        })
    })

commonUtility.js

 class commonUtility
    {
     getText(locator)
        {
            cy.wait(3000);
            
            cy.get(locator).then(($attribute)=> {
                let txt=$attribute.text();
                cy.log("Retun Text is = "+ txt);
            })
            return this;
        }
    }

here (txt variable) got the valid text but, in the above file(Login.sepc.js) were to call it there showing ([object Object])... how to get the same here also?

========================== OUTPUT

enter image description here



No comments:

Post a Comment