C# Selenium webdriver JavaScript errors logging

时光怂恿深爱的人放手 提交于 2020-01-23 02:45:31

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/8317705/c-sharp-selenium-webdriver-javascript-errors-logging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!