How to get Javascript console message

481 views
Skip to first unread message

lorenoo...@gmail.com

unread,
Oct 9, 2014, 1:57:24 PM10/9/14
to sele...@googlegroups.com
Hi Andrei,

did you ever had to get javascript console messages to debug some issue?

I'm trying to log some console.log() messages produced by my JS code with the following code:

private void printLogs( String prefix ) {
Set<String> logTypes = WebDriverRunner.getWebDriver().manage().logs().getAvailableLogTypes();
for( String logType : logTypes ) {
LogEntries logs = WebDriverRunner.getWebDriver().manage().logs().get( logType );
for( LogEntry log : logs.getAll() ) {
System.out.println( prefix + ": " + log.getLevel() + " [" + new Date( log.getTimestamp() ) + "] " + log.getMessage() );
}
}
}

However, what this code prints is more like internal browser's messages, so that my console.log() messages are not returned.

Did you ever had a need like this?

Loreno

PS: I'm not sure if this is a selenide or selenium question. I'm posting here in a first moment because the logs returned by webdriver can be related to the way the firefox driver is being created by selenide (according to this answer in SO: http://stackoverflow.com/questions/18261338/get-chromes-console-log/18283831#18283831)...

Andrei Solntsev

unread,
Oct 10, 2014, 11:19:15 AM10/10/14
to lorenoo...@gmail.com, sele...@googlegroups.com
Hi Loreno!
I would also want to get browser logs. I have even created a special method Selenide.getWebDriverLogs():


The problem is, that this method doesn't return required info. It seems to be a problem of concrete webdriver implementations. 

I can suggest 2 options:

Andrei Solntsev

--
You received this message because you are subscribed to the Google Groups "selenide" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenide+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

lorenoo...@gmail.com

unread,
Oct 10, 2014, 12:10:42 PM10/10/14
to Andrei Solntsev, sele...@googlegroups.com
Thanks Andrei.

What I did in the mean time since my email was something close to your second hint. However, I wrote an utility for storing log messages in the local storage. I preferred this approach over storing in memory because there are certain actions in my app that redirect the user between different pages (I mean, pages with refresh). Using the local storage I can preserve messages even across page refreshes.

Once the data is stored in local storage, I can get them in my test with executeJavaScript( "return localStorage.getItem( 'logs' )" ).

Loreno
Reply all
Reply to author
Forward
0 new messages