How to enable catch Javascript error in a page via .net.

35 views
Skip to first unread message

Xun He

unread,
Jan 8, 2016, 9:47:19 PM1/8/16
to phantomjs

I am using .net PhantomJS lib(namespace OpenQA.Selenium.PhantomJS) to implement a project trying to catch all the script error within a page. I couldn't find a way to enable onError described in this page:

There is no option in PhantomJSDriverService class, neither a PhantomJS command line option. How can I enable this in .net?

My code snippet:
            PhantomJSDriverService service = PhantomJSDriverService.CreateDefaultService(myPhantomJSPath);
            PhantomJSDriver) webDriver = new PhantomJSDriver(service);
//onError.js contains the code in http://phantomjs.org/api/phantom/handler/on-error.html to enable catching error.
            string script = File.ReadAllText("onError.js");
            var result = webDriver.ExecutePhantomJS(script);
            EventFiringWebDriver driver = new EventFiringWebDriver(webDriver);
            driver.ExceptionThrown += myExceptionHandler;

It will throw script error exception on a call to execute script directly on my code, like this:
            Driver.ExecuteScript("var y=window.prompt(\"Test script with error\");window.alert(z);");
but won't throw similar script error exception on a page which contains similar error, script portion of my page looks like:
<script>
var y=1;alert(mm);
</script> 

Does anyone know how to make this work on throwing error on script error on a page?

Thanks,
Xun
Reply all
Reply to author
Forward
0 new messages