I'm testing with Selenium webdriver using C#. How can I log all JavaScript errors that could happen through my tests?
2 Answers
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:
- Use the Firefox driver
- Instantiate it with a custom profile
- install the Firebug and ConsoleExport plugins
- 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...
-
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#?Oleg Strokatyy– Oleg Strokatyy11/30/2011 20:59:05Commented Nov 30, 2011 at 20:59
-
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.Anders– Anders11/30/2011 21:26:25Commented Nov 30, 2011 at 21:26
-
2I 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.Anders– Anders11/30/2011 21:38:57Commented Nov 30, 2011 at 21:38
-
But it is solution for Firefox. Any ideas about IE?Oleg Strokatyy– Oleg Strokatyy12/01/2011 12:32:26Commented Dec 1, 2011 at 12:32
-
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...Anders– Anders12/01/2011 14:40:28Commented Dec 1, 2011 at 14:40
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.