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
CodePudding user response:
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);
CodePudding user response:
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)