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??

1
  • Looks like an exact duplicate to me. Correct me if I am wrong. Thanks. Commented Jun 5, 2016 at 20:49

1 Answer 1

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/

Sign up to request clarification or add additional context in comments.

8 Comments

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
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.
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'));
@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.
@Saurabh please check gitter, i have posted my answer and also please check this link for asking questions in StackOverflow : stackoverflow.com/help/someone-answers
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.