tags:

views:

68

answers:

3

I have stored a webpage's HTML in the database.

I want to take advantage of HtmlUnit's ability to find/reference DOM elements.

Is it possible to load the HtmlPage object from a string (via a database column)?

A: 

uhhmm well yes.

You just need to serve it ( if you're using java you can use Tomcat for that ) and point your test to the served page.

OscarRyz
+2  A: 

StringWebResponse may help.

Edit: example:

    URL url = new URL("http://www.example.com");
    StringWebResponse response = new StringWebResponse("<html><head><title>Test</title></head><body></body></html>", url);
    HtmlPage page = HTMLParser.parseHtml(response, new TopLevelWindow("top", new WebClient()));
    System.out.println(page.getTitleText());
Mirko Nasato
can you help out with the code please? HtmlPage page = new HtmlPage(????);
mrblah
ah ok, great that worked out perfect!
mrblah
+2  A: 

I assume you're using HtmlParser.parseHtml to create the HtmlPage object and just need a WebResponse to pass to it?

If so, StringWebResponse will wrap your string so you can pass it directly to parseHtml.

ZoogieZork
I can't find HtmlParser in HtmlUnit, are you referring to another lib?
mrblah
http://htmlunit.sourceforge.net/apidocs/com/gargoylesoftware/htmlunit/html/HTMLParser.html#parseHtml%28com.gargoylesoftware.htmlunit.WebResponse,%20com.gargoylesoftware.htmlunit.WebWindow%29
Mirko Nasato