3

I tried to use the following code but do not know how to integrated it into existing selenium code:

<Existing code>

IJavaScriptEngine monitor = new JavaScriptEngine(driver);
List<string> consoleMessages = new List<string>();
monitor.JavaScriptConsoleApiCalled += (sender, e) =>
{
    consoleMessages.Add(e.MessageContent);
};
await monitor.StartEventMonitoring();

<Existing code>
someButton.Click(); // <== I can never get to this line of code. I'm stuck on the await line.
int messageCount = consoleMessages.Count;

The above code came from the following Selenium Docs link: Selenium Docs BiDi

The problem is that I cannot get past the 'await monitor.StartEventMonitoring()' line of code. I thought I would 'startMonitoring' ... then this monitoring task would run in the background, and the 'someButton.Click()' would then execute. But the code is waiting for 'await monitor.StartEventMonitoring()' line to complete before going onto the next line of code. Guessing this is an issue with my understanding of async/await. I thought the monitor would run in the background and I could then check the consoleMessages List every so often to see if any messages had been added to the list.


I have used the old (old old) method of listening to chrome browser without any issues.

ChromeOptions chromeOptions = new ChromeOptions();

// https://stackoverflow.com/questions/18261338/get-chromes-console-log
var co = new ChromeOptions();
co.SetLoggingPreference(LogType.TestBrowser, LogLevel.All);
                    
<Existing selenium code here>

// Check the log to see if anything had been added
var logList = Driver.Manage().Logs.GetLog(LogType.Browser);

<More existing selenium code here>

But unsure how to use Selenium's BiDi apis

Fractal
  • 1,748
  • 5
  • 26
  • 46

2 Answers2

0

Try writing to the list, so each event is captured and written to the console using .WriteLine, instead of .Add (that just adds the item to the list, but may not be streamed to console.log in a standard output text)

consoleMessages.WriteLine(e.MessageContent);

** Update **
Selenium have updated the webpage with a change from their original code:

IJavaScriptEngine monitor = new JavaScriptEngine(driver);
List<string> consoleMessages = new List<string>();
monitor.JavaScriptConsoleApiCalled += (sender, e) =>
{
    Console.WriteLine("Log: {0}", e.MessageContent);
};
await monitor.StartEventMonitoring();

See if that works now BiDi APIs - Listen to console.log events

djmonki
  • 3,020
  • 7
  • 18
  • @Fractal - see update as per Selenium documentation – djmonki Nov 18 '21 at 15:11
  • what's the point of creating consoleMessages? How do you store e.MessageContent? Btw, I was trying to find IJavaScriptEngine's code... there didn't seem to be a definitive source there... what's the source of that? – pcalkins Nov 18 '21 at 20:02
  • I think Taco's answer needs to be combined with this... so something needs to listen to notifications/events from a thread here, no? A go-between for monitor and consoleMessages? – pcalkins Nov 18 '21 at 20:20
  • Agreed, @TacoVerhagen, do you want to take the lead on this. You have more knowledge about C than I do and you would provide a better construct than me. Take my answer which is directly from the website and tailor your code round it, see if it can help our OP – djmonki Nov 18 '21 at 22:43
0

To my best knowledge with await you tell C# to wait until the task is finished. The problem being, that task does not finish until you close the browser. It keeps waiting and logging for ever.

So I would expect to create a task that runs forever and logs in the background like this:

var task = monitor.StartEventMonitoring(); 
// code that generates logging here
// compiler probably wines about task without asynchrone methode here

Or this to run in true parallel:

var task = Task.Run(()=> monitor.StartEventMonitoring()); 
// code that generates logging

The old code code gets the logs up until now and then returns. It lacks await/async (issues and possibilities)

Taco Verhagen
  • 222
  • 1
  • 6