Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: element(...).then is not a function in Protractor 3.2.1

I was using following code

element(by.xpath("//tf-navpane-item[contains(@class,'tf-state-selected')]//tf-navpane-item-text//*[contains(@class,'ng-binding')]")).then(function(ele){
            ele.getText().then(function(txt){
                console.log("txt: "+txt);
            });
        }); 

This code used to work fine when I was using Protractor 1.0. After upgrading Protractor to 3.2.1 ,I started to get following error.

TypeError: element(...).then is not a function

I maybe missing something but not sure what.

like image 217
Murali Krishna Avatar asked Dec 24 '22 06:12

Murali Krishna


1 Answers

Yeah, this is something you should expect since the element() cannot be directly resolved with then() anymore (breaking change in Protractor 2.0). Instead, do:

var elm = element(by.xpath("//tf-navpane-item[contains(@class,'tf-state-selected')]//tf-navpane-item-text//*[contains(@class,'ng-binding')]"));
elm.getText().then(function(txt) {
    console.log("txt: " + txt);
});

Note that, if you would need to assert the text, you can pass the getText() into expect() - it understands what a promise is and would resolve it before making an expectation:

expect(elm.getText()).toEqual("Expected text");
like image 116
alecxe Avatar answered Mar 07 '23 04:03

alecxe