Protractor console log

AngularjsProtractor

Angularjs Problem Overview


I want to output the text of a div in my protractor test, so far I have:

console.log(ptor.findElement(protractor.By.id('view-container')).getText());

but this outputs

[object Object]

I tried "toString()" and same result.

Is there a way to output the text to the console?

Angularjs Solutions


Solution 1 - Angularjs

getText and most other Protractor methods return promises. You want to put your console.log statement inside the promise resolution:

Using the new Protractor syntax:

element(by.id('view-container')).getText().then(function(text) {
  console.log(text);
});

Solution 2 - Angularjs

this is pretty old, but as a former n00b at protractor, I wished there was more documentation.

you could also use:

element(by.id('view-container')).getText().then(console.log);

or what I like to do for readability is put all the objects on a page in their own function, section, or file:

//top declaration of variables
var viewContainer = element(by.id('view-container')).getText();

.... //bunch of code
....

viewContainer.then(console.log);

That will take care of most of your garden-variety debugging needs.

For promises in general, you could try using protractor.promise.all()

let's say you have two things that are both promises:

var getTime      = element(by.xpath(theTimeXpath)).getText();
var getPageTitle = element(by.xpath(thePageTitle)).getInnerHtml();

protractor.promise.all([getTime, getPageTitle]).then(function(theResultArray){

  var timeText           = result[0];
  var pageTitleInnerHtml = result[1];

   console.log(timeText);           // outputs the actual text
   console.log(pageTitleInnerHtml); //outputs the text of the Inner html
});

This second method is useful for when things begin to get more complex. personally, however, I find other ways around this. Although it's not bad, it's kind of funky for other developers having to read my code.

Solution 3 - Angularjs

I would like to suggest a small improvement to other answers.

short answer : I like to use browser.sleep(0).then(..); where I need to push something to protractor's flow.

it is generic and easy to move around.

tl;dr

so using the above, you can easily add a function on browser (or ptor) something like:

browser.log = function( logger, level, msg ){
     browser.sleep(0).then(function(){ logger[level](msg); });
}

or something a bit more sophisticated with apply - but that depends on your logger.

you can obviously enhance that a bit to have logger like api

var logger = browser.getLogger('name');

should be implemented like (lets assume log4js)

browser.getLogger = function( name ){
        var logger = require('log4js').getLogger(name);

        function logMe( level ) {
                 return function(msg ){
                      browser.sleep(0).then(function(){ logger[level](msg); });
                 }
             
        }

        return { info : logMe('info'), ... }
}

basically, the sky is the limit.

I am sure there's a way to make my code a lot shorter, the point is using the sleep method as basis.

Solution 4 - Angularjs

You could always assert that the text you get is the text you expect:

expect(element(by.id('view-container')).getText()).toBe('desired-text');

Solution 5 - Angularjs

you can try this one:

const textInfo = element(by.id('view-container'));
console.log('text: ', textInfo.getText());

Solution 6 - Angularjs

When user wants to log the expected and actual result in protractor always use then method implementation.

verifyDisplayedText(locator: Locator, expectedText: string) {
const text = this.getText(locator);
try {
text.then(function(value){
if (value.trim() === expectedText) {
verifyLog("VERIFICATION: PASSED. Expected: '" + expectedText + "' Actual: '" + value+"'");
} else {
errorLog("VERIFICATION: FAILED. Expected: '" + expectedText + "' Actual: '" + value+"'");
}
});
expect(text).toBe(expectedText);
}catch (error1) {
errorLog("VERIFICATION: FAILED. Expected: '" + expectedText + "' Actual: '" + text+"'");
throw error1;
}
}

Solution 7 - Angularjs

If you're in 2021, you will want to read this answer

According to protractors documentation, .getText() returns a promise. Promise is an Object is javascript. So this is what you're logging.

You need to get the value off the promise, by resolving it

The best way to handle a promise, as of 2021, is to use async/await keywords. This will make Protractor 'freeze' and wait, until the promise is resolved before running the next command

it('test case 1', async () => {
  let text = await ptor.findElement(protractor.By.id('view-container')).getText();
  console.log(text);
  // or directly
  console.log(await ptor.findElement(protractor.By.id('view-container')).getText(););
})

.then() can also be used, but using async/await will make your code a lot more readable and easier to debug.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionbmw0128View Question on Stackoverflow
Solution 1 - AngularjsJmrView Answer on Stackoverflow
Solution 2 - AngularjsZach FolwickView Answer on Stackoverflow
Solution 3 - Angularjsguy mograbiView Answer on Stackoverflow
Solution 4 - AngularjsVlad AView Answer on Stackoverflow
Solution 5 - AngularjsSeanRayView Answer on Stackoverflow
Solution 6 - AngularjsKetan PardeshiView Answer on Stackoverflow
Solution 7 - AngularjsSergey PleshakovView Answer on Stackoverflow