vasundhara
vasundhara

Reputation: 109

Unable to get the "Text" of the "element" using "Protractor"

Hi I am writing Protractor automation scripts for "Angular 4" application .

The development code is below.

<!--Byndd Title div-->
            <div class="ui-g-12 ui-md-8 ui-lg-10">
                <div class="bynndTitleClass"  i18n="@@introductionBynddHeader">
                    BynddYour Information Together And CONNECT to the world  in an EFFICIENT way
                </div>
            </div>   

my protractor test script is below.

Page Object code :

//to return login page heading 
    getLoginPageMainHeading(){
              return element(by.css('div[class="bynndTitleClass"]')).getText();
    }

Spec code :

 //Testcase1 : To open the "Login page" of the application 
    it('should open the Login page' , ()=>{
        //To open the Login page
        page.loginPageDisplay();

        // to verify whether the Current page is Login page or not by comparing with Login page Main heading
        expect(page.getLoginPageMainHeading().toString()).toBe('BynddYour Information Together And CONNECT to the world  in an EFFICIENT way');
    });

After execution it is displaying below error message. W

1) should test certificate tab should open the Login page
  - Expected '[object Object]' to be 'BynddYour Information Together And CONNECT to the world  in an EFFICIENT way'.

Can anyone help me how to solve this issue

Upvotes: 0

Views: 499

Answers (2)

vasundhara
vasundhara

Reputation: 109

This solves my issue

page.getLoginPageMainHeading().getText().then(function(value){
            expect(value).toEqual('BynddYour Information Together And CONNECT to the world in an EFFICIENT way');
        })

Upvotes: 0

Ernst Zwingli
Ernst Zwingli

Reputation: 1422

Because expect() resolves a promise itself, you're on the right path to return element.getText() within your pageObject.

However, because you added .toString(), your expect command now puts the element to String rather than resolving the promise.

I'd suggest to return just the element, and apply the getText() within your expect statement. It makes more sense and keeps your pageObject function better re-usable for other tests as well.

So here my suggestion:

PageObject (return just the element without getText()):

//to return login page heading 
getLoginPageMainHeading(){
          return element(by.css('div[class="bynndTitleClass"]'));
}

and Spec (getText() instead of toString()):

//Testcase1 : To open the "Login page" of the application 
it('should open the Login page' , ()=>{
    //To open the Login page
    page.loginPageDisplay();

    // to verify whether the Current page is Login page or not by comparing with Login page Main heading
    expect(page.getLoginPageMainHeading().getText()).toBe('BynddYour Information Together And CONNECT to the world  in an EFFICIENT way');
});

Upvotes: 1

Related Questions