Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Console logs returned as zero using .NET Selenium even though there are errors #14537

Closed
sponnusamyneogov opened this issue Sep 25, 2024 · 8 comments

Comments

@sponnusamyneogov
Copy link

sponnusamyneogov commented Sep 25, 2024

What happened?

var options = new ChromeOptions();
options.SetLoggingPreference(LogType.Browser, LogLevel.All);
var Driver = new ChromeDriver(options);
Driver.Url = "https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html";
Driver.FindElement(By.Id("consoleLog")).Click();
Driver.FindElement(By.Id("consoleError")).Click();

Console logs returned as zero using .NET Selenium even though there are errors

Issue:
When trying to capture browser console logs using the following code in .NET Selenium:

csharp
Copy code
var jsErrors = Driver.Manage().Logs.GetLog(LogType.Browser);
the returned log shows zero entries, even though errors are clearly present in the browser's console.

How can we reproduce the issue?

var options = new ChromeOptions();
options.SetLoggingPreference(LogType.Browser, LogLevel.All);
var Driver = new ChromeDriver(options);
 Driver.Url = "https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html";
 Driver.FindElement(By.Id("consoleLog")).Click();
 Driver.FindElement(By.Id("consoleError")).Click();

How to capture console logs using selenium and c#

Relevant log output

The following code:

csharp

jsErrors = Driver.Manage().Logs.GetLog(LogType.Browser);

returns no errors, even though there are errors present in the browser console.

Operating System

Windows 10

Selenium version

Selenium Version 4.25.0

What are the browser(s) and version(s) where you see this issue?

Chrome

What are the browser driver(s) and version(s) where you see this issue?

129.0.6668.7

Are you using Selenium Grid?

No

Copy link

@sponnusamyneogov, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

@nvborisenko
Copy link
Member

I should advertise BiDi here:

var context = await driver.AsBiDiContextAsync();

await context.Log.OnEntryAddedAsync(Console.WriteLine);

driver.Url = "https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html";

driver.FindElement(By.Id("consoleLog")).Click();
driver.FindElement(By.Id("consoleError")).Click();

And in output I see:

