简体   繁体   中英

C# Selenium webdriver Catch javascript errors in Internet explorer

是否有可能捕获并记录在IE中通过我的测试运行可能发生的所有JS错误?

There are typically two classes of JavaScript error that one wants to catch when asking a question like this. The first is wanting to catch JavaScript errors in the source code of the website being automated. The second type of error is to attempt to catch JavaScript errors in JavaScript code executed by your WebDriver code. Unfortunately, there is not a good way to catch all JavaScript errors in IE via Selenium WebDriver. This is a common feature request for the library, with an open enhancement in the project's issue tracker . There are some techniques you can use to make a best effort at catching JavaScript errors, but they all have their limitations.

For the first class of JavaScript error, you can try to inject a script that hooks the window.onError event in the page. The limitation here is that it will miss any JavaScript errors that occur in scripts that execute before your onError hook is activated. Frequently, this will miss many errors that occur in scripts that are executed during the onLoad event.

For the second class of errors, the way the IE driver executes JavaScript, the script execution engine of IE does not allow us to catch the errors in execution. When the script is executed, if there is an error, the driver receives an HRESULT from the script engine that says, "I already displayed the error in the UI, so I'm not going to give you any further information." In this case, increasing the logging level of the driver will only tell you that a JavaScript error occurred, but not any other information about the JavaScript error. For this class of error, the development team is actively investigating possible solutions, but there is no clear-cut road forward here, nor is there any timeframe for a change in this behavior.

I believe you can use the log switch

--log-file=<logFile>

as explained in the documentation: http://code.google.com/p/selenium/wiki/InternetExplorerDriver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM