Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get proper console logs from WebDriver (Chrome, C#)

I wrote following micro-test (it's useless but it's not the case here) in Selenium WebDriver, C#, for Chrome browser:

[Test]
    public void T2_API_001_PrintingAlerts()
    {
        pgactions.OpenDataset(driver, settings.dataset); // opening page

        for (int i = 0; i < 10; i++)
        {
            ((IJavaScriptExecutor)driver).ExecuteScript("TsTable.Admin.log('this is error message', true);");
        }

        var logs = driver.Manage().Logs.GetLog(LogType.Browser);

        foreach (var log in logs)
        {
            Console.WriteLine(log.ToString());
        }

        Thread.Sleep(100000);
    }

In debugging console in VS I expect to see (like I see in Chrome browser instance started by WebDriver):

[14:02:22.816] Settings valid!
randomTable.js:6711 [14:02:22.976] Constructing table...
randomTable.js:6711 [14:02:22.976] Validating 32 rows data set...
( some more irrelevant rows )
randomTable.js:6711 [14:02:23.375] ...DONE (0.399 sec)
randomTable.js:6711 [14:02:24.949] this is error message
randomTable.js:6711 [14:02:25.060] this is error message
randomTable.js:6711 [14:02:25.174] this is error message
randomTable.js:6711 [14:02:25.344] this is error message
randomTable.js:6711 [14:02:25.477] this is error message
randomTable.js:6711 [14:02:25.612] this is error message
randomTable.js:6711 [14:02:25.715] this is error message
randomTable.js:6711 [14:02:25.809] this is error message
randomTable.js:6711 [14:02:25.993] this is error message
randomTable.js:6711 [14:02:26.089] this is error message

but all I get is:

------ Run test started ------
NUnit VS Adapter 2.0.0.0 executing tests is started
Loading tests from C:\automated_testing\testTable\TableSelenium.dll
Run started: C:\automated_testing\testTable\TableSelenium.dll
Starting test...
[2016-05-04T12:02:16Z] [All] http://tstable.testsite.com/favicon.ico 0:0 Failed to load resource: the server responded with a status of 404 (Not Found)

(the last line matters only, but to be honest - I can't it even see that line in browser(?))

Is the problem in this line? var logs = driver.Manage().Logs.GetLog(LogType.Browser); ?

I am aware of that: https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/6832

but also saw some 'hacks' to gather error logs in different ways. The main case it - I need no errors, but ALL logs.

Any ideas?

like image 662
Tomasz Tarnowski Avatar asked May 04 '16 12:05

Tomasz Tarnowski


People also ask

Does Selenium WebDriver work with Chrome?

Through WebDriver, Selenium supports all major browsers on the market such as Chrome/Chromium, Firefox, Internet Explorer, Edge, Opera, and Safari.


1 Answers

The issue was in driver declaration section where LogLevel was missing. Now I set SetLoggingPreference in [SetUp] section:

ChromeOptions options = new ChromeOptions();
options.SetLoggingPreference(LogType.Browser, LogLevel.All);
driver = new ChromeDriver("path to driver", options);

and all logs are printed in debug console :-)

like image 67
Tomasz Tarnowski Avatar answered Oct 18 '22 06:10

Tomasz Tarnowski