Browser√console output messages using Selenium.

5,319 views
Skip to first unread message

RV

unread,
Sep 17, 2013, 4:28:23 PM9/17/13
to webd...@googlegroups.com
Hi,

I've a need to capture browser console messages using selenium WD.  Is this possible?

-Raj,

Krishnan Mahadevan

unread,
Sep 17, 2013, 10:45:41 PM9/17/13
to webd...@googlegroups.com
Raj,
Yes I believe this is possible.

If you are working not the Grid mode then you could however do a simple HttpPost to this end-point to get this : ( I know that the APIs should ideally work for me, but I haven't managed to get the WebDriver APIs do this for me in the Grid mode ]


The URL would need to be prefixed with http://localhost:5555/wd/hub/ [ here 5555 is the IP address of the node and NOT the grid ]

I believe there are APIs exposed by WebDriver as well to help you do this :

Here's a sample that shows you how to do this [ I am on Selenium 2.35.0 ]

package organized.chaos;


import java.util.logging.Level;


import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.logging.LogEntries;

import org.openqa.selenium.logging.LogEntry;

import org.openqa.selenium.logging.LogType;

import org.openqa.selenium.logging.LoggingPreferences;

import org.openqa.selenium.remote.CapabilityType;

import org.openqa.selenium.remote.DesiredCapabilities;

import org.testng.annotations.Test;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.AfterMethod;


public class FetchBrowserConsoleLogsTest {

    private WebDriver wd = null;


    @Test

    public void f() {

        wd.get("http://www.facebook.com");

    }


    @BeforeMethod

    public void beforeMethod() {

        DesiredCapabilities dc = DesiredCapabilities.firefox();

        LoggingPreferences prefs = new LoggingPreferences();

        prefs.enable(LogType.BROWSER, Level.ALL);

        dc.setCapability(CapabilityType.LOGGING_PREFS, prefs);

        wd = new FirefoxDriver(dc);

    }


    @AfterMethod

    public void afterMethod() {

        if (wd != null) {

            LogEntries logEntries = wd.manage().logs().get(LogType.BROWSER);

            for (LogEntry eachEntry : logEntries.getAll()){

                System.out.println(eachEntry.toString());

            }

            wd.quit();

        }

    }


}



Thanks and Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/groups/opt_out.

Vasikarla, Raj

unread,
Sep 17, 2013, 10:47:27 PM9/17/13
to webd...@googlegroups.com
Thanks Krishnan!!!
appreciate your prompt response!!

Krishnan Mahadevan

unread,
Sep 17, 2013, 10:49:40 PM9/17/13
to webd...@googlegroups.com
Ah!! Typos…

This should have been :
The URL would need to be prefixed with http://localhost:5555/wd/hub/ [ here 5555 is the port numberof the node and NOT the grid ]

Thanks and Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

Vasikarla, Raj

unread,
Sep 17, 2013, 10:53:30 PM9/17/13
to webd...@googlegroups.com
Thx Krishnan!!

sorathiya amar

unread,
May 12, 2016, 12:39:37 AM5/12/16
to webdriver
Hi Krishna,

I have used this code but Console log available from this code is not same as what i can see in my firefox-> Firebug->Console->All tab.

My requirement is to get all Messages available under ALL tab of console.

Plz help

⇜Krishnan Mahadevan⇝

unread,
May 12, 2016, 4:13:41 AM5/12/16
to webdriver
Maybe you could iterate through the different LogTypes and see which gives you what you need.

Thanks & Regards

Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

Paramasivam K

unread,
Nov 9, 2016, 9:19:50 PM11/9/16
to webdriver, vasika...@gmail.com
Hi,

I am trying to capture logs logged from
console.log("your message")

But i could not get console.log messages. I have tried all type of logs. Can some one help me ?

Thanks,
Param

David

unread,
Nov 10, 2016, 4:10:35 PM11/10/16
to webdriver, vasika...@gmail.com
Here's a hacky way of doing it. And this only works after page load, so it won't capture any console logs before the page load completes. The hack here requires you to execute javascript with WebDriver to set things up. You do this before doing anything else with WebDriver after the page loads, then continue with your automation, and when you want the logs for the current page, run some more javascript with WebDriver to fetch the logs. This hack has to be done on every page load, as it gets wiped out when navigating to a new page or when reload/refresh current page. The workaround to not having to do this for every page load is to have your developers "inject" the javascript setup code on page load for the website or web app under test, when using via test/automation mode, that way you don't have to do the injection from WebDriver.

So here's the step:

Execute this javascript with WebDriver after page load:

window.consoleLogs = "";
console
.log = function(msg){ window.consoleLogs += msg + "\n"; };


This will define a global variable/property consoleLogs under window object (you could alternatively do var consoleLogs = "";  instead). The 2nd line redefines the console.log function to do something else, in this case, to append the text to the global variable we defined earlier. So whenever it gets called, it will do that instead. I'm not sure how you could redefine console.log to do what it currently does and do something else at the same time, might want to ask on StackOverflow as a javascript question on how to do that.

Then when you are ready to retrieve the logs, execute this javascript from WebDriver:

return window.consoleLogs; //or return the global variable you defined if named something else

you can then save that return value into a variable in the language you are using for WebDriver and can then access the log text. If you want to clear the console log text, you'll have to run javascript from WebDriver to reassign the global variable back to empty string to clear it out, or redefine "console.clear()" function to do that for you, so you can call console.clear() in javascript instead.

This hack approach should be cross browser compatible, except for maybe very old versions of IE. And this hack method works whether you run in Selenium Grid or not.

On Tuesday, September 17, 2013 at 1:28:23 PM UTC-7, RV wrote:

David

unread,
Nov 10, 2016, 4:41:02 PM11/10/16
to webdriver, vasika...@gmail.com
FYI, example of redefining console.log in javascript but still keeping the old functionality: http://stackoverflow.com/questions/15657607/javascript-override-console-log-and-keep-the-old-function
Reply all
Reply to author
Forward
0 new messages