Search code examples
javascriptrubyseleniumwebdriverhtmlunit

Is it possible to ignore JavaScript exceptions when working with WebDriver (HtmlUnit, Ruby bindings)


HtmlUnit throws exception and crash my test when I'm loading the page

caps = Selenium::WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled => true)
driver = Selenium::WebDriver.for(:remote, :desired_capabilities => caps)
driver.navigate.то url

ReferenceError: "x" is not defined. (net.sourceforge.htmlunit.corejs.javascript.EcmaError)

No exception is thrown if I use a Firefox driver.

caps = Selenium::WebDriver::Remote::Capabilities.firefox

Or disable JavaScript for HtmlUnit driver

caps = Selenium::WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled => false)

I am unable to change the code on the test page and fix the problem, so I need to either ignore it or in any way to use Firefox JavaScript Engine instead of the standard HtmlUnit JavaScript Engine.

Is it possible to solve my problem without changing the code of test page?

Update: Tried Capybara + WebKit as an alternative to Selenium + HtmlUnit - works fine, without errors. But still I would like to solve the problem without changing the framework.


Solution

  • After looking at the source of the HtmlUnitDriver, it seems like there is no possibility to customize the behaviour you want to change. The easiest thing you could do to solve this is to patch and recompile the Selenium server (which might or might not be an option). You'd need to add this line:

    --- HtmlUnitDriver.java 2012-01-05 17:45:22.779579136 +0100
    +++ HtmlUnitDriver.java 2012-01-05 18:14:51.415106195 +0100
    @@ -255,6 +255,7 @@
         WebClient client = newWebClient(version);
         client.setHomePage(WebClient.URL_ABOUT_BLANK.toString());
         client.setThrowExceptionOnFailingStatusCode(false);
    +    client.setThrowExceptionOnScriptError(false);
         client.setPrintContentOnFailingStatusCode(false);
         client.setJavaScriptEnabled(enableJavascript);
         client.setRedirectEnabled(true);