简体   繁体   中英

driver.manage().logs().get("browser"); does not work when called again

I'm trying to capture the browser console logs of Chrome using selenium webdriver. I used the below method twice in a @Test method. It returns the console logs when it is executed for the first time but when I call it again it returns an empty log entry.

public String getConsoleLogs(WebDriver driver)
{
     String consoleError="";
     LogEntries log=driver.manage().logs().get("browser"); 
     for(LogEntry entry:logs.getAll())
            {
                  consoleError+=entry.toString()
             }
     return consoleError;
}

Edit: Altered the code to give more clarity on my issue. When this is method is called the second time, the consoleError is an empty String.

It returns log entries not string. Use the following will solve your issue

 for (LogEntry entry : driver.manage().logs().get(LogType.BROWSER)) {
        String msg = entry.getMessage();
        System.err.println("Message: " + msg);
    }

Quoting the Webdriver Docs:

Note that log buffers are reset after each call, meaning that available log entries correspond to those entries not yet returned for a given log type. In practice, this means that this call will return the available log entries since the last call, or from the start of the session.

If this doesn't fit your needs, you need to push the logs onto a stack upon reading them so you can refer to them later.

Return type of driver.manage().logs().get("browser"); is LogEntries not String Just change the it

public LogEntries getConsoleLogs(WebDriver driver) {
    return driver.manage().logs().get("browser");
}

You have get all the log entries and then need to iterate whenever you want

eg

for (LogEntry entry : getConsoleLogs(driver)) {
        System.out.println(entry.getMessage());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM