Console Logging - Selenium and Katalon


#1

Re-posting due to the spam :frowning:

I have tried two solutions for getting the INFO webdriver console logs for Chrome.
**
The first method,**

import org.openqa.selenium.WebDriver

import org.openqa.selenium.logging.LogEntries

import org.openqa.selenium.logging.LogEntry

import com.kms.katalon.core.webui.driver.DriverFactory

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebUI.openBrowser(null)

WebUI.navigateToUrl(url)

WebDriver driver = DriverFactory.getWebDriver()

LogEntries logs = driver.manage().logs().get(“browser”)

for (LogEntry entry : logs){
println(entry)
}

With the desired capabilities, as a list of arguments --enable-logging and --log-level=0

This method does not provide not provide any output.

**The second method,
**

DesiredCapabilities caps = DesiredCapabilities.chrome()

LoggingPreferences logPrefs = new LoggingPreferences()

logPrefs.enable(LogType.BROWSER, Level.INFO)

caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs)

WebDriver driver = new ChromeDriver(caps)

driver.get(url)

LogEntries logs = driver.manage().logs().get(“browser”)

This method **works **but forces me to launch a new instance of the webdriver and so requires me to set the desired capabilities using selenium.

------------------------------------------------------------

I want to be able to have the desired capabilities inherited from Katalon. How can I get the first method to work?


#2

Vinh Nguyen wrote something at 12/27/2017:


It looks like what you need to know.


#3

Thanks for your help. I tried the following but I can’t seem to get the webdriver to inherit the desired capabilities of Katalon.

System.setProperty(“webdriver.chrome.driver”, DriverFactory.getChromeDriverPath());

ChromeOptions options = new ChromeOptions()

DesiredCapabilities capabilities = new DesiredCapabilities()

capabilities.setCapability(ChromeOptions.CAPABILITY, options)

WebDriver driver = new ChromeDriver(capabilities)

DriverFactory.changeWebDriver(driver)

WebUI.navigateToUrl(url)

WebUI.delay(GlobalVariable.medium_wait)

LogEntries logs = driver.manage().logs().get(“browser”)

for (LogEntry entry: logs){

println(entry.getMessage())

}

WebUI.closeBrowser()


#4

but I can’t seem to get the webdriver to inherit the desired capabilities of Katalon.

You are right. In the code you create a new instance of ChromeDriver and let Katalon to use it. Katalon does not modify any desired capabilities of your instance.


#5

I would suggest the following alternative:

import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.logging.LogEntries
import org.openqa.selenium.logging.LogEntry
import org.openqa.selenium.remote.DesiredCapabilities
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.driver.WebUIDriverType
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
WebUI.openBrowser('')
def url = 'http://demoaut.katalon.com'
WebUI.navigateToUrl(url)
WebUI.delay(GlobalVariable.medium_wait)
WebUIDriverType executedBrowser = DriverFactory.getExecutedBrowser()
switch(executedBrowser) {
    case WebUIDriverType.CHROME_DRIVER:
        WebDriver driver = DriverFactory.getWebDriver()
        LogEntries logs = driver.manage().logs().get("browser")
        for (LogEntry entry: logs) {
            println(">>> ${entry.getMessage()}")
        }
}
WebUI.closeBrowser()

In this alternative, you use the WebDriver instance created by Katalon Studio. You check if it is an instance of ChromeDriver; if so you call Selenium Logging API and get log information from the Chrome; the print the obtained messages.


#6

In this alternative, you use the WebDriver instance created by Katalon Studio. You check if it is an instance of ChromeDriver; if so you call Selenium Logging API and get log information from the Chrome; the print the obtained messages.

Great, thanks that works!!! :smiley: Do you know the appropriate desired capabilities for getting info logs? I’ve tried adding --log-level=INFO, --verbose and --enable-logging but no luck.


#7

Do you know the appropriate desired capabilities for getting info logs?

None is there. I think it is unlikely that some desired capability is defined in future for getting browser logs.

-------------

I will tell you why.

LogEntries logs = driver.manage().logs().get("browser")

The above one line is using Selenium Logging API. See https://github.com/SeleniumHQ/selenium/wiki/Logging. The Selenim Logging API was a part of the Selenium WebDriver API. The Selenium WebDriver API was defined by the Selenium project sometime around 2009.

Nowadays, the Selenium WebDriver API has become a sort of heritage; it is destined to be superseded by the W3C WebDrvier specification (https://www.w3.org/TR/webdriver/). All of browsers are supposed to support the W3C WebDriver as de facto.

The Desired Capability concept is a part of the W3C Web Driver specification: https://www.w3.org/TR/webdriver/#capabilities

We should be aware that the W3C WebDriver API does NOT include any Logging API. The W3C WebDriver team is not going to put high priority to Selenium Logging API-equivalent feature. You can read a comment by a member of the W3C WebDriver spec team in the following post:

https://github.com/mozilla/geckodriver/issues/330

andreastt commented on 25 Apr 2017

Well, we need to come to agreement with the other browser vendors about a sensible API. I don’t think the polling nature of the Selenium API is particularly sensible, but it may be the only option given the restrictions of having an HTTP protocol.

The tracking issue on the WebDriver standard is w3c/webdriver#406, but as we are just finishing up “level 1” for Candidate Recommendation, it is unlikely to be worked on in the near future.


#8

As you may or may not be aware of, you can not get Console Log from Firefox. I mean, the following code does not work for Firefox.

LogEntries logs = driver.manage().logs().get("browser")

The standard FirefoxDriver implementation (which is bundled in Katalon Studio) once was GeckoDriver. But now Marionette is bundled. See the following post:
https://stackoverflow.com/questions/43272919/difference-between-webdriver-firefox-marionette-webdriver-gecko-driver
Marionette is the implementation of the W3C WebDriver spec, which no longer comply with the good-old Selenium WebDriver API. Therefore we cannot get Console log from Firefox.

I would not be surprised if sometimes in future we can no longer get Console Log from the Google Chrome as well.


#9

Sorry if I misunderstand you but do mean that the getting the browser logs may soon no longer be possible?

I do know that my current chromedriver does have logging capabilities since in the if I input chromedriver -help into the command prompt I get the following.

Capture.PNG


#10

Bella,

the getting the browser logs may soon no longer be possible?

Sorry for making you confused.
What I wrote about Chrome is groundless. Please forget it.
What I wrote about Firefox is, I think, solid. Please note it.


#11

I’m aware of the issue with firefox but since I’m using chrome might it be possible to get the info logs? I just can’t seem to get the desired capabilities correct. :frowning:


#12

I’m using chrome might it be possible to get the info logs?

Yes, I have ever done it successfully by “LogEntries logs = driver.manage().logs().get(“browser”)”. You should be able to do it as well.

I just can’t seem to get the desired capabilities correct.

I believe no desired capabilities for Logging defined.


#13

Would be able to tell me if you used --verbose, --log-level=ALL, --enable-logging or anything like that? I gave them as arguments but have since been unsuccessful. Any guidance you could give would be appreciated.


#14

if you used --verbose, --log-level=ALL, --enable-logging

I do not quite understand what you mean.

Do you mean giving these options for chromedriver.exe? Specifying --log-level=ALL to chromedriver.exe would let chromedirver.exe to log all it is designed to — which does not include asking Chrome browser to transfer its internal console.log("…") texts to you and merge the texts into the chromedriver.log file.


#15

So my aim is to simply get the browser console logs and print them.
I have tried the following method,

WebUI.openBrowser(null)

WebUI.navigateToUrl(url)

//Get console logs from browser

WebDriver driver = DriverFactory.getWebDriver()

LogEntries logs = driver.manage().logs().get(“browser”)

for (LogEntry entry in logs){
println(entry)
}

And this appears to work for other people but I have been unable to get the same results.

I assume this is a problem with the desired capabilities.
I have tried a combination of desired capabilities including the ones mentioned. In particular, I thought adding both --verbose and --enable-logging should have yielded the INFO browser logs.


#16

Any error message?

-------------------------------

I doubt this line:

WebUI.openBrowser(null)

Rewrite it as follows and try:

WebUI.openBrowser('')

#17

kazurayam said:

Any error message?

-------------------------------

I doubt this line:

WebUI.openBrowser(null)

Rewrite it as follows and try:

WebUI.openBrowser('')

No error messages. It opens the url in the browser as expected but in the katalon console no browser logs are printed. As I understand it, WebUI.openBrowser(null) is a correct alternative to WebUI.openBrowser(’ ') but I did try rewriting it. No change. :confused:


#18

There must be some reason why you are not successful yet. But I can not see why.

All I can do more is to show how I did with running code. I have made a working demo project and published it on the GitHub.

My test case is this:

This project worked for me. I could get JavaScript Console Log from Chrome browser like this:

>>> [2018-07-25T09:44:47+0900] [SEVERE] http://demoaut.katalon.com/favicon.ico - Failed to load
>>> [2018-07-25T09:44:48+0900] [WARNING] http://demoaut.katalon.com/profile.php#login - This page includes a password or credit card input in a non-secure context. A warning has been added to the URL bar. For more information, see https://goo.gl/zmWq3m.


#19

Hey, thank you so much for all the effort you’ve gone to. In fact, when I try your code I don’t get any output either. Strange.

I assume I should see it between the following console statements?

07-25-2018 04:53:35 PM - [START] - Start action : Statement - If (executedBrowser == CHROME_DRIVER)

07-25-2018 04:53:35 PM - [END] - End action : Statement - If (executedBrowser == CHROME_DRIVER)
/////////////////////////////////////HERE//////////////////////////////////////

07-25-2018 04:53:35 PM - [START] - Start action : closeBrowser

07-25-2018 04:53:35 PM - [INFO] - Closing browser


#20

Possibly you have your Chrome DevTool > Console > Filter >Hide all
like this:


If “Hide all” is chosen, you will have no message there of course.

ChromeConsoleHideAll.png