0

I have also checked link: Protractor: element.getText() returns an object and not String but i found no answer for that on above link and i want string in return??

Saurabh
  • 41
  • 1
  • 2
  • 10

1 Answers1

2

All the protractor's methods return promises, to resolve that promise you need to send something like this:

element.getText().then(function(text) {
console.log(text);
});
or use "expect"-->jasmine's assertion
expect(element.getText()).toEqual("Your Text");

for detailed idea on promises i suggest please go through this link : http://www.html5rocks.com/en/tutorials/es6/promises/

Ram Pasala
  • 4,931
  • 3
  • 16
  • 26
  • I have tried : var abc= element(by.xpath("//h1[@ng-model='Save Employee']")); abc.getText().then(function(value) {console.log(value);}); //output- undefined but i want text instead of object or undefined – Saurabh Jun 04 '16 at 09:16
  • ok try this element(by.xpath("//h1[@ng-model='Save Employee']")).getText().then(function(text)){ console.log(text)}); if it doesn't work your xpath has a problem. – Ram Pasala Jun 04 '16 at 09:19
  • Xpath slow down the dom's rendering process, which in turn slow your scripts, xpaths should always be the last option, please use cssSelectors or the built in protractor locators instead. var abc = element(by.model('Save Employee')); – Ram Pasala Jun 04 '16 at 09:27
  • @Saurabh promises can take 2 arguments: a success and and error callback. try `element(by.xpath("//h1[@ng-model='Save Employee']")); abc.getText().then(function(success) {console.log("SUCCESS: "+success);}, function(error){console.log("ERROR: "+error);});` I am guessing that your xpath has a problem and the error function will help you identify it. – emory Jun 04 '16 at 10:01
  • yes, thanks igniteram it is working fine with "text" instead of "value". – Saurabh Jun 04 '16 at 10:02
  • @Saurabh sure, please accept the answer if it solves your issue. – Ram Pasala Jun 04 '16 at 10:05
  • I want to write generic test-case for above getText() so i have written that: getElementText=function(elementLocator){ elementLocator.getText().then(function(text) { return text; });}; After that i am calling whenever i need but in some case i need to split the returning value so i am doing like: var abc= getElementText(); name= abc.split(","); console.log(name[0]+"and"+name[1]); but i am getting value undefined from getElementText method and var name value is undefined, so please help how to resolve it to get splitted string value from getElementText() method – Saurabh Jun 04 '16 at 12:40
  • 1
    @Saurabh please check gitter, i have posted my answer and also please check this link for asking questions in StackOverflow : http://stackoverflow.com/help/someone-answers – Ram Pasala Jun 04 '16 at 20:43