Exception in httpunit

282 Views Asked by At

I am having a HttpUnit code,in which I am trying to access google''s official website.Here is my code:

/** everything you need to start is in the com.meterware.httpunit package **/ import com.meterware.httpunit.*;

/** This is a simple example of using HttpUnit to read and understand web pages. **/ public class Example {

public static void main(String[] params) {
    try {

        WebConversation wc = new WebConversation();

        String url = "https://www.yahoo.com";
        WebRequest request = new GetMethodWebRequest(url);
        WebResponse response = wc.getResponse(request);

        // WebLink httpunitLink = response.getFirstMatchingLink(
        // WebLink.MATCH_CONTAINED_TEXT, "HTML" );
        // response = httpunitLink.click();
        System.out.println("The yahoo main page '" + url + "' contains "
                + response.getLinks().length + " links");
    } catch (Exception e) {
        System.err.println("Exception: " + e);
    }
}

}

I am getting the below exception on running the code.I am using eclipse as IDE.

org.mozilla.javascript.EcmaError: TypeError: Cannot read property "className" from undefined (httpunit#3) at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3229) at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3219) at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3235) at org.mozilla.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:3254) at org.mozilla.javascript.ScriptRuntime.undefReadError(ScriptRuntime.java:3267) at org.mozilla.javascript.ScriptRuntime.getObjectProp(ScriptRuntime.java:1324) at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:2816) at script(httpunit:3) at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2251) at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:161) at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:340) at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2758) at org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:172) at org.mozilla.javascript.Context.evaluateString(Context.java:1132) at com.meterware.httpunit.javascript.ScriptingEngineImpl.runScript(ScriptingEngineImpl.java:92) at com.meterware.httpunit.scripting.ScriptableDelegate.runScript(ScriptableDelegate.java:88) at com.meterware.httpunit.parsing.NekoDOMParser.runScript(NekoDOMParser.java:151) at com.meterware.httpunit.parsing.ScriptFilter.getTranslatedScript(ScriptFilter.java:150) at com.meterware.httpunit.parsing.ScriptFilter.endElement(ScriptFilter.java:131) at org.cyberneko.html.filters.DefaultFilter.endElement(DefaultFilter.java:249) at org.cyberneko.html.filters.NamespaceBinder.endElement(NamespaceBinder.java:367) at org.cyberneko.html.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:1015) at org.cyberneko.html.HTMLTagBalancer.endElement(HTMLTagBalancer.java:888) at org.cyberneko.html.HTMLScanner$SpecialScanner.scan(HTMLScanner.java:2831) at org.cyberneko.html.HTMLScanner.scanDocument(HTMLScanner.java:809) at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:478) at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:431) at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) at org.apache.xerces.parsers.DOMParser.parse(Unknown Source) at com.meterware.httpunit.parsing.NekoHTMLParser.parse(NekoHTMLParser.java:48) at com.meterware.httpunit.HTMLPage.parse(HTMLPage.java:271) at com.meterware.httpunit.WebResponse.getReceivedPage(WebResponse.java:1301) at com.meterware.httpunit.WebResponse.getFrames(WebResponse.java:1285) at com.meterware.httpunit.WebResponse.getFrameRequests(WebResponse.java:1024) at com.meterware.httpunit.FrameHolder.updateFrames(FrameHolder.java:179) at com.meterware.httpunit.WebWindow.updateFrameContents(WebWindow.java:315) at com.meterware.httpunit.WebClient.updateFrameContents(WebClient.java:526) at com.meterware.httpunit.WebWindow.updateWindow(WebWindow.java:201) at com.meterware.httpunit.WebWindow.getSubframeResponse(WebWindow.java:183) at com.meterware.httpunit.WebWindow.getResponse(WebWindow.java:158) at com.meterware.httpunit.WebClient.getResponse(WebClient.java:122) at Example.main(Example.java:14) Exception: com.meterware.httpunit.ScriptException: Script '//used for perf beacons in 3pas rtTop = Number(new Date());

    document.documentElement.className += ' jsenabled';

    if (!("ontouchstart" in document.documentElement)) {
        document.documentElement.className += " no-touch";
    }








    (function(){var k=13,d=4,j=0,a=document.documentElement,b=[a.className],f,c=navigator.plugins,g=k;if(c&&c.length){f=c["Shockwave Flash"];if(f&&f.description){j=parseInt(f.description.match(/\b(\d+)\.\d+\b/)[1],10)||0}}else{while(g--){try{new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+g);j=g;break}catch(h){}}}b.push("flash-"+j);while(j-->d){b.push("flash-gt"+j)}a.className=b.join(" ")})();
    (function(){var j=this,n=/^(on)?load/,b=/^on/,i="addEventListener",f="attachEvent",e="_oc",h="detachEvent",g="removeEventListener",l=j[i],m=j[g],p=j[f],a=j[h],k={},d=0;function c(r,s,q){if(!r||!s){return;}if(n.test(r)){if(!s[e]){var t=++d;s[e]=t;k[t]=s;}}else{if(p&&b.test(r)){p(r,s);}else{if(l){l(r,s,q);}}}}function o(r,s,q){if(!r||!s){return;}if(n.test(r)){var t=s[e];if(t){delete k[t];}}else{if(a&&b.test(r)){a(r,s);}else{if(m){m(r,s,q);}}}}j.OnloadCache={enable:function(){if(l){j[i]=c;j[g]=o;}if(p){j[f]=c;j[h]=o;}},disable:function(){if(l){j[i]=l;j[g]=m;}if(p){j[f]=p;j[h]=a;}},dispatch:function(){var r={type:"load"},q;for(q in k){if(k.hasOwnProperty(q)){k[q](r);}}},reset:function(){k={};}};})();

OnloadCache.enable();

        var setJSCookie = true;
(function () {
    var cookieName = "FBJSC=";
    var cookieValue = "1409123746";
    var cookieIndex = document.cookie.indexOf(cookieName);
    if(cookieIndex >= 0) {
        var oldValue = document.cookie.substr(cookieIndex + cookieName.length, cookieValue.length);
        if(cookieValue <= oldValue) {
            setJSCookie = false;
        }
    }

    if(setJSCookie) {
        document.cookie = cookieName + cookieValue;
    }
})();' failed: org.mozilla.javascript.EcmaError: TypeError: Cannot read property "className" from undefined (httpunit#3)

Any help would be greatly appreciated.Thanks.enter code here

1

There are 1 best solutions below

0
On

As one of the httpunit committers I'd recommend reading the FAQ: http://httpunit.sourceforge.net/doc/faq.html

JavaScript support of httpunit is limited. The Error you are getting is showing that you ran into JavaScript code that is more complicated than what httpunit can handle. As a work-around you might want to switch off javascript altogether (if that is possible in your use case).

The following is from HttpUnit's developer FAQ:

HttpUnit 1.x support of Javascript is limited. There is a new Javascript engine in development but the development is almost stalled due to limited resources. To work around the issue you might want to

  • try the new scripting engine
  • switch off javascript
  • if you get an org.mozilla.javascript.EcmAError ... undefined ... find the missing javascript function, implement it and supply the patch for the community see "6. How can I suggest modifications to httpunit?"