Oleg Strokatyy
Oleg Strokatyy

Reputation: 641

C# Selenium webdriver JavaScript errors logging

I'm testing with Selenium webdriver using C#. How can I log all JavaScript errors that could happen through my tests?

Upvotes: 3

Views: 3402

Answers (2)

Agent Shoulder
Agent Shoulder

Reputation: 585

Regarding some of the comments to the above reply, you should get the javascript errors generated during test execution by using the webdrivers built-in methods for this:

Driver().Manage().Logs.GetLog();

By specifying what log you are interested in you can get the browser log, that is:

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

That will return a ReadOnlyCollection with all the log entries from the webdriver browser window.

Upvotes: 0

Anders
Anders

Reputation: 15397

That depends what you mean, if you want to capture javascript errors generated in your code when you use:

((IJavaScriptExecutor)_driver).ExecuteScript("some javascript code here")

Then just wrap those statements in a try/catch/finally and log the exception.

If you want to capture javascript errors generated by the browser, then the short answer is: you can't easily do so.

The long answer:

  1. Use the Firefox driver
  2. Instantiate it with a custom profile
  3. install the Firebug and ConsoleExport plugins
  4. Appropriately configure those plugins via SetPreference() so that it will automatically export the console to a location of your choice

If you need some sample code, let me know and I'll give you the really long answer...

Upvotes: 3

Related Questions