Console { BiDi = OpenQA.Selenium.BiDi.BiDi, Level = Info, Source = Source { Realm = OpenQA.Selenium.BiDi.Modules.Script.Realm, Context = OpenQA.Selenium.BiDi.Modules.BrowsingContext.BrowsingContext }, Text = Hello, world!, Timestamp = 9/25/2024 8:09:27 PM +00:00, StackTrace = StackTrace { CallFrames = System.Collections.Generic.List`1[OpenQA.Selenium.BiDi.Modules.Script.StackFrame] }, Method = log, Args = System.Collections.Generic.List`1[OpenQA.Selenium.BiDi.Modules.Script.RemoteValue] }
Console { BiDi = OpenQA.Selenium.BiDi.BiDi, Level = Error, Source = Source { Realm = OpenQA.Selenium.BiDi.Modules.Script.Realm, Context = OpenQA.Selenium.BiDi.Modules.BrowsingContext.BrowsingContext }, Text = I am console error, Timestamp = 9/25/2024 8:09:27 PM +00:00, StackTrace = StackTrace { CallFrames = System.Collections.Generic.List`1[OpenQA.Selenium.BiDi.Modules.Script.StackFrame] }, Method = error, Args = System.Collections.Generic.List`1[OpenQA.Selenium.BiDi.Modules.Script.RemoteValue] }

@sponnusamyneogov
Copy link
Author

I tried this and received the below error

System.ArgumentOutOfRangeException : The byte array must have a length of at least '24740' bytes.
Parameter name: internalBuffer

Stack Trace: 
System.ArgumentOutOfRangeException : The byte array must have a length of at least '24740' bytes.
Parameter name: internalBuffer

Stack Trace: 
WebSocketBuffer.Validate(Int32 count, Int32 receiveBufferSize, Int32 sendBufferSize, Boolean isServerBuffer)
ClientWebSocketOptions.SetBuffer(Int32 receiveBufferSize, Int32 sendBufferSize, ArraySegment1 buffer) <ConnectAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- ExceptionDispatchInfo.Throw() TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) <ConnectAsync>d__13.MoveNext() --- End of stack trace from previous location where exception was thrown --- ExceptionDispatchInfo.Throw() TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) <ConnectAsync>d__28.MoveNext() --- End of stack trace from previous location where exception was thrown --- ExceptionDispatchInfo.Throw() TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) <AsBiDiAsync>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- ExceptionDispatchInfo.Throw() TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) <AsBiDiContextAsync>d__1.MoveNext() --- End of stack trace from previous location where exception was thrown --- ExceptionDispatchInfo.Throw() TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) TaskAwaiter1.GetResult()
d__0.MoveNext() line 59
--- End of stack trace from previous location where exception was thrown ---
ExceptionDispatchInfo.Throw()
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
TaskAwaiter1.GetResult() GenericAdapter1.GetResult()
AsyncToSyncAdapter.Await(Func`1 invoke)
TestMethodCommand.RunTestMethod(TestExecutionContext context)
TestMethodCommand.Execute(TestExecutionContext context)
<>c__DisplayClass1_0.b__0()
DelegatingTestCommand.RunTestMethodInThreadAbortSafeZone(TestExecutionContext context, Action action)

nvborisenko added a commit to nvborisenko/selenium-hq that referenced this issue Sep 26, 2024
@nvborisenko
Copy link
Member

Thanks for highlighting this issue, already fixed.

BTW your code snippet works good for me:
image

@sponnusamyneogov
Copy link
Author

Thank you so much! Could you let me know where I can find the new jar file with your fixes?

@nvborisenko
Copy link
Member

Fix for BiDi implementation will be included in upcoming nightly build: https://github.com/SeleniumHQ/selenium/pkgs/nuget/Selenium.WebDriver

But your code works good for me, the original issue cannot be reproduced

var options = new ChromeOptions();
options.SetLoggingPreference(LogType.Browser, LogLevel.All);

using var Driver = new ChromeDriver(options);

Driver.Url = "https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html";

Driver.FindElement(By.Id("consoleLog")).Click();
Driver.FindElement(By.Id("consoleError")).Click();

var jsErrors = Driver.Manage().Logs.GetLog(LogType.Browser);

I am using ChromeDriver 129.0.6668.70.

@sponnusamyneogov
Copy link
Author

Works with latest version.

Thanks !

Output: 
[2024-09-26T15:53:38Z] [Severe] https://platform.twitter.com/js/button.b059c0eb61ea902a882e5c5b3c66a17a.js 0:0 Uncaught ReferenceError: __twttrll is not defined
TearDown : NUnit.Framework.AssertionException : [2024-09-26T15:53:38Z] [Severe] https://platform.twitter.com/js/button.b059c0eb61ea902a882e5c5b3c66a17a.js 0:0 Uncaught ReferenceError: __twttrll is not defined

Stack Trace: 
CommandFactory.LogFailure(String message) line 11272
ConsoleErrorLogger.LogJavaScriptErrors() line 127
CheckForJavascriptError.LogJavascriptErrors() line 40

@sponnusamyneogov
Copy link
Author

This code is not functioning correctly when using RemoteWebDriver with Selenium Grid.

[Test]
public void ConsoleLogsGridRun()
{
var options = new ChromeOptions();
options.SetLoggingPreference(LogType.Browser, OpenQA.Selenium.LogLevel.All);
var Driver = new RemoteWebDriver(
new Uri("http://10.0.0.37:8888/"),
new RemoteSessionSettings(options), TimeSpan.FromSeconds(100));
Driver.Url = "https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html";

Driver.FindElement(By.Id("consoleLog")).Click();
Driver.FindElement(By.Id("consoleError")).Click();

var jsErrors = Driver.Manage().Logs.GetLog(LogType.Browser);

}

These are the capabilities
image

Please advise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants