3

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

2 Answers 2

3

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...

Sign up to request clarification or add additional context in comments.

6 Comments

Thank for your answer. I want capture javascript errors generated by the browser. I heard about JSErrorCollector - mguillem.wordpress.com/2011/10/11/…. Maybe it is possible to use it in C#?
You could definitely do that in C#, and it would be somewhat easier to do than installing the Firebug and ConsoleExport plugins. However, I'm not familiar enough with JSErrorCollector to know how to use it - so it might actually require a C# port before it would really work as outlined in the blog.
I just looked it over, and it's completely do-able in C#, all you need to do is grab JSErrorCollector.xpi and install it in a custom Firefox profile, and then you execute "List<object> errors = (List<object>)((IJavaScriptExecutor)_driver).executeScript("return window.JSErrorCollector_errors.pump()");" to get a list of javascript errors.
But it is solution for Firefox. Any ideas about IE?
You might be out of luck with IE, the best you could do is inject javascript as outlined in the blog you mentioned, but like it says, you would miss any errors hit before that script is injected...
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.